从大二上快结束时接触android,此处留言一计算器代码:
这是活动的java文件,至于他的XML文件,大家可以自由发挥,只要按钮的id可以对应上,就可以
package com.example.administrator.test;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.TextView;
import java.lang.String;
import java.math.*;
public class MainActivity extends AppCompatActivity implements OnClickListener {
//按钮声明
TextView text1 = null;
TextView text2 =null;
Button button_0 = null;
Button button_1 = null;
Button button_2 = null;
Button button_3 = null;
Button button_4 = null;
Button button_5 = null;
Button button_6 = null;
Button button_7 = null;
Button button_8 = null;
Button button_9 = null;
Button button_ce = null;
Button button_c = null;
Button button_div = null;
Button button_mul = null;
Button button_sub = null;
Button button_add = null;
Button button_rel = null;
Button button_point = null;
Button button_equal = null;
int op = 0;//操作符
String str = "";
BigDecimal num = BigDecimal.ZERO,ans = BigDecimal.ZERO;
boolean is_op = false;
boolean is_num = false;
boolean rel = false;
boolean equal = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout);
//获取id
text1 = (TextView) findViewById(R.id.text1);
text2 = (TextView)findViewById(R.id.text2);
button_0 = (Button) findViewById(R.id.button_0);
button_1 = (Button) findViewById(R.id.button_1);
button_2 = (Button) findViewById(R.id.button_2);
button_3 = (Button) findViewById(R.id.button_3);
button_4 = (Button) findViewById(R.id.button_4);
button_5 = (Button) findViewById(R.id.button_5);
button_6 = (Button) findViewById(R.id.button_6);
button_7 = (Button) findViewById(R.id.button_7);
button_8 = (Button) findViewById(R.id.button_8);
button_9 = (Button) findViewById(R.id.button_9);
button_ce = (Button) findViewById(R.id.button_ce);
button_c = (Button) findViewById(R.id.button_c);
button_div = (Button) findViewById(R.id.button_div);
button_mul = (Button) findViewById(R.id.button_mul);
button_sub = (Button) findViewById(R.id.button_sub);
button_add = (Button) findViewById(R.id.button_add);
button_rel = (Button)findViewById(R.id.button_rel);
button_point = (Button) findViewById(R.id.button_point);
button_equal = (Button) findViewById(R.id.button_equal);
//添加监听
button_0.setOnClickListener(this);
button_1.setOnClickListener(this);
button_2.setOnClickListener(this);
button_3.setOnClickListener(this);
button_4.setOnClickListener(this);
button_5.setOnClickListener(this);
button_6.setOnClickListener(this);
button_7.setOnClickListener(this);
button_8.setOnClickListener(this);
button_9.setOnClickListener(this);
button_ce.setOnClickListener(this);
button_c.setOnClickListener(this);
button_div.setOnClickListener(this);
button_mul.setOnClickListener(this);
button_sub.setOnClickListener(this);
button_add.setOnClickListener(this);
button_point.setOnClickListener(this);
button_rel.setOnClickListener(this);
button_equal.setOnClickListener(this);
text1.setOnClickListener(this);
text2.setOnClickListener(this);
}
public void onClick(View v){
switch(v.getId()){
case R.id.button_0:
str += 0;
is_num = true;
is_op = false;
break;
case R.id.button_1:
str += "1";
is_num = true;
is_op = false;
break;
case R.id.button_2:
str += "2";
is_num = true;
is_op = false;
break;
case R.id.button_3:
str += "3";
is_num = true;
is_op = false;
break;
case R.id.button_4:
str += "4";
is_num = true;
is_op = false;
break;
case R.id.button_5:
str += "5";
is_num = true;
is_op = false;
break;
case R.id.button_6:
str += "6";
is_num = true;
is_op = false;
break;
case R.id.button_7:
str += "7";
is_num = true;
is_op = false;
break;
case R.id.button_8:
str += "8";
is_num = true;
is_op = false;
break;
case R.id.button_9:
str += "9";
is_num = true;
is_op = false;
break;
case R.id.button_point:
str += ".";
is_num = true;
is_op = false;
break;
case R.id.button_add:
if(op!=0)
return;
op = 1;
str += "+";
is_op = true;
is_num = false;
break;
case R.id.button_sub:
if(op!=0){
return;
}
op = 2;
str += "-";
is_op = true;
is_num = false;
break;
case R.id.button_mul:
if(op!=0){
return;
}
op = 3;
str += "*";
is_op = true;
is_num = false;
break;
case R.id.button_div:
if(op!=0){
return;
}
op = 4;
str += "/";
is_op = true;
is_num = false;
break;
case R.id.button_ce:
op = 0;
text1.setText("");
str = "";
ans = BigDecimal.ZERO;
num = BigDecimal.ZERO;
break;
case R.id.button_c:
str = "";
num = BigDecimal.ZERO;
break;
case R.id.button_rel:
rel = true;
break;
case R.id.button_equal:
if(num.equals(BigDecimal.ZERO)){
num = new BigDecimal(str);
}
switch(op) {
case 0:
return;
case 1:
ans = ans.add(num);
break;
case 2:
ans = ans.subtract(num);
break;
case 3:
ans = ans.multiply(num);
break;
case 4:
ans = ans.divide(num,3,4);
break;
default:
return;
}
op = 0;
is_num = false;
is_op = false;
equal = true;
num = BigDecimal.ZERO;
text1.setText("");
str = String.valueOf(ans);
break;
default:
break;
}
//首轮计算 等于号问题
if(equal&&is_num){
int len = str.length();
ans = BigDecimal.ZERO;
str = str.substring(len-1,len);
equal = false;
}
//第二次计算
if(is_op&&(!ans.equals(BigDecimal.ZERO))){
// str = ans.toString()+str;
text1.setText(str);
str = "";
equal = false;
}
//第一次遇到操作数
if(is_op&&(ans.equals(BigDecimal.ZERO))){
String temp;
text1.setText(str);
int len = str.length();
if(num.equals(BigDecimal.ZERO)) {
temp = str.substring(0, len - 1);
num = new BigDecimal(temp);
ans = num;
num = BigDecimal.ZERO;
}
str = "";
}
//正负号处理的部分
if(rel){
num = new BigDecimal(str);
if(op==0){
num = num.negate();
ans = num;
str = num.toString();
num = BigDecimal.ZERO;
}
else{
num = num.negate();
str = num.toString();
}
rel = false;
}
text2.setText(str);
}
}