界面截图:
xml 代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:padding="0dp">
<GridLayout
android:id="@+id/GridLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:columnCount="4"
android:orientation="horizontal"
android:rowCount="6">
<EditText
android:id="@+id/result"
android:layout_width="231dp"
android:layout_height="176dp"
android:layout_columnSpan="4"
android:layout_gravity="fill"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="20dp"
android:background="#E8E8E8"
android:gravity="right|center"
android:hint=""
android:text=""
android:textColorHint="#000000"
android:focusable="false"
android:focusableInTouchMode="false"
android:textSize="40sp" />
<Button
android:id="@+id/cle"
android:layout_height="82dp"
android:text="C"
android:textSize="25sp" />
<Button
android:id="@+id/del"
android:layout_height="82dp"
android:text="DEL"
android:textSize="25sp" />
<Button
android:id="@+id/div"
android:layout_height="82dp"
android:text="/"
android:textSize="25sp" />
<Button
android:id="@+id/mul"
android:layout_height="82dp"
android:text="*"
android:textSize="25sp" />
<Button
android:id="@+id/seven"
android:layout_height="82dp"
android:text="7"
android:textSize="25sp" />
<Button
android:id="@+id/eight"
android:layout_height="82dp"
android:text="8"
android:textSize="25sp" />
<Button
android:id="@+id/nine"
android:layout_height="82dp"
android:text="9"
android:textSize="25sp" />
<Button
android:id="@+id/min"
android:layout_height="82dp"
android:text="-"
android:textSize="25sp" />
<Button
android:id="@+id/four"
android:layout_height="82dp"
android:text="4"
android:textSize="25sp" />
<Button
android:id="@+id/five"
android:layout_height="82dp"
android:text="5"
android:textSize="25sp" />
<Button
android:id="@+id/six"
android:layout_height="82dp"
android:text="6"
android:textSize="25sp" />
<Button
android:id="@+id/plus"
android:layout_height="82dp"
android:text="+"
android:textSize="25sp" />
<Button
android:id="@+id/one"
android:layout_height="82dp"
android:text="1"
android:textSize="25sp" />
<Button
android:id="@+id/two"
android:layout_height="82dp"
android:text="2"
android:textSize="25sp" />
<Button
android:id="@+id/three"
android:layout_width="wrap_content"
android:layout_height="82dp"
android:text="3"
android:textSize="25sp" />
<Button
android:id="@+id/equals"
android:layout_height="82dp"
android:layout_rowSpan="2"
android:layout_gravity="fill"
android:text="="
android:textSize="25sp"
app:backgroundTint="#FE7733" />
<Button
android:id="@+id/zero"
android:layout_height="82dp"
android:layout_columnSpan="2"
android:layout_gravity="fill"
android:text="0"
android:textSize="25sp" />
<Button
android:id="@+id/spot"
android:layout_height="82dp"
android:text="."
android:textSize="25sp" />
</GridLayout>
</LinearLayout>
themes.xml代码:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.MyApplication004" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
Java代码:
package com.example.myapplication004;
import static android.content.ContentValues.TAG;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
import java.util.regex.Pattern;
import java.math.BigDecimal;
public class MainActivity<v> extends AppCompatActivity implements View.OnClickListener {
//输入框
EditText result;
//定义数字按钮
Button zero, one, two, three, four, five, six, seven, eight, nine, spot;
//定义加减乘除按钮
Button plus, min, mul, div;
//等号按钮
Button equals;
//清除按钮
Button cle,del;
//运算符
String op="";
double num=0,res=0;
//第一个操作数
String s1 = "";
//第二个操作数
String s2 = "";
//小数点处理标志
int k=0;
//EditText显示内容
String display = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result = (EditText)findViewById(R.id.result);
zero = (Button )findViewById(R.id.zero);
one = (Button)findViewById(R.id.one);
two = (Button)findViewById(R.id.two);
three = (Button)findViewById(R.id.three);
four = (Button)findViewById(R.id.four);
five = (Button)findViewById(R.id.five);
six = (Button)findViewById(R.id.six);
seven = (Button)findViewById(R.id.seven);
eight = (Button)findViewById(R.id.eight);
nine = (Button)findViewById(R.id.nine);
spot = (Button)findViewById(R.id.spot);
zero.setOnClickListener(this);
one.setOnClickListener(this);
two.setOnClickListener(this);
three.setOnClickListener(this);
four.setOnClickListener(this);
five.setOnClickListener(this);
six.setOnClickListener(this);
seven.setOnClickListener(this);
eight.setOnClickListener(this);
nine.setOnClickListener(this);
spot.setOnClickListener(this);
plus = (Button)findViewById(R.id.plus);
min = (Button)findViewById(R.id.min);
mul = (Button)findViewById(R.id.mul);
div = (Button)findViewById(R.id.div);
plus.setOnClickListener(this);
min.setOnClickListener(this);
mul.setOnClickListener(this);
div.setOnClickListener(this);
equals = (Button)findViewById(R.id.equals);
equals.setOnClickListener(this);
cle = (Button)findViewById(R.id.cle);// C
cle.setOnClickListener(this);
del = (Button)findViewById(R.id.del);// DEL
del.setOnClickListener(this);
result.setCursorVisible(false);//输入框内隐藏光标
//限定最大输入字符数为12
// result.setFilters(new InputFilter[]{new InputFilter.LengthFilter(12)});
try {
//C清空按钮
cle.setOnClickListener((v)->{
res=0;
op="";
num=0;
s1="";
s2="";
display="";
result.setText("");
});
//DEL退格按钮
del.setOnClickListener((v)->{
String str=result.getText().toString();
if(str.equals(".") || str.equals("error") || str.equals("错误")){
return;
}
if(s1.length()!=0){
if(s2.length()==0){
s1 = s1.substring(0, s1.length() - 1);
Log.d(TAG,"s1="+s1);
if(s1.length()!=0)
res=Double.parseDouble(s1);
else
res=0;
result.setText(s1);
}else {
s2 = s2.substring(0, s2.length() - 1);
if(s2.length()!=0)
num=Double.parseDouble(s2);
else
num=0;
result.setText(s2);
}
}
});
}catch (Exception e){
}
}
public static boolean shuzi_1(String input){
String pattern = "^(\\-|\\+)?\\d+(\\.\\d+)?";//正数、负数、和小数 ?:0次或1次 +:1次或多次
return (Pattern.matches(pattern, input)) ? true : false;
}
private void Data(String input){
if(op.length()==0){
s1+=input;
if(s1.charAt(0)=='.'){
s1="0"+s1;
}
display=s1;
if (shuzi_1(s1)){
res=Double.parseDouble(s1);
}
}else{
s2+=input;
if(s2.charAt(0)=='.'){
s2="0"+s2;
}
display=s2;
if (shuzi_1(s2)){
num=Double.parseDouble(s2);
}
}
result.setText(display);
}
private void Point(String input){
if(input.equals(".")){
if(s1.contains(".") && s2.length()==0){
k=1;
}
if(s2.contains(".") && s1.length()!=0){
k=1;
}
}else {
k=0;
}
}
@Override
public void onClick(View v) {
// 根据当前按钮按下的id进行判断
switch (v.getId()) {
case R.id.zero:
if(s1.equals("0") || s2.equals("0")){
return;
}
case R.id.one:
case R.id.two:
case R.id.three:
case R.id.four:
case R.id.five:
case R.id.six:
case R.id.seven:
case R.id.eight:
case R.id.nine:
case R.id.spot:
//将一个View对象v强制转换为Button类型,并获取该按钮的文本内容
String input=((Button) v).getText().toString();
Log.d(TAG,"input="+input);
//小数点处理函数
Point(input);
if(k==0){
Data(input);
}
Log.d(TAG,"s1="+s1);
Log.d(TAG,"s2="+s2);
break;
case R.id.plus:
case R.id.min:
case R.id.mul:
case R.id.div:
if(s1.length()!=0 && s2.length()!=0 && op.length()!=0){
getResult();
}
if((op.contains("+")|| op.contains("-")|| op.contains("*")|| op.contains("/")) && s2.length()==0 && s1.length()==0){
result.setText("0");
Reset();
return;
}
op=((Button) v).getText().toString();
Log.d(TAG,"op="+op);
if ((op.contains("+") || op.contains("-") || op.contains("*") || op.contains("/")) &&s2.length()!=0 && s1.length()!=0){
if (!s1.contains(".") && !s2.contains(".") && !op.equals("/")) {//加减乘 整数情况
s1=String.valueOf((int) res);
}
display="";
//第2个操作数清空
s2="";
num=0;
}
break;
case R.id.equals:
if(s1.length()!=0 || s2.length()!=0 ){
getResult();
Reset();
}
if (!s1.contains(".") && !s2.contains(".") && !op.equals("/")) {//加减乘 整数情况,结果取整
s1=String.valueOf((int) res);
}else {
s1=String.valueOf(res);
}
display="";
s2="";
num=0;
if(s1.length()!=0 && s2.length()==0 && op.length()==0){
s1="";
}
Log.d(TAG,"点击等号后结果res="+res);
break;
}
}
//重置
private void Reset(){
s1="";
op="";
s2="";
}
private void Mul(){
BigDecimal str1 = new BigDecimal(Double.toString(res));
BigDecimal str2 = new BigDecimal(Double.toString(num));
if(String.valueOf(res).length()>=13){ // 精度溢出需要四舍五入
res=str1.multiply(str2).setScale(0,BigDecimal.ROUND_HALF_UP).doubleValue();// 1/3*3=1
}else{
res =str1.multiply(str2).doubleValue();// 6.6*6.6=43.56
}
}
//点了等号
private void getResult() {
Log.d(TAG,"计算后的res1="+res);
Log.d(TAG,"计算后的num1="+num);
Log.d(TAG,"计算后的op1="+op);
String exp = result.getText().toString();
if(exp.equals(".") || exp.equals("error") || exp.equals("错误") ){
return;
}
// 创建一个动态数组
ArrayList<String> list = new ArrayList<>();
// 往动态数组中添加元素
list.add(String.valueOf(res));
list.add(op);
list.add(String.valueOf(num));
try {
res = Double.parseDouble(list.get(0));
} catch (Exception e) {
result.setText(String.valueOf(res));
}
try {
op = list.get(1);
num = Double.parseDouble(list.get(2));
BigDecimal str1 = new BigDecimal(Double.toString(res));
BigDecimal str2 = new BigDecimal(Double.toString(num));
if (op.equals("+")) {
res =str1.add(str2).doubleValue();
} else if (op.equals("-")) {
res =str1.subtract(str2).doubleValue();
} else if (op.equals("*")) {
Mul();
} else if (op.equals("/")) {
if (num == 0) {
result.setText("error");
Reset();//error后重置操作数和操作符
return;
}else{
res =str1.divide(str2,13, BigDecimal.ROUND_HALF_UP).doubleValue();
//scale:保留小数位13位
//BigDecimal.ROUND_HALF_UP:四舍五入
//.doubleValue(): 取double真实值
}
Log.d(TAG,"计算后的s1="+s1);
Log.d(TAG,"计算后的s2="+s2);
}
// result.setText(String.valueOf(res));
//判断整数和小数
if (!s1.contains(".") && !s2.contains(".") && !op.equals("/")) {//加减乘 整数情况
result.setText(String.valueOf((int) res));
}else
if (Double.parseDouble(list.get(0)) % num == 0 && op.equals("/")) {//整除无小数点
result.setText(String.valueOf((int) res));
}else {
result.setText(String.valueOf(res));
}
}catch(Exception e){
result.setText("错误");
return;
}
}
}