您好我是Android的新手,我的操作栏上有几个项目,还有一个注销图标/按钮。现在,当用户点击退出按钮时,它应显示确认对话框,如果用户点击“是”,则返回登录屏幕。
我已设法做到这一点,但是我注意到现在显示所有项目的确认对话框,而不仅仅是注销。
这是我的代码:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id) {
case R.id.action_open_invoices:
Intent i = new Intent(context, InvoicesActivity.class);
startActivity(i);
case R.id.action_synchronize:
// Start Updater Service
Intent updaterService = new Intent(context, UpdaterService.class);
startService(updaterService);
case R.id.action_logout:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Confirm");
builder.setMessage("Are you sure?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
settings.edit().remove("Token").commit();
Intent loginIntent = new Intent(context, LoginActivity.class);
startActivity(loginIntent);
dialog.dismiss();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
return super.onOptionsItemSelected(item);
}
答案 0 :(得分:1)
case R.id.action_open_invoices:
Intent i = new Intent(context, InvoicesActivity.class);
startActivity(i);
break;
使用break语句