Android 中利用反射技术实现加减乘除
2010-08-17 11:00:00 来源:WEB开发网int first, second;
String operaionFun = "";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findview();
add.setOnClickListener(click);
cut.setOnClickListener(click);
ride.setOnClickListener(click);
Except.setOnClickListener(click);
sum.setOnClickListener(click);
}
void findview() {
one = (EditText) findViewById(R.id.EditText01);
two = (EditText) findViewById(R.id.EditText02);
result = (TextView) findViewById(R.id.TextView01);
add = (Button) findViewById(R.id.Button01);
cut = (Button) findViewById(R.id.Button02);
ride = (Button) findViewById(R.id.Button03);
Except = (Button) findViewById(R.id.Button04);
sum = (Button) findViewById(R.id.Button05);
}
OnClickListener click = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
first = Integer.parseInt(one.getText().toString());
second = Integer.parseInt(two.getText().toString());
switch (v.getId()) {
case R.id.Button01:
operaionFun = "+";
break;
case R.id.Button02:
operaionFun = "-";
break;
case R.id.Button03:
operaionFun = "*";
break;
case R.id.Button04:
operaionFun = "/";
break;
case R.id.Button05:
try {
result.setText(operation(operaionFun, first, second));
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
更多精彩
赞助商链接