Java 当触摸在[Android]之外时,AlertDialog消失
我在我的应用程序上使用了一个警报对话框,但当用户接触到它的外部时,它会一直隐藏。 这是我的密码:Java 当触摸在[Android]之外时,AlertDialog消失,java,android,android-alertdialog,Java,Android,Android Alertdialog,我在我的应用程序上使用了一个警报对话框,但当用户接触到它的外部时,它会一直隐藏。 这是我的密码: public class DialogMessageEnd extends DialogFragment { String winner; @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient di
public class DialogMessageEnd extends DialogFragment
{
String winner;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
Snooker_Scoreboard ss = new Snooker_Scoreboard();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(false);
builder.setMessage(ss.winnerPlayer + " won the match ("+ss.frame1ToPass+"-"+ss.frame2ToPass+")!")
.setPositiveButton("New Match!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent i = new Intent(getContext(),PlayerSelection.class);
startActivity(i);
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
如你所见,我曾经
builder.setCancelable(false);
但它仍然不能解决问题。
你能帮助我吗?谢谢添加
AlertDialog alertDialog = builder.show();
alertDialog.setCanceledOnTouchOutside(false);
在您的代码中使用设置CanceledOnTouchOutside(false)以防止触摸警报对话框外部时解除 可设置取消(false)用于防止按下后退按钮时关闭
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
Snooker_Scoreboard ss = new Snooker_Scoreboard();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(false);
builder.setMessage(ss.winnerPlayer + " won the match ("+ss.frame1ToPass+"-"+ss.frame2ToPass+")!")
.setPositiveButton("New Match!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent i = new Intent(getContext(),PlayerSelection.class);
startActivity(i);
}
});
// Create the AlertDialog object and return it
Dialog dialog = builder.create();
dialog.setCanceledOnTouchOutside(false);
return dialog;
}
您可以覆盖对话的默认功能,并确保不会发生任何事情。应该行得通
@Override
public boolean onTouchEvent(MotionEvent event) {
// If we've received a touch notification that the user has touched
// outside the app, finish the activity.
if (MotionEvent.ACTION_OUTSIDE == event.getAction()) {
// Do Something or not...
return true;
}
return false;
}
或者为了更好的实践:
builder.setCanceledOnTouchOutside(false)
您是否查看了
对话框。setCanceledOnTouchOutside
方法?如果您至少能够阅读和理解,您不会发现我使用的Alert Dialog没有setCanceledOnTouchOutside方法。@Pino AlertDialog确实有setCanceledOnTouchOutside它告诉我AlertDialog没有setCanceledOnTouchOutside方法。请参阅更新的答案。在退货声明前看两行谢谢,太好了。