activity_main.xml 布局文件里的代码为:
package com.example.administrator.calculator;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
StringBuffer sbff = new StringBuffer();
int a=0,b=0,c=0,d=0;
private Button bt1;
private Button bt2;
private Button bt3;
private Button bt4;
private Button bt5;
private Button bt6;
private Button bt7;
private Button bt8;
private Button bt9;
private Button bt10;
private Button bt11;
private Button bt12;
private Button bt13;
private Button bt14;
private Button bt15;
private Button bt16;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化
editText = (EditText) findViewById(R.id.abc);
bt1 = (Button) findViewById(R.id.one);
bt2 = (Button) findViewById(R.id.two);
bt3 = (Button) findViewById(R.id.three);
bt4 = (Button) findViewById(R.id.four);
bt5 = (Button) findViewById(R.id.five);
bt6 = (Button) findViewById(R.id.six);
bt7 = (Button) findViewById(R.id.seven);
bt8 = (Button) findViewById(R.id.eight);
bt9 = (Button) findViewById(R.id.nine);
bt10 = (Button) findViewById(R.id.zero);
bt11 = (Button) findViewById(R.id.jia);
bt12 = (Button) findViewById(R.id.jian);
bt13 = (Button) findViewById(R.id.cheng);
bt14 = (Button) findViewById(R.id.chu);
bt15 = (Button) findViewById(R.id.dengyu);
bt16 = (Button) findViewById(R.id.c);
//监听
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
bt3.setOnClickListener(this);
bt4.setOnClickListener(this);
bt5.setOnClickListener(this);
bt6.setOnClickListener(this);
bt7.setOnClickListener(this);
bt8.setOnClickListener(this);
bt9.setOnClickListener(this);
bt10.setOnClickListener(this);
bt11.setOnClickListener(this);
bt12.setOnClickListener(this);
bt13.setOnClickListener(this);
bt14.setOnClickListener(this);
bt15.setOnClickListener(this);
bt16.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Button bt = (Button) v;
switch (v.getId()) {
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.zero:
case R.id.jia:
case R.id.jian:
case R.id.cheng:
case R.id.chu:
sbff.append(bt.getText());
editText.setText(sbff.toString());
break;
case R.id.dengyu:
String s = sbff.toString();
a = s.indexOf("+");
b = s.indexOf("-");
c = s.indexOf("*");
d = s.indexOf("/");
if(!(a==-1)){
String m[] = s.split("\\+");
String[] r =m[0].split("\\.");
if(r[0]==null){
a = Integer.valueOf(m[0]);
b = Integer.valueOf(m[1]);
c = a+b;
s = c+"";
editText.setText(s);
sbff = new StringBuffer();
sbff.append(s);
}else {
double a = Double.valueOf(m[0]);
b = Integer.valueOf(m[1]);
double c = a+b;
s = c+"";
editText.setText(s);
sbff = new StringBuffer();
sbff.append(s);
}
}else if(!(b==-1)){
String m[] = s.split("-");
String[] r =m[0].split("\\.");
if(r[0]==null){
a = Integer.valueOf(m[0]);
b = Integer.valueOf(m[1]);
c = a-b;
s = c+"";
editText.setText(s);
sbff = new StringBuffer();
sbff.append(s);
}else {
double a = Double.valueOf(m[0]);
b = Integer.valueOf(m[1]);
double c = a-b;
s = c+"";
editText.setText(s);
sbff = new StringBuffer();
sbff.append(s);
}
}else if(!(c==-1)){
String m[] = s.split("\\*");
String[] r =m[0].split("\\.");
if(r[0]==null){
a = Integer.valueOf(m[0]);
b = Integer.valueOf(m[1]);
c = a*b;
s = c+"";
editText.setText(s);
sbff = new StringBuffer();
sbff.append(s);
}else {
double a = Double.valueOf(m[0]);
b = Integer.valueOf(m[1]);
double c = a*b;
s = c+"";
editText.setText(s);
sbff = new StringBuffer();
sbff.append(s);
}
}else if(!(d==-1)){
String m[] = s.split("/");
String[] r =m[0].split("\\.");
if(r[0]==null){
a = Integer.valueOf(m[0]);
b = Integer.valueOf(m[1]);
c = a/b;
s = c+"";
editText.setText(s);
sbff = new StringBuffer();
sbff.append(s);
}else {
double a = Double.valueOf(m[0]);
b = Integer.valueOf(m[1]);
double c = a/b;
s = c+"";
editText.setText(s);
sbff = new StringBuffer();
sbff.append(s);
}
}
// editText.setText(null);
break;
case R.id.c:
String s1 = sbff.toString();
a = s1.length();
if(a==1){
sbff = new StringBuffer();
editText.setText("sb不能再删了");
}else if(a==0) {
}else{
s1 = s1.substring(0,a-1);
sbff = new StringBuffer();
sbff.append(s1);
editText.setText(sbff);
}
break;
}
}
}