设计一款带有可视化界面的简单计算器,供用户输入数据并查看结果。用户通过点击相应按钮(加减乘除运算符、等号、数字)输入正确的表达式,计算器进行相应的加减乘除运算,且可以进行小数和整数的运算;长按清除按钮3秒,可以清除已录入的内容。
package com.example.a1108;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.Date;
public class MainActivity 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;
// 标识符,标识运算完成
Boolean clr_flag=false;
// 清除按钮
Button cls;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result=(EditText) findViewById(R.id.result);
zero=findViewById(R.id.zero);
one=findViewById(R.id.one);
two=findViewById(R.id.two);
three=findViewById(R.id.three);
four=findViewById(R.id.four);
five=findViewById(R.id.five);
six=findViewById(R.id.six);
seven=findViewById(R.id.seven);
eight=findViewById(R.id.eight);
nine=findViewById(R.id.nine);
spot=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=findViewById(R.id.plus);
min=findViewById(R.id.min);
mul=findViewById(R.id.mul);
div=findViewById(R.id.div);
plus.setOnClickListener(this);
min.setOnClickListener(this);
mul.setOnClickListener(this);
div.setOnClickListener(this);
equals=findViewById(R.id.equal);
equals.setOnClickListener(this);
cls=findViewById(R.id.cls);
// 为清除设置事件
cls.setOnTouchListener(new View.OnTouchListener() {
Date curDate=new Date(System.currentTimeMillis());
Date endDate=new Date(System.currentTimeMillis());
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()){
// 按下
case MotionEvent.ACTION_DOWN:
curDate=new Date((System.currentTimeMillis()));
break;
// 抬起
case MotionEvent.ACTION_UP:
endDate=new Date(System.currentTimeMillis());
long durationMS=endDate.getTime()-curDate.getTime();
if(durationMS>3000)
result.setText("");
break;
}
return true;
}
});
}
@Override
public void onClick(View view) {
// getText()获取的内容是一个对象,所以要转换一下
String str=result.getText().toString();
// 根据当前按钮按下的id进行判断
switch (view.getId())
{
case R.id.zero:
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:
// 如果标识符为真,让值为空
if(clr_flag)
str="";
// 把现在的内容追加上,现在的内容来自于按钮的文本
// 按钮这个view对象先转换为Button
result.setText(str+((Button)view).getText());
clr_flag=false;
break;
case R.id.plus:
case R.id.min:
case R.id.mul:
case R.id.div:
// 如果标识符为真,让值为空
if(clr_flag)
str="";
if(str.contains("+")||str.contains("-")||str.contains("×")||str.contains("÷"))
// 从起始位置开始,我们只要运算符之前的内容
str=str.substring(0,str.indexOf(" "));
// 所以在运算符的前面和后面都追加一个“ ”
result.setText(str+" "+((Button)view).getText()+" ");
clr_flag=false;
break;
case R.id.equal:
getResult();
break;
}
}
// 点了等号后
private void getResult(){
clr_flag=true;
// 获取到字符串
String exp=result.getText().toString();
// 按照空格分隔字符串,形成字符串数组,第一个元素是左侧操作数,第二个元素是运算符,第三个元素是右侧操作数
String [] exp_arr=exp.split(" ");
// 定义结果
double cnt=0;
// 定义操作数
double d1=Double.parseDouble(exp_arr[0]);
double d2=Double.parseDouble(exp_arr[2]);
// 判断运算符
if(exp_arr[1].equals("+"))
cnt=d1+d2;
else if(exp_arr[1].equals("-"))
cnt=d1-d2;
else if(exp_arr[1].equals("×"))
cnt=d1*d2;
else if(exp_arr[1].equals("÷"))
cnt=d1/d2;
// 设置结果
result.setText(String.valueOf(cnt));
}
}
注释都写在里面了