2

我的屏幕上有对话框。当我触摸屏幕对话框关闭。我希望对话框不应该在外部触摸时关闭。我的代码如下:-

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();
    }
4

6 回答 6

3

您只需将cancelable 设置为false,这样当在对话框外触摸时,对话框将保持打开状态。在你的代码中试试这个:

myDialogue.setCancelable(false);
于 2014-03-27T10:00:08.073 回答
3

setCancelable(boolean cancelable) :设置对话框是否可取消。阅读更多

myDialogue.setCancelable(false);
于 2013-06-26T06:03:35.590 回答
2

添加这一行

myDialogue.setCancelable(false);

希望这可以帮助..

于 2013-06-26T06:03:03.583 回答
2

将可取消属性设置为 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();
}
于 2013-06-26T06:11:21.010 回答
1

您可能已经得到了这个,但对于未来的人来说,这对我有用:您可以覆盖外部触摸以在对话框上不执行任何操作,如下所示https://stackoverflow.com/a/5831214

于 2014-12-03T16:19:52.900 回答
1

inisdedisplayDialog()方法后.setMessage(),添加以下行:

myDialogue.setCancelable(false);
于 2013-06-26T06:07:00.540 回答