在工具栏内显示带有警告对话框的注销
Display Logout with an alert dialog inside a toolbar
[需要帮助。我如何在工具栏内使用警报对话框执行注销?因为我希望在用户想要注销他的帐户时显示一个警报对话框。我正处于学习阶段。希望你们能帮帮我,嘿嘿。这是我注销的代码 activity: : ][1] [2]
[1]: https://i.stack.imgur.com/lU7i1.png
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
alert.setMessage("Are you sure?")
.setPositiveButton("Logout", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}).setNegativeButton("Cancel", null);
AlertDialog alert1 = alert.create();
alert1.show();
if (id == R.id.action_settings) {
logout();
return true;
}
return super.onOptionsItemSelected(item);
}
private void logout() {
startActivity(new Intent(this, LoginActivity.class));
finish();
}
你需要考虑算法结构。
首先,用户单击选项菜单并select注销。
然后,显示弹窗。
最后,用户与此弹出窗口交互并验证注销。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
// This is first step.
showPopup();
return true;
}
return super.onOptionsItemSelected(item);
}
// first step helper function
private void showPopup() {
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
alert.setMessage("Are you sure?")
.setPositiveButton("Logout", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
logout(); // Last step. Logout function
}
}).setNegativeButton("Cancel", null);
AlertDialog alert1 = alert.create();
alert1.show();
}
private void logout() {
startActivity(new Intent(this, LoginActivity.class));
finish();
}
[需要帮助。我如何在工具栏内使用警报对话框执行注销?因为我希望在用户想要注销他的帐户时显示一个警报对话框。我正处于学习阶段。希望你们能帮帮我,嘿嘿。这是我注销的代码 activity: : ][1] [2]
[1]: https://i.stack.imgur.com/lU7i1.png
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
alert.setMessage("Are you sure?")
.setPositiveButton("Logout", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}).setNegativeButton("Cancel", null);
AlertDialog alert1 = alert.create();
alert1.show();
if (id == R.id.action_settings) {
logout();
return true;
}
return super.onOptionsItemSelected(item);
}
private void logout() {
startActivity(new Intent(this, LoginActivity.class));
finish();
}
你需要考虑算法结构。
首先,用户单击选项菜单并select注销。
然后,显示弹窗。
最后,用户与此弹出窗口交互并验证注销。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
// This is first step.
showPopup();
return true;
}
return super.onOptionsItemSelected(item);
}
// first step helper function
private void showPopup() {
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
alert.setMessage("Are you sure?")
.setPositiveButton("Logout", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
logout(); // Last step. Logout function
}
}).setNegativeButton("Cancel", null);
AlertDialog alert1 = alert.create();
alert1.show();
}
private void logout() {
startActivity(new Intent(this, LoginActivity.class));
finish();
}