Android 如何在外部接触时保持对话框打开
我的屏幕上有一个对话框。当我触摸屏幕时,对话框关闭。我希望对话框在外部触摸时不应关闭。我的代码如下所示:-Android 如何在外部接触时保持对话框打开,android,android-alertdialog,Android,Android Alertdialog,我的屏幕上有一个对话框。当我触摸屏幕时,对话框关闭。我希望对话框在外部触摸时不应关闭。我的代码如下所示:- public class Dialogue extends Activity{ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_dialogue);
public class Dialogue extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialogue);
this.setFinishOnTouchOutside(false);
displayDialogue();
}
private void displayDialogue(){
final AlertDialog.Builder myDialogue = new AlertDialog.Builder(this);
myDialogue.setMessage("Please check your voice input output settings.It should be ON" );
TextView messageView = new TextView(this);
messageView.setGravity(Gravity.CENTER);
myDialogue.setView(messageView);
myDialogue.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(Dialogue.this, MainActivity.class);
startActivity(i);
finish();
}
});
AlertDialog dialog = myDialogue.create();
dialog.show();
}
添加这一行
myDialogue.setCancelable(false);
希望这有帮助。可设置可取消(布尔可取消):
设置对话框是否可取消
在.setMessage()
之后的sdedisplayDialog()
方法中,添加以下行:
myDialogue.setCancelable(false);
将可取消属性设置为false
private void displayDialogue(){
final AlertDialog.Builder myDialogue = new AlertDialog.Builder(this);
myDialogue.setMessage("Please check your voice input output settings.It should be ON" );
TextView messageView = new TextView(this);
messageView.setGravity(Gravity.CENTER);
myDialogue.setView(messageView);
myDialogue.setCancelable(false);
myDialogue.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(Dialogue.this, MainActivity.class);
startActivity(i);
finish();
}
});
AlertDialog dialog = myDialogue.create();
dialog.show();
}
您只需将cancelable设置为false,这样当触摸到对话框外部时,对话框将保持打开状态。 在代码中尝试以下操作:
myDialogue.setCancelable(false);
你可能已经知道了,但对未来的人来说,这对我来说很有用:
如图所示,您可以覆盖外部触摸,使其在对话框上不执行任何操作将可取消设置为false@user2493740养成将答案向上投票/标记为已解决的答案的习惯,以获得帮助post@user2493740,很乐意帮忙。请标记“已接受”和“向上投票”有效答案,以供未来的谷歌搜索者参考。:)@用户2493740您找到解决方案了吗??
myDialogue.setCancelable(false);