(转载自我的另一个csdn号:weixin_38865554)
编译器为:Android studio 3.1.2;
SDK Lever 28,Revision 6;
java version 1.8.0_171;
界面是根据老师画的原型图来设计的,所以在界面设计上,我使用了6行4列的网格布局,但是这样布局有个问题是在不同机型和横屏显示时各控件不能自动调整比例,影响了整体的感官。
activity_main.xml
在进行逻辑设计时,我对以下问题进行了思考:
屏幕输出的规则。
第一种方案是把所有的输入都一股脑输出出来,当按下等于号时再刷新屏幕输出结果;
第二种方案是只输出数字,当点击运算符后再次输入数字时会覆盖掉输入运算符前显示在屏幕上的数字;
计算的规则。
第一种是把中缀表达式改成后缀表达式再计算,这种方法能够进行更复杂一些的运算。
第二种是分别把数和运算符分别记录起来,当再一次输入运算符的时候再对前面的数和运算符进行运算并输出。
此外就是对小数点个数的检查和对除数的检查和处理。
以下是MainActivity.java文件的内容:
MainActivity.java
package com.example.ex2_9;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.*;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private TextView txt;
private Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,btnA,btnS,btnM,btnP,btnE,btnD,btnClean;
String text="";
Double num1 = new Double(0.0),num2 = new Double(0.0),num = new Double(0.0);
final double zero=0;
/*state:输入框的状态,0为覆盖,1为追加*/
/*Calstate:存储上一个计算符的状态,0为无,1加2减3乘4除*/
int state = 0,Calstate = 0;
int point=0; //小数点个数
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView)findViewById(R.id.textView);
b0 = (Button)findViewById(R.id.btn_0);
b1 = (Button)findViewById(R.id.btn_1);
b2 = (Button)findViewById(R.id.btn_2);
b3 = (Button)findViewById(R.id.btn_3);
b4 = (Button)findViewById(R.id.btn_4);
b5 = (Button)findViewById(R.id.btn_5);
b6 = (Button)findViewById(R.id.btn_6);
b7 = (Button)findViewById(R.id.btn_7);
b8 = (Button)findViewById(R.id.btn_8);
b9 = (Button)findViewById(R.id.btn_9);
btnA = (Button)findViewById(R.id.btn_a);
btnS = (Button)findViewById(R.id.btn_s);
btnM = (Button)findViewById(R.id.btn_m);
btnP = (Button)findViewById(R.id.btn_p);
btnE = (Button)findViewById(R.id.btn_e);
btnD = (Button)findViewById(R.id.btn_d);
btnClean = (Button)findViewById(R.id.clean);
btnClean.setOnClickListener(new click());
b0.setOnClickListener(new click());
b1.setOnClickListener(new click());
b2.setOnClickListener(new click());
b3.setOnClickListener(new click());
b4.setOnClickListener(new click());
b5.setOnClickListener(new click());
b6.setOnClickListener(new click());
b7.setOnClickListener(new click());
b8.setOnClickListener(new click());
b9.setOnClickListener(new click());
btnA.setOnClickListener(new click());
btnS.setOnClickListener(new click());
btnM.setOnClickListener(new click());
btnP.setOnClickListener(new click());
btnE.setOnClickListener(new click());
btnD.setOnClickListener(new click());
}
/*--判断使用加减乘除那种方法并计算,最后返回布尔类型参数--*/
private boolean calculation(){
if (Calstate==1)/*加法运算*/{
num2 = Double.valueOf(text);
num1 = num1 + num2;
Calstate = 0;
return true;
}else if (Calstate==2)/*减法运算*/{
num2 = Double.valueOf(text);
num1 = num1 - num2;
Calstate = 0;
return true;
}else if (Calstate==3)/*乘法运算*/{
num2 = Double.valueOf(text);
num1 = num1 * num2;
Calstate = 0;
return true;
}else if (Calstate==4)/*除法运算*/{
num2 = Double.valueOf(text);
if (num2.doubleValue()==0){
text = "错误!";
state = 0;
Calstate = 0;
return false;
}else{
num1 = num1 / num2;
Calstate = 0;
return true;
}
}else {
state = 0;
Calstate = 0;
text = "Calstate参数错误!";
return false;
}
}
class click implements View.OnClickListener{
public void onClick(View v){
//0和小数点
if(v==b0){
if(state==0){
text = "0";
state = 0;
}else if (state==1){
text = text+"0";
}
txt.setText(text);
}
if(v==btnP){
if(state==0){
text = "0.";
state = 1;
}else if (state==1){
if (text.indexOf(".")==-1)
text = text+".";
}
txt.setText(text);
}
//数字键
if(v==b1){
if(state==0){
text = "1";
state = 1;
}else if (state==1){
text = text+"1";
}
txt.setText(text);
}
if(v==b2){
if(state==0){
text = "2";
state = 1;
}else if (state==1){
text = text+"2";
}
txt.setText(text);
}
if(v==b3){
if(state==0){
text = "3";
state = 1;
}else if (state==1){
text = text+"3";
}
txt.setText(text);
}
if(v==b4){
if(state==0){
text = "4";
state = 1;
}else if (state==1){
text = text+"4";
}
txt.setText(text);
}
if(v==b5){
if(state==0){
text = "5";
state = 1;
}else if (state==1){
text = text+"5";
}
txt.setText(text);
}
if(v==b6){
if(state==0){
text = "6";
state = 1;
}else if (state==1){
text = text+"6";
}
txt.setText(text);
}
if(v==b7){
if(state==0){
text = "7";
state = 1;
}else if (state==1){
text = text+"7";
}
txt.setText(text);
}
if(v==b8){
if(state==0){
text = "8";
state = 1;
}else if (state==1){
text = text+"8";
}
txt.setText(text);
}
if(v==b9) {
if(state==0){
text = "9";
state = 1;
}else if (state==1){
text = text+"9";
}
txt.setText(text);
}
/*-----------清除-------------*/
if(v==btnClean){
state = 0;
text = "0";
txt.setText(text);
}
/*----------------加减乘除-----------------*/
if(v==btnA){
if(Calstate==0){
num1 = Double.valueOf(text);
Calstate = 1;
text = num1.toString();
state = 0;
}else{
if(calculation()){
Calstate = 1;
text = num1.toString();
state = 0;
}
}
txt.setText(text);
}
if(v==btnS){
if(Calstate==0){
num1 = Double.valueOf(text);
Calstate = 2;
text = num1.toString();
state = 0;
}else{
if(calculation()){
Calstate = 2;
text = num1.toString();
state = 0;
}
}
txt.setText(text);
}
if(v==btnM){
if(Calstate==0){
num1 = Double.valueOf(text);
Calstate = 3;
text = num1.toString();
state = 0;
}else{
if(calculation()){
Calstate = 3;
text = num1.toString();
state = 0;
}
}
txt.setText(text);
}
if(v==btnD){
if(Calstate==0){
num1 = Double.valueOf(text);
Calstate = 4;
text = num1.toString();
state = 0;
}else{
if(calculation()){
Calstate = 4;
text = num1.toString();
state = 0;
}
}
txt.setText(text);
}
/*---------等于----------*/
if(v==btnE){
if(Calstate==0){
num = Double.valueOf(text);
text = num.toString();
num1 = zero;
num2 = zero;
Calstate = 0;
state = 0;
}else{
if(calculation()){
num = num1;
text = num.toString();
num1 = zero;
num2 = zero;
Calstate = 0;
state = 0;
}
}
txt.setText(text);
}
}
}
}