App下載

Android簡(jiǎn)易計(jì)算器實(shí)現(xiàn)功能附源碼

猿友 2021-06-23 14:35:54 瀏覽數(shù) (5000)
反饋

在Android中實(shí)現(xiàn)計(jì)算功能是比較基礎(chǔ)的,很多剛?cè)腴T的Android開發(fā)人員都可以自主動(dòng)手嘗試一下。

效果如圖:


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="操作數(shù):"
  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="請(qǐng)輸入數(shù)值操作數(shù)"
  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="算術(shù)運(yùn)算:"
  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="操作數(shù):"
  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="請(qǐng)輸入數(shù)值操作數(shù)"
  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="計(jì)算結(jié)果"
  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>請(qǐng)選擇運(yùn)算符</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);//獲取活動(dòng)布局中的Spinner對(duì)象
  22. //為Spinner注冊(cè)內(nèi)部監(jiān)聽器對(duì)象
  23. spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  24. @Override
  25. public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  26. //將Spinner選項(xiàng)的值賦值給成員變量op(保存算術(shù)運(yùn)算符)
  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. //計(jì)算核心
  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://非法情況報(bào)錯(cuò)
  64. result.setText(R.string.ERROR);
  65. break;
  66. }
  67. });
  68. }
  69. }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助


0 人點(diǎn)贊