【问题标题】:How to Dialogue box keep open on outside touch如何使对话框在外部触摸时保持打开状态
【发布时间】:2013-06-26 05:57:22
【问题描述】:

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

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

【问题讨论】:

  • 设置可取消为假
  • @user2493740 习惯于对有用的帖子进行投票/标记答案为已解决

标签: android android-alertdialog


【解决方案1】:

setCancelable(布尔可取消): 设置对话框是否可取消。 Read More.

myDialogue.setCancelable(false);

【讨论】:

  • @user2493740,很高兴为您提供帮助。请标记已接受并投票赞成有效的答案,以提供给未来的谷歌用户。 :)
【解决方案2】:

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

myDialogue.setCancelable(false);

【讨论】:

    【解决方案3】:

    添加这一行

    myDialogue.setCancelable(false);
    

    希望这会有所帮助..

    【讨论】:

      【解决方案4】:

      将可取消属性设置为 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();
      }
      

      【讨论】:

        【解决方案5】:

        inisde displayDialog() 方法在.setMessage() 之后,添加以下行:

        myDialogue.setCancelable(false);
        

        【讨论】:

        • @user2493740 你找到解决办法了吗??
        【解决方案6】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-06-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-31
          • 1970-01-01
          • 2015-03-25
          相关资源
          最近更新 更多