Android简易计算器实现功能附源码

2021-06-23 14:35:54 浏览数 (4846)

在Android中实现计算功能是比较基础的,很多刚入门的Android开发人员都可以自主动手尝试一下。

效果如图:


activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:gravity="clip_horizontal"
  6. android:orientation="vertical"
  7. android:padding="30dp">
  8.  
  9. <LinearLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content">
  12.  
  13. <TextView
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_weight="1"
  17. android:text="操作数:"
  18. android:textSize="20sp" />
  19.  
  20. <EditText
  21. android:id="@+id/firstNum"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_weight="1"
  25. android:hint="请输入数值操作数"
  26. android:textStyle="bold"
  27. android:inputType="number" />
  28. </LinearLayout>
  29.  
  30. <LinearLayout
  31. android:layout_width="match_parent"
  32. android:layout_height="wrap_content">
  33.  
  34. <TextView
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:layout_weight="1"
  38. android:text="算术运算:"
  39. android:textSize="20sp">
  40.  
  41. </TextView>
  42.  
  43. <Spinner
  44. android:id="@+id/operator"
  45. android:layout_width="wrap_content"
  46. android:layout_height="wrap_content"
  47. android:layout_weight="1"
  48. android:entries="@array/sign" />
  49. </LinearLayout>
  50.  
  51. <LinearLayout
  52. android:layout_width="match_parent"
  53. android:layout_height="wrap_content">
  54.  
  55. <TextView
  56. android:layout_width="wrap_content"
  57. android:layout_height="wrap_content"
  58. android:layout_weight="1"
  59. android:text="操作数:"
  60. android:textSize="20sp" />
  61.  
  62. <EditText
  63. android:id="@+id/secondNum"
  64. android:layout_width="wrap_content"
  65. android:layout_height="wrap_content"
  66. android:layout_weight="1"
  67. android:hint="请输入数值操作数"
  68. android:textStyle="bold"
  69. android:inputType="number" />
  70. </LinearLayout>
  71.  
  72. <LinearLayout
  73. android:layout_width="match_parent"
  74. android:layout_height="wrap_content">
  75.  
  76. <Button
  77. android:id="@+id/calc"
  78. android:layout_width="wrap_content"
  79. android:layout_height="wrap_content"
  80. android:layout_weight="1"
  81. android:text="等于:" />
  82.  
  83. <TextView
  84. android:id="@+id/result"
  85. android:layout_width="wrap_content"
  86. android:layout_height="wrap_content"
  87. android:layout_weight="5"
  88. android:hint="计算结果"
  89. android:padding="15dp"
  90. android:textColor="#F44336"
  91. android:textSize="25sp"
  92. android:textStyle="bold" />
  93. </LinearLayout>
  94. </LinearLayout>

arrays.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string-array name="sign">
  4. <item>请选择运算符</item>
  5. <item>+</item>
  6. <item>-</item>
  7. <item>*</item>
  8. <item>/</item>
  9. </string-array>
  10. </resources>

MainActivity

  1. package com.jld.homework;
  2.  
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.AdapterView;
  6. import android.widget.Button;
  7. import android.widget.EditText;
  8. import android.widget.Spinner;
  9. import android.widget.TextView;
  10.  
  11. import androidx.appcompat.app.AppCompatActivity;
  12.  
  13. public class MainActivity extends AppCompatActivity {
  14. String op;
  15.  
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_main);
  20.  
  21. Spinner spinner = (Spinner) this.findViewById(R.id.operator);//获取活动布局中的Spinner对象
  22. //为Spinner注册内部监听器对象
  23. spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  24. @Override
  25. public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  26. //将Spinner选项的值赋值给成员变量op(保存算术运算符)
  27. op = ((TextView) view).getText().toString();
  28. }
  29. @Override
  30. public void onNothingSelected(AdapterView<?> parent) {
  31. }
  32. });
  33.  
  34. //各类组件
  35. Button calcButton = findViewById(R.id.calc);
  36. EditText firstNum = findViewById(R.id.firstNum);
  37. EditText secondNum = findViewById(R.id.secondNum);
  38. TextView result = findViewById(R.id.result);
  39.  
  40. //计算核心
  41. calcButton.setOnClickListener(v -> {
  42. switch (op) {
  43. case "+": {
  44. double r = Double.parseDouble(firstNum.getText().toString()) + Double.parseDouble(secondNum.getText().toString());
  45. result.setText(String.valueOf(r));
  46. break;
  47. }
  48. case "-": {
  49. double r = Double.parseDouble(firstNum.getText().toString()) - Double.parseDouble(secondNum.getText().toString());
  50. result.setText(String.valueOf(r));
  51. break;
  52. }
  53. case "*": {
  54. double r = Double.parseDouble(firstNum.getText().toString()) * Double.parseDouble(secondNum.getText().toString());
  55. result.setText(String.valueOf(r));
  56. break;
  57. }
  58. case "/": {
  59. double r = Double.parseDouble(firstNum.getText().toString()) / Double.parseDouble(secondNum.getText().toString());
  60. result.setText(String.valueOf(r));
  61. break;
  62. }
  63. default://非法情况报错
  64. result.setText(R.string.ERROR);
  65. break;
  66. }
  67. });
  68. }
  69. }

以上就是本文的全部内容,希望对大家的学习有所帮助