我有一个对话框提示用户,用户输入将在代码的下一步中使用。我正在尝试使用OnDismiss来使其余的代码接受用户输入。以下是代码片段:
AlertDialog.Builder alert=新AlertDialog.Bilter(this);
alert.setTitle("Title");
alert.setMessage("Message");
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
input = 3;
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
input = 1;
}
});
alert.show();
}
public void OnDismiss(DialogInterface dialogInterface)
{
Toast.makeText(this, "Input: " + input, Toast.LENGTH_LONG)
.show();
}
但是,当我按下对话框按钮时,对话框将被关闭,但吐司不会出现。请说明我在这里做错了什么。
首先将OnDismissListener
设置为您的AlertDialog
,然后在按下对话框按钮时调用dismiss()
,代码如下:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title");
alert.setMessage("Message");
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
input = 3;
dialog.dismiss();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
input = 1;
dialog.dismiss();
}
});
alert.setOnDismissListener(new OnDismissListener(){
public void OnDismiss(DialogInterface dialogInterface)
{
Toast.makeText(this, "Input: " + input, Toast.LENGTH_LONG)
.show();
}
});
alert.show();
您必须使用dialog.disse();
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
input = 3;
dialog.dismiss();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
input = 1;
dialog.dismiss();
}
});