我的代码采用分离监听器的模式,即不在 setOnclickListener 中 new OnClickListener().
public class MainActivity extends Activity implements View.OnClickListener{
protected void onCreate(Bundle savedInstanceState){
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(this);
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final EditText etname = (EditText)findViewById(R.id.name);
EditText etphone = (EditText)findViewById(R.id.phoneNum);
EditText etpassword = (EditText)findViewById(R.id.password);
RadioGroup group = (RadioGroup)findViewById(R.id.radiogroup1);
RadioButton select = (RadioButton)findViewById(group.getCheckedRadioButtonId());
String line = "你的姓名:" + etname.getText().toString() +
"\n你的电话:" + etphone.getText().toString() +
"\n你的密码:" + etpassword.getText().toString() +
"\n你的选择:" + select.getText().toString();
AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
dlg.setIcon(android.R.drawable.btn_dialog);
dlg.setTitle("欢迎你的注册");
dlg.setMessage(line);
dlg.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
etname.setText("你已点过注册啦!");
}
});
dlg.setNegativeButton("退出", null);
dlg.create().show();
}
}
如果以上论述有错误或不足之处,欢迎大家指出,希望我的文章能帮助到大家。