2011-07-07 54 views
4

一個簡單的問題。我想要一個只有文本的靜態對話框消息才能在選項菜單中按下按鈕時彈出。這是我的菜單代碼: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: 
       //popup window code here 
     } 
     return true; 
    } 
} 

我該做這個最簡單的方法嗎?謝謝!

回答

4
@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: 
      //popup window code here 
Toast.makeText(this, "This is the Toast message", Toast.LENGTH_LONG).show(); 

    } 
    return true; 
} 
} 

或u我可以使用對話框

@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: 
      //popup window code here 
AlertDialog.Builder alertbox = new AlertDialog.Builder(this); 

     // set the message to display 
     alertbox.setMessage("This is the alertbox!"); 

     // add a neutral button to the alert box and assign a click listener 
     alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() { 

      // click listener on the alert box 
      public void onClick(DialogInterface arg0, int arg1) { 
       // the button was clicked 

      } 
     }); 

     // show it 
     alertbox.show(); 

    } 
    return true; 
} 

}

+0

你的第二個選項似乎有效,彈出一個對話框,但在此之後它立即返回到Main.class活動。有什麼問題? :) – Simonas

+0

我不知道... – kannappan

+0

該死......你沒有任何線索嗎?代碼看起來很簡單。 :/ – Simonas

0

您可以創建簡單的對話框

static final int DIALOG_MESSAGE_ID= 0; 

protected Dialog onCreateDialog(int id) { 
    switch(id) { 
    case DIALOG_MESSAGE_ID: 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setMessage("Your message ")//your message 
     }); 
    return builder.create(); 
    break; 
    } 
    return null; 
} 


//in your code 
case R.id.help: 
    showDialog(DIALOG_MESSAGE_ID); 

更在http://developer.android.com/guide/topics/ui/dialogs.html

0
AlertDialog.Builder dialog = new AlertDialog.Builder(context); 
dialog.setMessage("Blah Blah..."); 
dialog.show();