具体源代码
MainActivity.java
package com.example.calculator; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ Button btn_clean,btn_del,btn_divide,btn_0,btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9, btn_multiply,btn_add,btn_minus,btn_point,btn_equal; TextView textView; boolean clear_flag; //清空标识 protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); btn_0 = findViewById(R.id.btn_0); //初始化 btn_1 = findViewById(R.id.btn_1); btn_2 = findViewById(R.id.btn_2); btn_3 = findViewById(R.id.btn_3); btn_4 = findViewById(R.id.btn_4); btn_5 = findViewById(R.id.btn_5); btn_6 = findViewById(R.id.btn_6); btn_7 = findViewById(R.id.btn_7); btn_8 = findViewById(R.id.btn_8); btn_9 = findViewById(R.id.btn_9); btn_multiply = findViewById(R.id.btn_multiply); btn_divide = findViewById(R.id.btn_divide); btn_add = findViewById(R.id.btn_add); btn_minus = findViewById(R.id.btn_minus); btn_point = findViewById(R.id.btn_point); btn_del =findViewById(R.id.btn_del); btn_equal = findViewById(R.id.btn_equal); btn_clean = findViewById(R.id.btn_clean); textView = findViewById(R.id.textView); //设置按钮的点击事件 btn_0.setOnClickListener(this); btn_1.setOnClickListener(this); btn_2.setOnClickListener(this); btn_3.setOnClickListener(this); btn_4.setOnClickListener(this); btn_5.setOnClickListener(this); btn_6.setOnClickListener(this); btn_7.setOnClickListener(this); btn_8.setOnClickListener(this); btn_9.setOnClickListener(this); btn_minus.setOnClickListener(this); btn_multiply.setOnClickListener(this); btn_del.setOnClickListener(this); btn_divide.setOnClickListener(this); btn_point.setOnClickListener(this); btn_add.setOnClickListener(this); btn_equal.setOnClickListener(this); btn_clean.setOnClickListener(this); } public void onClick(View v) { String str = textView.getText().toString(); switch(v.getId ()){ case R.id.btn_0: case R.id.btn_1: case R.id.btn_2: case R.id.btn_3: case R.id.btn_4: case R.id.btn_5: case R.id.btn_6: case R.id.btn_7: case R.id.btn_8: case R.id.btn_9: case R.id.btn_point: if(clear_flag){ clear_flag=false; str=""; textView.setText (""); } textView.setText(str+((Button)v).getText ()); break; case R.id.btn_add: case R.id.btn_minus: case R.id.btn_multiply: case R.id.btn_divide: if(clear_flag){ clear_flag=false; str = ""; textView.setText(""); } textView.setText(str+" "+((Button)v).getText()+" "); break; case R.id.btn_del: if(clear_flag){ clear_flag=false; textView.setText (""); }else if (str != null && !str.equals ("")){ textView.setText(str.substring(0,str.length()-1)); //删除一个字符 } break; case R.id.btn_clean: clear_flag=false; str = ""; textView.setText(""); //清空文本内容 break; case R.id.btn_equal: getResult(); //获取结果 break; default: break; } } //算法 private void getResult() { String xiao = textView.getText().toString(); if (xiao == null || xiao.equals("")) { return; } if (!xiao.contains("")) { return; } if (clear_flag) { clear_flag = false; return; } clear_flag = true; String str1 = xiao.substring(0, xiao.indexOf(" ")); // 获取到运算符前面的字符 String str_y = xiao.substring(xiao.indexOf(" ") + 1, xiao.indexOf(" ") + 2); //获取到运算符 String str2 = xiao.substring(xiao.indexOf(" ") + 3); //获取到运算符后面的字符 double result = 0; if (!str1.equals("") && !str2.equals("")) { double num1 = Double.parseDouble(str1); //将str1、str2强制转化为double类型 double num2 = Double.parseDouble(str2); if (str_y.equals("+")) { result = num1 + num2; } else if (str_y.equals("-")) { result = num1 - num2; } else if (str_y.equals("/")) { if (num2 == 0) { result = 0; } else { result = num1 / num2; } } else if (str_y.equals("*")) { result = num1 * num2; } if (!str1.contains(".") && !str2.contains(".") && !xiao.equals("÷")) { int k = (int) result; //强制转换 textView.setText(k + ""); } else { textView.setText(result + ""); } } else if (!str1.equals("") && str2.equals("")) { textView.setText(xiao); } else if (str1.equals("") && !str2.equals("")) { double num2 = Double.parseDouble(str2); if (xiao.equals("+")) { result = 0 + num2; } else if (xiao.equals("-")) { result = 0 - num2; } else if (xiao.equals("×")) { result = 0; } else if (xiao.equals("/")) { result = 0; } if (!str2.contains(".")) { int r = (int) result; textView.setText(r + ""); } else { textView.setText(result + ""); } } else { textView.setText(""); } } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#D8ECF3"> <TextView android:gravity="bottom|right" android:textSize="60dp" android:singleLine="true" android:layout_margin="15dp" android:layout_width="match_parent" android:layout_height="150dp" android:background="@drawable/white" android:id="@+id/textView"/> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp"> <Button android:id="@+id/btn_clean" android:layout_marginLeft="10dp" android:background="@drawable/orange" android:gravity="center" android:text="C" android:textSize="25sp" /> <Button android:id="@+id/btn_del" android:layout_marginLeft="10dp" android:layout_span="2" android:background="@drawable/gray" android:gravity="center" android:text="Del" android:textSize="25sp" /> <Button android:id="@+id/btn_divide" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:background="@drawable/gray" android:gravity="center" android:layout_span="1" android:text="/" android:textSize="25sp" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp"> <Button android:id="@+id/btn_7" android:layout_marginLeft="10dp" android:background="@drawable/white" android:gravity="center" android:text="7" android:textSize="25sp" /> <Button android:id="@+id/btn_8" android:layout_marginLeft="10dp" android:background="@drawable/white" android:gravity="center" android:text="8" android:textSize="25sp" /> <Button android:id="@+id/btn_9" android:layout_marginLeft="10dp" android:background="@drawable/white" android:gravity="center" android:text="9" android:textSize="25sp" /> <Button android:id="@+id/btn_multiply" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:background="@drawable/gray" android:gravity="center" android:text="*" android:textSize="25sp" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp"> <Button android:id="@+id/btn_4" android:layout_marginLeft="10dp" android:background="@drawable/white" android:gravity="center" android:text="4" android:textSize="25sp" /> <Button android:id="@+id/btn_5" android:layout_marginLeft="10dp" android:background="@drawable/white" android:gravity="center" android:text="5" android:textSize="25sp" /> <Button android:id="@+id/btn_6" android:layout_marginLeft="10dp" android:background="@drawable/white" android:gravity="center" android:text="6" android:textSize="25sp" /> <Button android:id="@+id/btn_add" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:background="@drawable/gray" android:gravity="center" android:text="+" android:textSize="25sp" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp"> <Button android:id="@+id/btn_1" android:layout_marginLeft="10dp" android:background="@drawable/white" android:gravity="center" android:text="1" android:textSize="25sp" /> <Button android:id="@+id/btn_2" android:layout_marginLeft="10dp" android:background="@drawable/white" android:gravity="center" android:text="2" android:textSize="25sp" /> <Button android:id="@+id/btn_3" android:layout_marginLeft="10dp" android:background="@drawable/white" android:gravity="center" android:text="3" android:textSize="25sp" /> <Button android:id="@+id/btn_minus" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:background="@drawable/gray" android:gravity="center" android:text="-" android:textSize="25sp" /> </TableRow> <TableRow android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginTop="10dp"> <Button android:id="@+id/btn_0" android:layout_marginLeft="10dp" android:layout_span="2" android:background="@drawable/white" android:gravity="center" android:text="0" android:textSize="25sp" /> <Button android:id="@+id/btn_point" android:layout_marginLeft="10dp" android:layout_span="1" android:background="@drawable/white" android:gravity="center" android:text="." android:textSize="25sp" /> <Button android:id="@+id/btn_equal" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_span="1" android:background="@drawable/gray" android:gravity="center" android:text="=" android:textSize="25sp" /> </TableRow> </TableLayout>
drawable中建change.xml gray.xml orange.xml white.xml 四个
change.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/gray"/> //默认颜色 <item android:drawable="@drawable/orange" android:state_pressed="true"/> </selector>
gray.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="5dp"/> <solid android:color="#f9f9f9"/> <stroke android:width="2dp" android:color="#ffa600"/> </shape>
orange.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="5dp"/> // 圆角 <solid android:color="#F7B684"/> //颜色 </shape>
white.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="5dp"/> <solid android:color="#ffffff"/> <stroke android:width="1dp" android:color="#ffa600"/> </shape>
若是在poject下运行不了,可以试一下在android下运行。
小白一个,算法写得不太好,请见谅!