菜单项未显示对话框下面是我的代码,当用户点击菜单项时,应该带他登录页面添加该代码。
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)
试试这个,它应该有效:
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));
您在此处未提供任何确定或取消按钮, 而且你也把它设置为可取消(假)。所以用户不能在这里选择任何选项。
所以我建议你在这里给出Ok和Cancel Button,并在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;
希望,这可能会有所帮助。
答案 1 :(得分: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();