【问题标题】:Android: Dialog box disappears immediatelyAndroid:对话框立即消失
【发布时间】:2011-09-30 09:14:37
【问题描述】:

所以我有这个代码。我想要做的是当我按下带有 ID 帮助的按钮(案例 R.id.help)时,我希望自定义文本出现在对话框中。这段代码似乎可以工作,只是对话框出现然后立即消失......有什么问题?

代码如下:

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
          case R.id.icon:
               Intent intent = new Intent(this, Main.class);
               startActivity(intent);
          case R.id.help:
    AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
     alertbox.setMessage("This is the alertbox!");
           alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface arg0, int arg1) {
                   // the button was clicked

               }
           });

           // show it
           alertbox.show();

      }
      return true;
   }
}

【问题讨论】:

    标签: android menu popup dialog


    【解决方案1】:

    我找到了解决办法:

      @Override
       public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
              case R.id.icon:
                   Intent intent = new Intent(this, Main.class);
                   startActivity(intent);
                     return true;
              case R.id.help:
        AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
         alertbox.setMessage("Tai yra dėžutė, kurioje bus aprašymas \n\n text text text text!");
               alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface arg0, int arg1) {
    
                   }
               });
    
               // show it
               alertbox.show();
          }
        return true;
       }
    }
    

    【讨论】:

      【解决方案2】:

      尝试返回 true 而不是 false。见the doc

      【讨论】:

      • 您没有在开关/机箱中使用任何中断。 alertbox.show(); 后面有代码吗? ?
      • 那么接下来我该怎么做呢?对不起,如果我很烦人,这对我来说只是全新的。
      猜你喜欢
      • 2014-04-28
      • 2012-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-24
      • 1970-01-01
      • 1970-01-01
      • 2016-04-24
      相关资源
      最近更新 更多