Android开发之简单计算器
运行截图
点击"相加"按钮
ViewActivity.java
package com.archie.activity03;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class ViewActivity extends Activity {
private TextView textView;
private EditText editText1;
private EditText editText2;
private Button btn1;
private Button btn2;
private Button btn3;
private Button btn4;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//编辑框
editText1=(EditText) findViewById(R.id.numberOne);
editText2=(EditText) findViewById(R.id.numberTwo);
//文本
textView=(TextView) findViewById(R.id.textView);
textView.setText("与");
//按钮
btn1=(Button) findViewById(R.id.btn1);
btn1.setText("相加");
//绑定监听器对象
btn1.setOnClickListener(new Btn1CalListener());
btn2=(Button) findViewById(R.id.btn2);
btn2.setText("相减");
btn2.setOnClickListener(new Btn2CalListener());
btn3=(Button) findViewById(R.id.btn3);
btn3.setText("相乘");
btn3.setOnClickListener(new Btn3CalListener());
btn4=(Button) findViewById(R.id.btn4);
btn4.setText("相除");
btn4.setOnClickListener(new Btn3CalListener());
}
//加法
class Btn1CalListener implements OnClickListener{
@Override
public void onClick(View v) {
//获取两个EditText的值
String numberOne=editText1.getText().toString();
String numberTwo=editText2.getText().toString();
//将获取的值存放到Intent对象当中,以便传递
Intent intent=new Intent();
intent.putExtra("one", numberOne);
intent.putExtra("two", numberTwo);
//运算符
intent.putExtra("calculate", "+");
intent.setClass(ViewActivity.this, ResultActivity.class);
//启动ResultActivity
ViewActivity.this.startActivity(intent);
}
}
//减法
class Btn2CalListener implements OnClickListener{
@Override
public void onClick(View v) {
//获取两个EditText的值
String numberOne=editText1.getText().toString();
String numberTwo=editText2.getText().toString();
//将获取的值存放到Intent对象当中,以便传递
Intent intent=new Intent();
intent.putExtra("one", numberOne);
intent.putExtra("two", numberTwo);
//运算符
intent.putExtra("calculate", "-");
intent.setClass(ViewActivity.this, ResultActivity.class);
//启动ResultActivity
ViewActivity.this.startActivity(intent);
}
}
//乘法
class Btn3CalListener implements OnClickListener{
@Override
public void onClick(View v) {
//获取两个EditText的值
String numberOne=editText1.getText().toString();
String numberTwo=editText2.getText().toString();
//将获取的值存放到Intent对象当中,以便传递
Intent intent=new Intent();
intent.putExtra("one", numberOne);
intent.putExtra("two", numberTwo);
//运算符
intent.putExtra("calculate", "*");
intent.setClass(ViewActivity.this, ResultActivity.class);
//启动ResultActivity
ViewActivity.this.startActivity(intent);
}
}
//除法
class Btn4CalListener implements OnClickListener{
@Override
public void onClick(View v) {
//获取两个EditText的值
String numberOne=editText1.getText().toString();
String numberTwo=editText2.getText().toString();
//将获取的值存放到Intent对象当中,以便传递
Intent intent=new Intent();
intent.putExtra("one", numberOne);
intent.putExtra("two", numberTwo);
//运算符
intent.putExtra("calculate", "/");
intent.setClass(ViewActivity.this, ResultActivity.class);
//启动ResultActivity
ViewActivity.this.startActivity(intent);
}
}
}
ResultActivity.java
package com.archie.activity03;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class ResultActivity extends Activity{
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
textView=(TextView) findViewById(R.id.result);
//得到Intent对象
Intent intent=getIntent();
int one=Integer.parseInt(intent.getStringExtra("one"));
int two=Integer.parseInt(intent.getStringExtra("two"));
String calculate=intent.getStringExtra("calculate");
int result=0;
//加法
if(calculate.equals("+")){
result=one+two;
textView.setText(result+"");
}
//减法
else if(calculate.equals("-")){
result=one-two;
textView.setText(result+"");
}
//乘法
else if(calculate.equals("*")){
result=one*two;
textView.setText(result+"");
}
//除法
else if(calculate.equals("/")){
try {
result=one/two;
textView.setText(result+"");
} catch (Exception e) {
textView.setText("除数不能为零");
e.printStackTrace();
}
}
}
}
by archie
posted on 2011-05-12 23:04 archie2010 阅读(2538) 评论(0) 编辑 收藏 举报