2011-07-07 132 views
2

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

下面的代码:

@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; 
    } 
} 

回答

0

我找到了解决办法:

@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; 
    } 
} 
0

尝试返回真正的而不是虚假的。见the doc

+0

没有什么变化。 :/ – Simonas

+0

你没有在你的开关/外壳中使用任何中断。 alertbox.show()之后是否有任何代码? ? – xevincent

+0

不,就是这样.. – Simonas