第一个android小程序。
学了也有一段时间的android,自己写了一个计算器,超级简单的,就当练手熟练熟练。
看一下界面:


2个activity,第一个有2个editbox,4个radiobutton和一个button
第二个activity只是显示计算的结果,我这里设置成为了一个弹出的activity。在AndroidManifest.xml文件中声明第二个activity的时候加上这一句就实现了
android:theme="@android:style/Theme.Dialog"
在main.xml中对相应的控件进行配置,需要注意的是,4个radiobutton一定要放在一个radiogroup里面。
然后在第一个activity中创建控件,并通过findViewById来获取代表控件的对象。
给radiogroup添加监听器,判断哪个radiobutton按下。
symbolGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if (addBtn.getId() == checkedId) {
symbol = "add";
} else if (jianBtn.getId() == checkedId) {
symbol = "jian";
} else if (multiBtn.getId() == checkedId) {
symbol = "multi";
} else {
symbol = "div";
}
}
});
给button添加监听器,并绑定在button上。
class BtnListener implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String factorOne = one.getText().toString();
String factorTwo = two.getText().toString();
Intent intent = new Intent();
//通过Intent的putExtra方法来传值和操作符
intent.putExtra("one", factorOne);
intent.putExtra("two", factorTwo);
intent.putExtra("sym", symbol);
intent.setClass(ActivityTest.this, Result.class);
ActivityTest.this.startActivity(intent);
}
}
}
Intent it = getIntent();
factorOne = it.getStringExtra("one");
factorTwo = it.getStringExtra("two");
symbol = it.getStringExtra("sym");
int one = Integer.parseInt(factorOne);
int two = Integer.parseInt(factorTwo);
System.out.println(one);
System.out.println(two);
if (two == 0) {
flag = 1;
}
int result;
if (symbol.equals("add")) {
result = one + two;
} else if (symbol.equals("jian")) {
result = one - two;
} else if (symbol.equals("multi")) {
result = one * two;
} else {
if (flag == 1) {
result = 0;
} else {
result = one / two;
}
}
myTextView.setText(result + " ");
好了,这样程序就完成了。
附上源代码:
http://u.115.com/file/f96c6fc0c1
谁教教我怎么上传zip的附件呢?真不会。。。
浙公网安备 33010602011771号