試試這個,它應該工作:
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)
方法在Activity
class
到handle 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;
希望,這可能會有所幫助。
嘗試添加休息;在案件的最後。 –
在情況結束時添加中斷然後不出現對話框 – Tycoon
「沒有對話框出現」,這裏是什麼意思。你有一個「你確定要退出嗎?」對話框無論你是指這一個還是其他的東西?...無論你的意思是菜單對話與登錄,關於,退出沒有出現。 –