package com.hxx.jisuanqi;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener {
Button yi, er, san, si, wu, liu, qi, ba, jiu, ling;// 定义属性数字0--9按钮
Button c, del;// 定义属性 归零,清除。
Button jia, jian, cheng, chu, deyu;// 定义属性 “加、减、乘、除”。
Button dian;// 定义属性 点。
EditText et1;// 定义属性 文本输入框
boolean clear_flag;//清空标识
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ling = (Button) findViewById(R.id.ling);
yi = (Button) findViewById(R.id.yi);
er = (Button) findViewById(R.id.er);
san = (Button) findViewById(R.id.san);
si = (Button) findViewById(R.id.si);
wu = (Button) findViewById(R.id.wu);
liu = (Button) findViewById(R.id.liu);
qi = (Button) findViewById(R.id.qi);
ba = (Button) findViewById(R.id.ba);
jiu = (Button) findViewById(R.id.jiu);
dian = (Button) findViewById(R.id.dian);
jia = (Button) findViewById(R.id.jia);
jian = (Button) findViewById(R.id.jian);
cheng = (Button) findViewById(R.id.cheng);
chu = (Button) findViewById(R.id.chu);
deyu = (Button) findViewById(R.id.deyu);
c = (Button) findViewById(R.id.c);
del = (Button) findViewById(R.id.del);
et1 = (EditText) findViewById(R.id.et1);
ling.setOnClickListener(this);
yi.setOnClickListener(this);
er.setOnClickListener(this);
san.setOnClickListener(this);
si.setOnClickListener(this);
wu.setOnClickListener(this);
liu.setOnClickListener(this);
qi.setOnClickListener(this);
ba.setOnClickListener(this);
jiu.setOnClickListener(this);
dian.setOnClickListener(this);
jia.setOnClickListener(this);
jian.setOnClickListener(this);
cheng.setOnClickListener(this);
chu.setOnClickListener(this);
c.setOnClickListener(this);
del.setOnClickListener(this);
et1.setOnClickListener(this);
deyu.setOnClickListener(this);
// 以上设置点击事件
}
@Override
public void onClick(View v) {
String str=et1.getText().toString();
switch (v.getId()) {
case R.id.ling:
case R.id.yi:
case R.id.er:
case R.id.san:
case R.id.si:
case R.id.wu:
case R.id.liu:
case R.id.qi:
case R.id.ba:
case R.id.jiu:
case R.id.dian:
if(clear_flag){
clear_flag=false;
et1.setText("");
}
et1.setText(str+((Button)v).getText());
break;
case R.id.jia:
case R.id.jian:
case R.id.cheng:
case R.id.chu:
if(clear_flag){
clear_flag=false;
et1.setText("");
}
et1.setText(str+""+((Button)v).getText()+"");
break;
case R.id.del:
if(clear_flag){
clear_flag=false;
et1.setText("");
}
else if(str!=null&&!str.equals("")){
et1.setText(str.substring(0, str.length()-1));
}
break;
case R.id.c:
clear_flag=false;
et1.setText("");
break;
case R.id.deyu:
getResult();
break;
}
}
//运算结果
private void getResult(){
String exp=et1.getText().toString();
if(exp==null||exp.equals("")){
return;
}if(exp.contains(" ")){
return;
}
if(clear_flag){
clear_flag=false;
return;
}
clear_flag=true;
double result=0;
String s1=exp.substring(0,exp.indexOf(" "));//运算符前面的字符
String op=exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")+2);
String s2=exp.substring(exp.indexOf(" ")+3);
if(!s1.equals("")&&!s2.equals("")){
Double d1=Double.parseDouble(s1);
Double d2=Double.parseDouble(s2);
if(op.equals("+")){
result=d1+d2;
}else if(op.equals("-")){
result=d1-d2;
}else if(op.equals("×")){
result=d1*d2;
}else if(op.equals("÷")){
if(d2==0){
result = 0;
}else{
result=d1/d2;
}
}
if(!s1.contains(".")&&!s2.contains(".")){
int r=(int) result;
et1.setText(r+"");
}else{
et1.setText(result+"");
}
}
if(!s1.equals("")&&s2.equals("")){
et1.setText(exp);
}
if(s1.equals("")&&!s2.equals("")){
Double d1=Double.parseDouble(s1);
Double d2=Double.parseDouble(s2);
if(op.equals("+")){
result=0+d2;
}else if(op.equals("-")){
result=0-d2;
}else if(op.equals("×")){
result=0;
}else if(op.equals("÷")){
result=0;
}
if(!s2.contains(".")){
int r=(int) result;
et1.setText(r+"");
}else{
et1.setText(result+"");
}
}else{
et1.setText(exp);
}
}
}
yyww
yuicer
相关分类