2011-07-07 64 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