在Android中实现计算功能是比较基础的,很多刚入门的Android开发人员都可以自主动手尝试一下。
效果如图:
activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:gravity="clip_horizontal"
- android:orientation="vertical"
- android:padding="30dp">
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="操作数:"
- android:textSize="20sp" />
-
- <EditText
- android:id="@+id/firstNum"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:hint="请输入数值操作数"
- android:textStyle="bold"
- android:inputType="number" />
- </LinearLayout>
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="算术运算:"
- android:textSize="20sp">
-
- </TextView>
-
- <Spinner
- android:id="@+id/operator"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:entries="@array/sign" />
- </LinearLayout>
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="操作数:"
- android:textSize="20sp" />
-
- <EditText
- android:id="@+id/secondNum"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:hint="请输入数值操作数"
- android:textStyle="bold"
- android:inputType="number" />
- </LinearLayout>
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
-
- <Button
- android:id="@+id/calc"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="等于:" />
-
- <TextView
- android:id="@+id/result"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="5"
- android:hint="计算结果"
- android:padding="15dp"
- android:textColor="#F44336"
- android:textSize="25sp"
- android:textStyle="bold" />
- </LinearLayout>
- </LinearLayout>
arrays.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string-array name="sign">
- <item>请选择运算符</item>
- <item>+</item>
- <item>-</item>
- <item>*</item>
- <item>/</item>
- </string-array>
- </resources>
MainActivity
- package com.jld.homework;
-
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Spinner;
- import android.widget.TextView;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- public class MainActivity extends AppCompatActivity {
- String op;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- Spinner spinner = (Spinner) this.findViewById(R.id.operator);//获取活动布局中的Spinner对象
- //为Spinner注册内部监听器对象
- spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
- @Override
- public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
- //将Spinner选项的值赋值给成员变量op(保存算术运算符)
- op = ((TextView) view).getText().toString();
- }
- @Override
- public void onNothingSelected(AdapterView<?> parent) {
- }
- });
-
- //各类组件
- Button calcButton = findViewById(R.id.calc);
- EditText firstNum = findViewById(R.id.firstNum);
- EditText secondNum = findViewById(R.id.secondNum);
- TextView result = findViewById(R.id.result);
-
- //计算核心
- calcButton.setOnClickListener(v -> {
- switch (op) {
- case "+": {
- double r = Double.parseDouble(firstNum.getText().toString()) + Double.parseDouble(secondNum.getText().toString());
- result.setText(String.valueOf(r));
- break;
- }
- case "-": {
- double r = Double.parseDouble(firstNum.getText().toString()) - Double.parseDouble(secondNum.getText().toString());
- result.setText(String.valueOf(r));
- break;
- }
- case "*": {
- double r = Double.parseDouble(firstNum.getText().toString()) * Double.parseDouble(secondNum.getText().toString());
- result.setText(String.valueOf(r));
- break;
- }
- case "/": {
- double r = Double.parseDouble(firstNum.getText().toString()) / Double.parseDouble(secondNum.getText().toString());
- result.setText(String.valueOf(r));
- break;
- }
- default://非法情况报错
- result.setText(R.string.ERROR);
- break;
- }
- });
- }
- }
以上就是本文的全部内容,希望对大家的学习有所帮助
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。