当用户单击我的提交按钮时,为什么会出现这种奇怪的行为?
我有一个活动 - 一个带有一些文本字段的表单,用户在填写所有字段后单击提交按钮。当用户触摸提交按钮时,它应该显示一个警报对话框,然后当用户触摸“确定”时,将执行 OnClicklistener 代码的其余部分。目前,我的代码是这样的。
提交/完成按钮的侦听器:
private final OnClickListener mFinishListener = new OnClickListener() {
public void onClick(View v) {
displayAlert();
// Some other things to do here. Lets say showing some other activity
};
警报对话框代码:
public void displayAlert(){
new AlertDialog.Builder(this).setMessage("Hi , I am Alert Dialog")
.setTitle("My Alert")
.setCancelable(true)
.setNeutralButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton){
finish();
}
})
.show();
}
我收到奇怪的输出。当我单击“提交/完成”按钮时,它会显示警报对话框,但在我按下“确定”按钮之前它会消失。为什么?
I have an activity - a form with some TextFields and the user clicks a submit button after filling all the fields. When the user touches the submit button, it should show an alert dialog, and then when user touches OK, the rest of the OnClicklistener code will execute. Currently, my code is something like this.
listener for submit/finish button:
private final OnClickListener mFinishListener = new OnClickListener() {
public void onClick(View v) {
displayAlert();
// Some other things to do here. Lets say showing some other activity
};
Alert dialog code:
public void displayAlert(){
new AlertDialog.Builder(this).setMessage("Hi , I am Alert Dialog")
.setTitle("My Alert")
.setCancelable(true)
.setNeutralButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton){
finish();
}
})
.show();
}
I am getting strange output. When I click the submit/finish button, it shows me the alert dialog but it disappears before I press touch the OK button. Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您希望在用户单击“确定”后执行的代码应移至 AlertDialog 的 onClick 方法。就在
finish()
之后。否则它将在对话框显示时开始执行。The code that you wish to execute after the user clicks OK should be moved to the onClick method of the AlertDialog. Right after
finish()
. Otherwise it will begin executing while the dialog box is showing.如果您只想关闭对话框
如果您想同时关闭对话框和活动
If you wish to close the dialog only
If you wish to close both dialog and close activity