public class MainActivity extends Activity {
EditText a, b;
TextView c;
String a1;
String b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
a = (EditText) findViewById(R.id.editText1);
b = (EditText) findViewById(R.id.editText2);
c = (TextView) findViewById(R.id.textView1);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
a1 = a.getText().toString();
b1 = b.getText().toString();
if (!TextUtils.isEmpty(a1) && !TextUtils.isEmpty(b1)) {
int total = Integer.parseInt(a1) + Integer.parseInt(b1);
c.setText(total + "");
} else {
Toast.makeText(MainActivity.this, "请输入您要相加的两个数",
Toast.LENGTH_SHORT).show();
}
}
});
}
}