赞
踩
public class MainActivity extends Activity {
private EditText num1=null;
private EditText num2=null;
private TextView result=null;
//表示的是符号
private TextView tips=null;
private Button butAdd=null;
private Button butSub=null;
private Button butMul=null;
private Button butDiv=null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.num1=(EditText)super.findViewById(R.id.num1);
this.num2=(EditText)super.findViewById(R.id.num2);
this.result=(TextView)super.findViewById(R.id.result);
this.tips=(TextView)super.findViewById(R.id.tips);
this.butAdd=(Button)super.findViewById(R.id.add);
this.butSub=(Button)super.findViewById(R.id.sub);
this.butMul=(Button)super.findViewById(R.id.mul);
this.butDiv=(Button)super.findViewById(R.id.div);
//增加监听
this.butAdd.setOnClickListener(new AddListener());
this.butSub.setOnClickListener(new SubListener());
this.butMul.setOnClickListener(new MulListener());
this.butDiv.setOnClickListener(new DivListener());
this.num1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
MainActivity.this.num1.setText("");
}
});
this.num2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
MainActivity.this.num2.setText("");
}
});
}
//加法操作
public class AddListener implements OnClickListener{
int onenum=0;
int twonum=0;
public void onClick(View v) {
onenum=Integer.parseInt(MainActivity.this.num1.getText().toString());
twonum=Integer.parseInt(MainActivity.this.num2.getText().toString());
MainActivity.this.tips.setText("+");
MainActivity.this.result.setText((onenum+twonum)+"");
}
}
//减法操作
public class SubListener implements OnClickListener{
int onenum=0;
int twonum=0;
public void onClick(View v) {
onenum=Integer.parseInt(MainActivity.this.num1.getText().toString());
twonum=Integer.parseInt(MainActivity.this.num2.getText().toString());
MainActivity.this.tips.setText("-");
MainActivity.this.result.setText((onenum-twonum)+"");
}
}
//乘法操作
public class MulListener implements OnClickListener{
int onenum=0;
int twonum=0;
public void onClick(View v) {
onenum=Integer.parseInt(MainActivity.this.num1.getText().toString());
twonum=Integer.parseInt(MainActivity.this.num2.getText().toString());
MainActivity.this.tips.setText("*");
MainActivity.this.result.setText((onenum*twonum)+"");
}
}
//除法操作
public class DivListener implements OnClickListener{
int onenum=0;
int twonum=0;
public void onClick(View v) {
onenum=Integer.parseInt(MainActivity.this.num1.getText().toString());
twonum=Integer.parseInt(MainActivity.this.num2.getText().toString());
MainActivity.this.tips.setText("/");
MainActivity.this.result.setText((onenum/twonum)+"");
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。