2012-11-02 17 views
2

菜單項不顯示下面的對話框是我的代碼,當用戶點擊菜單項時,應該讓他登錄頁面添加該代碼的位置。菜單項也不顯示對話框事件即使沒有錯誤

public boolean onOptionsItemSelected(MenuItem item, int id) { 
    switch (item.getItemId()) { 
     case R.id.Login: 
      startActivity(new Intent(this, Login.class)); 
      return true; 
     case R.id.About: 
     startActivity(new Intent(this, About.class)); 
      return true; 
     case R.id.Post_Ads: 
      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setMessage("Are you sure you want to exit?") 
        .setCancelable(false); 
      AlertDialog alert = builder.create(); 
        alert.show(); 
        startActivity(new Intent(this, Login.class)); 
      return true; 


      } 
    return super.onOptionsItemSelected(item); 
} 
+0

嘗試添加休息;在案件的最後。 –

+0

在情況結束時添加中斷然後不出現對話框 – Tycoon

+0

「沒有對話框出現」,這裏是什麼意思。你有一個「你確定要退出嗎?」對話框無論你是指這一個還是其他的東西?...無論你的意思是菜單對話與登錄,關於,退出沒有出現。 –

回答

0

試試這個,它應該工作:

public boolean onOptionsItemSelected(MenuItem item) { 
      // TODO Auto-generated method stub 
      switch (item.getItemId()) { 
      case R.id.Login: 
       Toast.makeText(getApplicationContext(), "Fired 1", Toast.LENGTH_SHORT).show(); 
       startActivity(new Intent(this, Login.class)); 
       return true; 

      case R.id.About: 
    Toast.makeText(getApplicationContext(), "Fired 2", Toast.LENGTH_SHORT).show(); 
      startActivity(new Intent(this, About.class)); 

       return true; 
         case R.id.Post_Ads: 
       AlertDialog.Builder builder = new AlertDialog.Builder(this); 
       builder.setMessage("Are you sure you want to exit?") 
         .setCancelable(false); 
       AlertDialog alert = builder.create(); 
         alert.show(); 
         startActivity(new Intent(this, Login.class)); 

       return true; 


       } 
     return super.onOptionsItemSelected(item); 

     } 

沒有名爲public boolean onOptionsItemSelected(MenuItem item, int id)方法在Activityclasshandle option menu's.

的方法來處理這類事件是onOptionsItemSelected(MenuItem)

爲了您的菜單項,越來越顯示出來,你必須使用下面的方法也 檢查您是否已經實現了這種方法也還是不行,

public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.your_menu_file, menu); 
    return true; 
} 

而小幅回調,

你有實施了以下Alertdialog方法,

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setMessage("Are you sure you want to exit?") 
        .setCancelable(false); 
      AlertDialog alert = builder.create(); 
        alert.show(); 
        startActivity(new Intent(this, Login.class)); 

您沒有提供任何確定或取消此按鈕, 而且你讓它setCancelable(false)。所以用戶不能在這裏選擇任何選項。

所以我建議你在這裏給出Ok和Cancel按鈕,並在onClick()方法內做你想做的事情。像下面的例子:

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setMessage("Are you sure you want to exit?") 
        .setCancelable(false); 
      AlertDialog alert = builder.create(); 
      alert.setButton("Ok", new DialogInterface.OnClickListener() { 

       public void onClick(DialogInterface dialog, int which) { 
        // Do call some activity or close the app. Do what you wish to; 

       } 
      }); 

alert.setButton2("Cancel", new DialogInterface.OnClickListener() { 

       public void onClick(DialogInterface dialog, int which) { 
        // Do stay in the current activity or something else. Do what you wish to; 

       } 
      }); 
        alert.show(); 

      return true; 

希望,這可能會有所幫助。

+0

謝謝你的工作,但對話框立即在幾分之一秒內消失 – Tycoon

+0

好...做得很好。不要忘記接受答案和upvote。 –

+0

「對話框立即在幾分之一秒內消失」無法讓你。哪個對話框?當它消失時,無論點擊發生在某處或出現並自動消失? –

0

嘗試這個

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setMessage("Navigate to login").setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          //do things 
          startActivity(new Intent(this, Login.class)); 
         } 
        }); 

      AlertDialog alert = builder.create(); 
      alert.show(); 
相關問題