我想在drawerlayout中添加一个弹出屏幕。在退出之前,屏幕将给出是或否选项。现在我有了这段代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
答案 0 :(得分:0)
你去;在你的活动有抽屉这样做:
@Override
public void onBackPressed() {
//super.onBackPressed();
//i have commented out the super so that app wont destroy immediately
askBeforeExit();
}
private void askBeforeExit() {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
String title = "Confirm Exit";
String message = "Are u sure u want to exit";
dialogBuilder.setTitle(title);
dialogBuilder.setMessage(message);
dialogBuilder.setNegativeButton("Nope", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialogBuilder.setPositiveButton("Yeah sure", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//if okay exit the app
dialog.dismiss();
finish();
}
});
dialogBuilder.create();
dialogBuilder.show();
}