Onclick Listener for OK Button

This is my code for creating a dialog box.

public void onClick(View v) { try{ Builder dialog= new AlertDialog.Builder(context); dialog.setTitle(R.string.dialog_title1); dialog.setMessage(R.string.url); dialog.setPositiveButton(R.string.dialog_ok, null); dialog.show(); } 

I want to add an event listener to the SetPositive Button (OK button). When you click OK, my application should be closed, and the user should exit the application. Can someone help me achieve this?

+4
source share
4 answers
 dialog.setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { MyActivity.this.finish(); } }); 
+15
source

Try the following:

  new AlertDialog.Builder(this) .setTitle("XYZ") .setMessage("Do you want to Exit ?") .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub MyActivityClass.this.finish(); dialog.dismiss(); } }) .setNeutralButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.dismiss(); } }) .create() .show(); 
+2
source

try this code

 Builder dialog = new AlertDialog.Builder(this); dialog.setTitle("Message"); dialog.setMessage("Exit App?"); dialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Enter your Code for exit Application } }); dialog.show(); 
0
source
 AlertDialog ad1=new AlertDialog.Builder(Hangman.this).create(); ad1.setMessage("You want to Exit"); ad1.setCancelable(false); ad1.setButton("Exit", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub HomeActivity.this.finish(); } }); 

Try this ... :) Updated

0
source

Source: https://habr.com/ru/post/1400148/


All Articles