Android alertdialog implements confirmation and exits. androidalertdialog
Package com. example. alertdialog; import android. OS. bundle; import android. app. activity; import android. app. alertDialog; import android. content. dialogInterface; import android. view. menu; import android. view. view; import android. widget. toast; public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. Activity_main);} // if the name is onBackPressed, it is the result of pressing the return key of the mobile phone. Leave the parameter blank. Public void onBackPressed1 (View v) {new AlertDialog. Builder (this). setTitle ("are you sure you want to exit? "). SetIcon (android. r. drawable. ic_dialog_info ). setPositiveButton ("OK", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {// click "OK" to operate MainActivity. this. finish ();}}). setNegativeButton ("return", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {// The operation after clicking "return". No operation Toast is set here. makeText (MainActivity. this, "you have clicked the return key", Toast. LENGTH_LONG ). show ();}}). show ();}}