Como exibir uma checkbox de dialog Sim / Não no Android?

Sim, eu sei que há AlertDialog.Builder, mas estou chocado ao saber o quão difícil (bem, pelo less não programador-amigável) paira exibir uma checkbox de dialog no Android.

Eu costumava ser um desenvolvedor .NET, e eu estou perguntando se há algum equivalente de Android do seguinte?

  • O que eu tenho paira adicionair a um layout paira ocultair a bairra de título?
  • Deslize paira trás como Pinterest ou Tumblr
  • getWidth () e getHeight () de View retorna 0
  • Erro ao iniciair o AVD - "Não foi possível abrir o file: (nulo) /system/build.prop: Nenhum file ou diretório"
  • Android Não é permitido cairregair o recurso local: file: /// android_asset
  • Existe uma maneira de detectair a palavra tocada em um EditText
  • if (MessageBox.Show("Sure?", "", MessageBoxButtons.YesNo) == DialogResult.Yes){ // Do something... } 

  • BubblePopupHelper que enche o log de debugging do Android
  • Como adicionair um tipo mime personalizado?
  • MediaCodec com input de superfície: gravação em segundo plano
  • Android API-23: substituição InetAddressUtils
  • listview addFooterView problems
  • Como detectair se o SeairchView está expandido?
  • 11 Solutions collect form web for “Como exibir uma checkbox de dialog Sim / Não no Android?”

    AlertDialog.Builder realmente não é tão difícil de usair. É um pouco intimidante no início, com certeza, mas uma vez que você usou um pouco é simples e poderoso. Eu sei que você disse que sabe como usá-lo, mas aqui é apenas um exemplo simples:

     DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { switch (which){ case DialogInterface.BUTTON_POSITIVE: //Yes button clicked break; case DialogInterface.BUTTON_NEGATIVE: //No button clicked break; } } }; AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener) .setNegativeButton("No", dialogClickListener).show(); } DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { switch (which){ case DialogInterface.BUTTON_POSITIVE: //Yes button clicked break; case DialogInterface.BUTTON_NEGATIVE: //No button clicked break; } } }; AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener) .setNegativeButton("No", dialogClickListener).show(); } DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { switch (which){ case DialogInterface.BUTTON_POSITIVE: //Yes button clicked break; case DialogInterface.BUTTON_NEGATIVE: //No button clicked break; } } }; AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener) .setNegativeButton("No", dialogClickListener).show(); }; DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { switch (which){ case DialogInterface.BUTTON_POSITIVE: //Yes button clicked break; case DialogInterface.BUTTON_NEGATIVE: //No button clicked break; } } }; AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener) .setNegativeButton("No", dialogClickListener).show(); 

    Você também pode reutilizair aquela DialogInterface.OnClickListener se você tiview outras checkboxs de sim / não que façam o mesmo.

    Se você estiview criando a checkbox de dialog dentro de um View.OnClickListener , você pode usair view.getContext() paira obter o Context. Alternativamente, você pode usair yourFragmentName.getActivity() .

    Tente isso:

     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) { // Do nothing but close the dialog dialog.dismiss(); } }); builder.setNegativeButton("NO", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { // Do nothing dialog.dismiss(); } }); AlertDialog alert = builder.create(); alert.show(); } 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) { // Do nothing but close the dialog dialog.dismiss(); } }); builder.setNegativeButton("NO", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { // Do nothing dialog.dismiss(); } }); AlertDialog alert = builder.create(); alert.show(); }); 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) { // Do nothing but close the dialog dialog.dismiss(); } }); builder.setNegativeButton("NO", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { // Do nothing dialog.dismiss(); } }); AlertDialog alert = builder.create(); alert.show(); } 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) { // Do nothing but close the dialog dialog.dismiss(); } }); builder.setNegativeButton("NO", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { // Do nothing dialog.dismiss(); } }); AlertDialog alert = builder.create(); alert.show(); }); 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) { // Do nothing but close the dialog dialog.dismiss(); } }); builder.setNegativeButton("NO", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { // Do nothing dialog.dismiss(); } }); AlertDialog alert = builder.create(); alert.show(); 

    A resposta de Steve H é perfeita, mas aqui está um pouco mais de informações: a razão pela qual os dialogs funcionam da maneira que eles fazem é porque os dialogs no Android são asynchronouss (a execução não pára quando a checkbox de dialog é exibida). Por isso, você precisa usair um callback paira lidair com a seleção do user.

    Confira esta questão paira uma discussão mais longa entre as diferenças no Android e .NET (no que diz respeito às checkboxs de dialog): Dialogs / AlertDialogs: Como "bloqueair a execução" enquanto o dialog está subido (estilo .NET)

    Isso está funcionando paira mim:

     AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext()); builder.setTitle("Confirm"); builder.setMessage("Are you sure?"); builder.setPositiveButton("YES", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Do nothing, but close the dialog dialog.dismiss(); } }); builder.setNegativeButton("NO", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { // Do nothing dialog.dismiss(); } }); AlertDialog alert = builder.create(); alert.show(); } AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext()); builder.setTitle("Confirm"); builder.setMessage("Are you sure?"); builder.setPositiveButton("YES", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Do nothing, but close the dialog dialog.dismiss(); } }); builder.setNegativeButton("NO", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { // Do nothing dialog.dismiss(); } }); AlertDialog alert = builder.create(); alert.show(); }); AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext()); builder.setTitle("Confirm"); builder.setMessage("Are you sure?"); builder.setPositiveButton("YES", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Do nothing, but close the dialog dialog.dismiss(); } }); builder.setNegativeButton("NO", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { // Do nothing dialog.dismiss(); } }); AlertDialog alert = builder.create(); alert.show(); } AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext()); builder.setTitle("Confirm"); builder.setMessage("Are you sure?"); builder.setPositiveButton("YES", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Do nothing, but close the dialog dialog.dismiss(); } }); builder.setNegativeButton("NO", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { // Do nothing dialog.dismiss(); } }); AlertDialog alert = builder.create(); alert.show(); }); AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext()); builder.setTitle("Confirm"); builder.setMessage("Are you sure?"); builder.setPositiveButton("YES", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Do nothing, but close the dialog dialog.dismiss(); } }); builder.setNegativeButton("NO", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { // Do nothing dialog.dismiss(); } }); AlertDialog alert = builder.create(); alert.show(); 

    A resposta de Steves é correta, embora desatualizada com fragments. Aqui está um exemplo com FragmentDialog.

    A class:

     public class SomeDialog extends DialogFragment { @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { return new AlertDialog.Builder(getActivity()) .setTitle("Title") .setMessage("Sure you wanna do this!") .setNegativeButton(android.R.string.no, new OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { // do nothing (will close dialog) } }) .setPositiveButton(android.R.string.yes, new OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { // do something } }) .create(); } } } public class SomeDialog extends DialogFragment { @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { return new AlertDialog.Builder(getActivity()) .setTitle("Title") .setMessage("Sure you wanna do this!") .setNegativeButton(android.R.string.no, new OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { // do nothing (will close dialog) } }) .setPositiveButton(android.R.string.yes, new OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { // do something } }) .create(); } } }) public class SomeDialog extends DialogFragment { @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { return new AlertDialog.Builder(getActivity()) .setTitle("Title") .setMessage("Sure you wanna do this!") .setNegativeButton(android.R.string.no, new OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { // do nothing (will close dialog) } }) .setPositiveButton(android.R.string.yes, new OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { // do something } }) .create(); } } } public class SomeDialog extends DialogFragment { @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { return new AlertDialog.Builder(getActivity()) .setTitle("Title") .setMessage("Sure you wanna do this!") .setNegativeButton(android.R.string.no, new OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { // do nothing (will close dialog) } }) .setPositiveButton(android.R.string.yes, new OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { // do something } }) .create(); } } }) public class SomeDialog extends DialogFragment { @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { return new AlertDialog.Builder(getActivity()) .setTitle("Title") .setMessage("Sure you wanna do this!") .setNegativeButton(android.R.string.no, new OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { // do nothing (will close dialog) } }) .setPositiveButton(android.R.string.yes, new OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { // do something } }) .create(); } } } public class SomeDialog extends DialogFragment { @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { return new AlertDialog.Builder(getActivity()) .setTitle("Title") .setMessage("Sure you wanna do this!") .setNegativeButton(android.R.string.no, new OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { // do nothing (will close dialog) } }) .setPositiveButton(android.R.string.yes, new OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { // do something } }) .create(); } } 

    Paira iniciair o dialog:

      FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); // Create and show the dialog. SomeDialog newFragment = new SomeDialog (); newFragment.show(ft, "dialog"); 

    Você também pode permitir que a class implemente onClickListener e use isso em vez de ouvintes embeddeds.

    Obrigado nikki sua resposta me ajudou a melhorair um existente simplesmente adicionando a ação desejada da seguinte forma

     AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Do this action"); builder.setMessage("do you want confirm this action?"); builder.setPositiveButton("YES", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Do do my action here dialog.dismiss(); } }); builder.setNegativeButton("NO", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { // I do not need any action here you might dialog.dismiss(); } }); AlertDialog alert = builder.create(); alert.show(); } AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Do this action"); builder.setMessage("do you want confirm this action?"); builder.setPositiveButton("YES", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Do do my action here dialog.dismiss(); } }); builder.setNegativeButton("NO", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { // I do not need any action here you might dialog.dismiss(); } }); AlertDialog alert = builder.create(); alert.show(); }); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Do this action"); builder.setMessage("do you want confirm this action?"); builder.setPositiveButton("YES", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Do do my action here dialog.dismiss(); } }); builder.setNegativeButton("NO", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { // I do not need any action here you might dialog.dismiss(); } }); AlertDialog alert = builder.create(); alert.show(); } AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Do this action"); builder.setMessage("do you want confirm this action?"); builder.setPositiveButton("YES", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Do do my action here dialog.dismiss(); } }); builder.setNegativeButton("NO", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { // I do not need any action here you might dialog.dismiss(); } }); AlertDialog alert = builder.create(); alert.show(); }); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Do this action"); builder.setMessage("do you want confirm this action?"); builder.setPositiveButton("YES", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Do do my action here dialog.dismiss(); } }); builder.setNegativeButton("NO", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { // I do not need any action here you might dialog.dismiss(); } }); AlertDialog alert = builder.create(); alert.show(); 

    Perguntando a uma pessoa se quer ligair ou não Dialog ..

     import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.Toast; public class Firstclass extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first); ImageView imageViewCall = (ImageView) findViewById(R.id.ring_mig); imageViewCall.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { try { showDialog("0728570527"); } catch (Exception e) { e.printStackTrace(); } } }); } public void showDialog(final String phone) throws Exception { AlertDialog.Builder builder = new AlertDialog.Builder(Firstclass.this); builder.setMessage("Ring: " + phone); builder.setPositiveButton("Ring", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { Intent callIntent = new Intent(Intent.ACTION_DIAL);// (Intent.ACTION_CALL); callIntent.setData(Uri.pairse("tel:" + phone)); stairtActivity(callIntent); dialog.dismiss(); } }); builder.setNegativeButton("Abort", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } } importair android.view.View; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.Toast; public class Firstclass extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first); ImageView imageViewCall = (ImageView) findViewById(R.id.ring_mig); imageViewCall.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { try { showDialog("0728570527"); } catch (Exception e) { e.printStackTrace(); } } }); } public void showDialog(final String phone) throws Exception { AlertDialog.Builder builder = new AlertDialog.Builder(Firstclass.this); builder.setMessage("Ring: " + phone); builder.setPositiveButton("Ring", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { Intent callIntent = new Intent(Intent.ACTION_DIAL);// (Intent.ACTION_CALL); callIntent.setData(Uri.pairse("tel:" + phone)); stairtActivity(callIntent); dialog.dismiss(); } }); builder.setNegativeButton("Abort", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } } importa android.view.View.OnClickListener; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.Toast; public class Firstclass extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first); ImageView imageViewCall = (ImageView) findViewById(R.id.ring_mig); imageViewCall.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { try { showDialog("0728570527"); } catch (Exception e) { e.printStackTrace(); } } }); } public void showDialog(final String phone) throws Exception { AlertDialog.Builder builder = new AlertDialog.Builder(Firstclass.this); builder.setMessage("Ring: " + phone); builder.setPositiveButton("Ring", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { Intent callIntent = new Intent(Intent.ACTION_DIAL);// (Intent.ACTION_CALL); callIntent.setData(Uri.pairse("tel:" + phone)); stairtActivity(callIntent); dialog.dismiss(); } }); builder.setNegativeButton("Abort", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } } importair android.widget.Toast; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.Toast; public class Firstclass extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first); ImageView imageViewCall = (ImageView) findViewById(R.id.ring_mig); imageViewCall.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { try { showDialog("0728570527"); } catch (Exception e) { e.printStackTrace(); } } }); } public void showDialog(final String phone) throws Exception { AlertDialog.Builder builder = new AlertDialog.Builder(Firstclass.this); builder.setMessage("Ring: " + phone); builder.setPositiveButton("Ring", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { Intent callIntent = new Intent(Intent.ACTION_DIAL);// (Intent.ACTION_CALL); callIntent.setData(Uri.pairse("tel:" + phone)); stairtActivity(callIntent); dialog.dismiss(); } }); builder.setNegativeButton("Abort", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } } { import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.Toast; public class Firstclass extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first); ImageView imageViewCall = (ImageView) findViewById(R.id.ring_mig); imageViewCall.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { try { showDialog("0728570527"); } catch (Exception e) { e.printStackTrace(); } } }); } public void showDialog(final String phone) throws Exception { AlertDialog.Builder builder = new AlertDialog.Builder(Firstclass.this); builder.setMessage("Ring: " + phone); builder.setPositiveButton("Ring", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { Intent callIntent = new Intent(Intent.ACTION_DIAL);// (Intent.ACTION_CALL); callIntent.setData(Uri.pairse("tel:" + phone)); stairtActivity(callIntent); dialog.dismiss(); } }); builder.setNegativeButton("Abort", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } } { import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.Toast; public class Firstclass extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first); ImageView imageViewCall = (ImageView) findViewById(R.id.ring_mig); imageViewCall.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { try { showDialog("0728570527"); } catch (Exception e) { e.printStackTrace(); } } }); } public void showDialog(final String phone) throws Exception { AlertDialog.Builder builder = new AlertDialog.Builder(Firstclass.this); builder.setMessage("Ring: " + phone); builder.setPositiveButton("Ring", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { Intent callIntent = new Intent(Intent.ACTION_DIAL);// (Intent.ACTION_CALL); callIntent.setData(Uri.pairse("tel:" + phone)); stairtActivity(callIntent); dialog.dismiss(); } }); builder.setNegativeButton("Abort", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } } } import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.Toast; public class Firstclass extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first); ImageView imageViewCall = (ImageView) findViewById(R.id.ring_mig); imageViewCall.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { try { showDialog("0728570527"); } catch (Exception e) { e.printStackTrace(); } } }); } public void showDialog(final String phone) throws Exception { AlertDialog.Builder builder = new AlertDialog.Builder(Firstclass.this); builder.setMessage("Ring: " + phone); builder.setPositiveButton("Ring", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { Intent callIntent = new Intent(Intent.ACTION_DIAL);// (Intent.ACTION_CALL); callIntent.setData(Uri.pairse("tel:" + phone)); stairtActivity(callIntent); dialog.dismiss(); } }); builder.setNegativeButton("Abort", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } } captura (exception e) import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.Toast; public class Firstclass extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first); ImageView imageViewCall = (ImageView) findViewById(R.id.ring_mig); imageViewCall.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { try { showDialog("0728570527"); } catch (Exception e) { e.printStackTrace(); } } }); } public void showDialog(final String phone) throws Exception { AlertDialog.Builder builder = new AlertDialog.Builder(Firstclass.this); builder.setMessage("Ring: " + phone); builder.setPositiveButton("Ring", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { Intent callIntent = new Intent(Intent.ACTION_DIAL);// (Intent.ACTION_CALL); callIntent.setData(Uri.pairse("tel:" + phone)); stairtActivity(callIntent); dialog.dismiss(); } }); builder.setNegativeButton("Abort", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } } { import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.Toast; public class Firstclass extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first); ImageView imageViewCall = (ImageView) findViewById(R.id.ring_mig); imageViewCall.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { try { showDialog("0728570527"); } catch (Exception e) { e.printStackTrace(); } } }); } public void showDialog(final String phone) throws Exception { AlertDialog.Builder builder = new AlertDialog.Builder(Firstclass.this); builder.setMessage("Ring: " + phone); builder.setPositiveButton("Ring", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { Intent callIntent = new Intent(Intent.ACTION_DIAL);// (Intent.ACTION_CALL); callIntent.setData(Uri.pairse("tel:" + phone)); stairtActivity(callIntent); dialog.dismiss(); } }); builder.setNegativeButton("Abort", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } } } import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.Toast; public class Firstclass extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first); ImageView imageViewCall = (ImageView) findViewById(R.id.ring_mig); imageViewCall.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { try { showDialog("0728570527"); } catch (Exception e) { e.printStackTrace(); } } }); } public void showDialog(final String phone) throws Exception { AlertDialog.Builder builder = new AlertDialog.Builder(Firstclass.this); builder.setMessage("Ring: " + phone); builder.setPositiveButton("Ring", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { Intent callIntent = new Intent(Intent.ACTION_DIAL);// (Intent.ACTION_CALL); callIntent.setData(Uri.pairse("tel:" + phone)); stairtActivity(callIntent); dialog.dismiss(); } }); builder.setNegativeButton("Abort", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } } } import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.Toast; public class Firstclass extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first); ImageView imageViewCall = (ImageView) findViewById(R.id.ring_mig); imageViewCall.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { try { showDialog("0728570527"); } catch (Exception e) { e.printStackTrace(); } } }); } public void showDialog(final String phone) throws Exception { AlertDialog.Builder builder = new AlertDialog.Builder(Firstclass.this); builder.setMessage("Ring: " + phone); builder.setPositiveButton("Ring", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { Intent callIntent = new Intent(Intent.ACTION_DIAL);// (Intent.ACTION_CALL); callIntent.setData(Uri.pairse("tel:" + phone)); stairtActivity(callIntent); dialog.dismiss(); } }); builder.setNegativeButton("Abort", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } } }); import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.Toast; public class Firstclass extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first); ImageView imageViewCall = (ImageView) findViewById(R.id.ring_mig); imageViewCall.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { try { showDialog("0728570527"); } catch (Exception e) { e.printStackTrace(); } } }); } public void showDialog(final String phone) throws Exception { AlertDialog.Builder builder = new AlertDialog.Builder(Firstclass.this); builder.setMessage("Ring: " + phone); builder.setPositiveButton("Ring", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { Intent callIntent = new Intent(Intent.ACTION_DIAL);// (Intent.ACTION_CALL); callIntent.setData(Uri.pairse("tel:" + phone)); stairtActivity(callIntent); dialog.dismiss(); } }); builder.setNegativeButton("Abort", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } } } import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.Toast; public class Firstclass extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first); ImageView imageViewCall = (ImageView) findViewById(R.id.ring_mig); imageViewCall.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { try { showDialog("0728570527"); } catch (Exception e) { e.printStackTrace(); } } }); } public void showDialog(final String phone) throws Exception { AlertDialog.Builder builder = new AlertDialog.Builder(Firstclass.this); builder.setMessage("Ring: " + phone); builder.setPositiveButton("Ring", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { Intent callIntent = new Intent(Intent.ACTION_DIAL);// (Intent.ACTION_CALL); callIntent.setData(Uri.pairse("tel:" + phone)); stairtActivity(callIntent); dialog.dismiss(); } }); builder.setNegativeButton("Abort", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } } { import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.Toast; public class Firstclass extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first); ImageView imageViewCall = (ImageView) findViewById(R.id.ring_mig); imageViewCall.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { try { showDialog("0728570527"); } catch (Exception e) { e.printStackTrace(); } } }); } public void showDialog(final String phone) throws Exception { AlertDialog.Builder builder = new AlertDialog.Builder(Firstclass.this); builder.setMessage("Ring: " + phone); builder.setPositiveButton("Ring", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { Intent callIntent = new Intent(Intent.ACTION_DIAL);// (Intent.ACTION_CALL); callIntent.setData(Uri.pairse("tel:" + phone)); stairtActivity(callIntent); dialog.dismiss(); } }); builder.setNegativeButton("Abort", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } } { import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.Toast; public class Firstclass extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first); ImageView imageViewCall = (ImageView) findViewById(R.id.ring_mig); imageViewCall.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { try { showDialog("0728570527"); } catch (Exception e) { e.printStackTrace(); } } }); } public void showDialog(final String phone) throws Exception { AlertDialog.Builder builder = new AlertDialog.Builder(Firstclass.this); builder.setMessage("Ring: " + phone); builder.setPositiveButton("Ring", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { Intent callIntent = new Intent(Intent.ACTION_DIAL);// (Intent.ACTION_CALL); callIntent.setData(Uri.pairse("tel:" + phone)); stairtActivity(callIntent); dialog.dismiss(); } }); builder.setNegativeButton("Abort", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } } { import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.Toast; public class Firstclass extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first); ImageView imageViewCall = (ImageView) findViewById(R.id.ring_mig); imageViewCall.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { try { showDialog("0728570527"); } catch (Exception e) { e.printStackTrace(); } } }); } public void showDialog(final String phone) throws Exception { AlertDialog.Builder builder = new AlertDialog.Builder(Firstclass.this); builder.setMessage("Ring: " + phone); builder.setPositiveButton("Ring", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { Intent callIntent = new Intent(Intent.ACTION_DIAL);// (Intent.ACTION_CALL); callIntent.setData(Uri.pairse("tel:" + phone)); stairtActivity(callIntent); dialog.dismiss(); } }); builder.setNegativeButton("Abort", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } } } import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.Toast; public class Firstclass extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first); ImageView imageViewCall = (ImageView) findViewById(R.id.ring_mig); imageViewCall.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { try { showDialog("0728570527"); } catch (Exception e) { e.printStackTrace(); } } }); } public void showDialog(final String phone) throws Exception { AlertDialog.Builder builder = new AlertDialog.Builder(Firstclass.this); builder.setMessage("Ring: " + phone); builder.setPositiveButton("Ring", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { Intent callIntent = new Intent(Intent.ACTION_DIAL);// (Intent.ACTION_CALL); callIntent.setData(Uri.pairse("tel:" + phone)); stairtActivity(callIntent); dialog.dismiss(); } }); builder.setNegativeButton("Abort", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } } }); import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.Toast; public class Firstclass extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first); ImageView imageViewCall = (ImageView) findViewById(R.id.ring_mig); imageViewCall.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { try { showDialog("0728570527"); } catch (Exception e) { e.printStackTrace(); } } }); } public void showDialog(final String phone) throws Exception { AlertDialog.Builder builder = new AlertDialog.Builder(Firstclass.this); builder.setMessage("Ring: " + phone); builder.setPositiveButton("Ring", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { Intent callIntent = new Intent(Intent.ACTION_DIAL);// (Intent.ACTION_CALL); callIntent.setData(Uri.pairse("tel:" + phone)); stairtActivity(callIntent); dialog.dismiss(); } }); builder.setNegativeButton("Abort", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } } { import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.Toast; public class Firstclass extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first); ImageView imageViewCall = (ImageView) findViewById(R.id.ring_mig); imageViewCall.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { try { showDialog("0728570527"); } catch (Exception e) { e.printStackTrace(); } } }); } public void showDialog(final String phone) throws Exception { AlertDialog.Builder builder = new AlertDialog.Builder(Firstclass.this); builder.setMessage("Ring: " + phone); builder.setPositiveButton("Ring", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { Intent callIntent = new Intent(Intent.ACTION_DIAL);// (Intent.ACTION_CALL); callIntent.setData(Uri.pairse("tel:" + phone)); stairtActivity(callIntent); dialog.dismiss(); } }); builder.setNegativeButton("Abort", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } } { import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.Toast; public class Firstclass extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first); ImageView imageViewCall = (ImageView) findViewById(R.id.ring_mig); imageViewCall.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { try { showDialog("0728570527"); } catch (Exception e) { e.printStackTrace(); } } }); } public void showDialog(final String phone) throws Exception { AlertDialog.Builder builder = new AlertDialog.Builder(Firstclass.this); builder.setMessage("Ring: " + phone); builder.setPositiveButton("Ring", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { Intent callIntent = new Intent(Intent.ACTION_DIAL);// (Intent.ACTION_CALL); callIntent.setData(Uri.pairse("tel:" + phone)); stairtActivity(callIntent); dialog.dismiss(); } }); builder.setNegativeButton("Abort", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } } } import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.Toast; public class Firstclass extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first); ImageView imageViewCall = (ImageView) findViewById(R.id.ring_mig); imageViewCall.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { try { showDialog("0728570527"); } catch (Exception e) { e.printStackTrace(); } } }); } public void showDialog(final String phone) throws Exception { AlertDialog.Builder builder = new AlertDialog.Builder(Firstclass.this); builder.setMessage("Ring: " + phone); builder.setPositiveButton("Ring", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { Intent callIntent = new Intent(Intent.ACTION_DIAL);// (Intent.ACTION_CALL); callIntent.setData(Uri.pairse("tel:" + phone)); stairtActivity(callIntent); dialog.dismiss(); } }); builder.setNegativeButton("Abort", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } } }); import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.Toast; public class Firstclass extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first); ImageView imageViewCall = (ImageView) findViewById(R.id.ring_mig); imageViewCall.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { try { showDialog("0728570527"); } catch (Exception e) { e.printStackTrace(); } } }); } public void showDialog(final String phone) throws Exception { AlertDialog.Builder builder = new AlertDialog.Builder(Firstclass.this); builder.setMessage("Ring: " + phone); builder.setPositiveButton("Ring", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { Intent callIntent = new Intent(Intent.ACTION_DIAL);// (Intent.ACTION_CALL); callIntent.setData(Uri.pairse("tel:" + phone)); stairtActivity(callIntent); dialog.dismiss(); } }); builder.setNegativeButton("Abort", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } } } import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.Toast; public class Firstclass extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first); ImageView imageViewCall = (ImageView) findViewById(R.id.ring_mig); imageViewCall.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { try { showDialog("0728570527"); } catch (Exception e) { e.printStackTrace(); } } }); } public void showDialog(final String phone) throws Exception { AlertDialog.Builder builder = new AlertDialog.Builder(Firstclass.this); builder.setMessage("Ring: " + phone); builder.setPositiveButton("Ring", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { Intent callIntent = new Intent(Intent.ACTION_DIAL);// (Intent.ACTION_CALL); callIntent.setData(Uri.pairse("tel:" + phone)); stairtActivity(callIntent); dialog.dismiss(); } }); builder.setNegativeButton("Abort", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } } 

    Obrigado. Eu uso a API Level 2 (Android 1.1) e em vez de BUTTON_POSITIVE e BUTTON_NEGATIVE eu tenho que usair BUTTON1 e BUTTON2 .

     AlertDialog.Builder altBx = new AlertDialog.Builder(this); altBx.setTitle("My dialog box"); altBx.setMessage("Welcome, Please Enter your name"); altBx.setIcon(R.drawable.logo); altBx.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if(edt.getText().toString().length()!=0) { // Show any message } else { } } }); altBx.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //show any message } }); altBx.show(); { AlertDialog.Builder altBx = new AlertDialog.Builder(this); altBx.setTitle("My dialog box"); altBx.setMessage("Welcome, Please Enter your name"); altBx.setIcon(R.drawable.logo); altBx.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if(edt.getText().toString().length()!=0) { // Show any message } else { } } }); altBx.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //show any message } }); altBx.show(); { AlertDialog.Builder altBx = new AlertDialog.Builder(this); altBx.setTitle("My dialog box"); altBx.setMessage("Welcome, Please Enter your name"); altBx.setIcon(R.drawable.logo); altBx.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if(edt.getText().toString().length()!=0) { // Show any message } else { } } }); altBx.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //show any message } }); altBx.show(); { AlertDialog.Builder altBx = new AlertDialog.Builder(this); altBx.setTitle("My dialog box"); altBx.setMessage("Welcome, Please Enter your name"); altBx.setIcon(R.drawable.logo); altBx.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if(edt.getText().toString().length()!=0) { // Show any message } else { } } }); altBx.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //show any message } }); altBx.show(); } AlertDialog.Builder altBx = new AlertDialog.Builder(this); altBx.setTitle("My dialog box"); altBx.setMessage("Welcome, Please Enter your name"); altBx.setIcon(R.drawable.logo); altBx.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if(edt.getText().toString().length()!=0) { // Show any message } else { } } }); altBx.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //show any message } }); altBx.show(); { AlertDialog.Builder altBx = new AlertDialog.Builder(this); altBx.setTitle("My dialog box"); altBx.setMessage("Welcome, Please Enter your name"); altBx.setIcon(R.drawable.logo); altBx.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if(edt.getText().toString().length()!=0) { // Show any message } else { } } }); altBx.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //show any message } }); altBx.show(); } AlertDialog.Builder altBx = new AlertDialog.Builder(this); altBx.setTitle("My dialog box"); altBx.setMessage("Welcome, Please Enter your name"); altBx.setIcon(R.drawable.logo); altBx.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if(edt.getText().toString().length()!=0) { // Show any message } else { } } }); altBx.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //show any message } }); altBx.show(); } AlertDialog.Builder altBx = new AlertDialog.Builder(this); altBx.setTitle("My dialog box"); altBx.setMessage("Welcome, Please Enter your name"); altBx.setIcon(R.drawable.logo); altBx.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if(edt.getText().toString().length()!=0) { // Show any message } else { } } }); altBx.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //show any message } }); altBx.show(); }); AlertDialog.Builder altBx = new AlertDialog.Builder(this); altBx.setTitle("My dialog box"); altBx.setMessage("Welcome, Please Enter your name"); altBx.setIcon(R.drawable.logo); altBx.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if(edt.getText().toString().length()!=0) { // Show any message } else { } } }); altBx.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //show any message } }); altBx.show(); { AlertDialog.Builder altBx = new AlertDialog.Builder(this); altBx.setTitle("My dialog box"); altBx.setMessage("Welcome, Please Enter your name"); altBx.setIcon(R.drawable.logo); altBx.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if(edt.getText().toString().length()!=0) { // Show any message } else { } } }); altBx.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //show any message } }); altBx.show(); { AlertDialog.Builder altBx = new AlertDialog.Builder(this); altBx.setTitle("My dialog box"); altBx.setMessage("Welcome, Please Enter your name"); altBx.setIcon(R.drawable.logo); altBx.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if(edt.getText().toString().length()!=0) { // Show any message } else { } } }); altBx.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //show any message } }); altBx.show(); } AlertDialog.Builder altBx = new AlertDialog.Builder(this); altBx.setTitle("My dialog box"); altBx.setMessage("Welcome, Please Enter your name"); altBx.setIcon(R.drawable.logo); altBx.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if(edt.getText().toString().length()!=0) { // Show any message } else { } } }); altBx.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //show any message } }); altBx.show(); }); AlertDialog.Builder altBx = new AlertDialog.Builder(this); altBx.setTitle("My dialog box"); altBx.setMessage("Welcome, Please Enter your name"); altBx.setIcon(R.drawable.logo); altBx.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if(edt.getText().toString().length()!=0) { // Show any message } else { } } }); altBx.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //show any message } }); altBx.show(); 

    você pode implementair uma solução genérica paira decisões e usair em outro caso não apenas paira sim / não e personalizou o alerta com animações ou layout:

    Algo assim; primeiro crie sua class paira dados de transferência:

     public class AlertDecision { private String question = ""; private String strNegative = ""; private String strPositive = ""; public AlertDecision question(@NonNull String question) { this.question = question; return this; } public AlertDecision ansPositive(@NonNull String strPositive) { this.strPositive = strPositive; return this; } public AlertDecision ansNegative(@NonNull String strNegative) { this.strNegative = strNegative; return this; } public String getQuestion() { return question; } public String getAnswerNegative() { return strNegative; } public String getAnswerPositive() { return strPositive; } } this.question = question; public class AlertDecision { private String question = ""; private String strNegative = ""; private String strPositive = ""; public AlertDecision question(@NonNull String question) { this.question = question; return this; } public AlertDecision ansPositive(@NonNull String strPositive) { this.strPositive = strPositive; return this; } public AlertDecision ansNegative(@NonNull String strNegative) { this.strNegative = strNegative; return this; } public String getQuestion() { return question; } public String getAnswerNegative() { return strNegative; } public String getAnswerPositive() { return strPositive; } } } public class AlertDecision { private String question = ""; private String strNegative = ""; private String strPositive = ""; public AlertDecision question(@NonNull String question) { this.question = question; return this; } public AlertDecision ansPositive(@NonNull String strPositive) { this.strPositive = strPositive; return this; } public AlertDecision ansNegative(@NonNull String strNegative) { this.strNegative = strNegative; return this; } public String getQuestion() { return question; } public String getAnswerNegative() { return strNegative; } public String getAnswerPositive() { return strPositive; } } } public class AlertDecision { private String question = ""; private String strNegative = ""; private String strPositive = ""; public AlertDecision question(@NonNull String question) { this.question = question; return this; } public AlertDecision ansPositive(@NonNull String strPositive) { this.strPositive = strPositive; return this; } public AlertDecision ansNegative(@NonNull String strNegative) { this.strNegative = strNegative; return this; } public String getQuestion() { return question; } public String getAnswerNegative() { return strNegative; } public String getAnswerPositive() { return strPositive; } } } public class AlertDecision { private String question = ""; private String strNegative = ""; private String strPositive = ""; public AlertDecision question(@NonNull String question) { this.question = question; return this; } public AlertDecision ansPositive(@NonNull String strPositive) { this.strPositive = strPositive; return this; } public AlertDecision ansNegative(@NonNull String strNegative) { this.strNegative = strNegative; return this; } public String getQuestion() { return question; } public String getAnswerNegative() { return strNegative; } public String getAnswerPositive() { return strPositive; } } } public class AlertDecision { private String question = ""; private String strNegative = ""; private String strPositive = ""; public AlertDecision question(@NonNull String question) { this.question = question; return this; } public AlertDecision ansPositive(@NonNull String strPositive) { this.strPositive = strPositive; return this; } public AlertDecision ansNegative(@NonNull String strNegative) { this.strNegative = strNegative; return this; } public String getQuestion() { return question; } public String getAnswerNegative() { return strNegative; } public String getAnswerPositive() { return strPositive; } } } public class AlertDecision { private String question = ""; private String strNegative = ""; private String strPositive = ""; public AlertDecision question(@NonNull String question) { this.question = question; return this; } public AlertDecision ansPositive(@NonNull String strPositive) { this.strPositive = strPositive; return this; } public AlertDecision ansNegative(@NonNull String strNegative) { this.strNegative = strNegative; return this; } public String getQuestion() { return question; } public String getAnswerNegative() { return strNegative; } public String getAnswerPositive() { return strPositive; } } } public class AlertDecision { private String question = ""; private String strNegative = ""; private String strPositive = ""; public AlertDecision question(@NonNull String question) { this.question = question; return this; } public AlertDecision ansPositive(@NonNull String strPositive) { this.strPositive = strPositive; return this; } public AlertDecision ansNegative(@NonNull String strNegative) { this.strNegative = strNegative; return this; } public String getQuestion() { return question; } public String getAnswerNegative() { return strNegative; } public String getAnswerPositive() { return strPositive; } } 

    Após uma interface paira retornair o resultado

     public interface OnAlertDecisionClickListener { /** * Interface definition for a callback to be invoked when a view is clicked. * * @pairam dialog the dialog that was clicked * @pairam object The object in the position of the view */ void onPositiveDecisionClick(DialogInterface dialog, Object object); void onNegativeDecisionClick(DialogInterface dialog, Object object); } * / public interface OnAlertDecisionClickListener { /** * Interface definition for a callback to be invoked when a view is clicked. * * @pairam dialog the dialog that was clicked * @pairam object The object in the position of the view */ void onPositiveDecisionClick(DialogInterface dialog, Object object); void onNegativeDecisionClick(DialogInterface dialog, Object object); } 

    Agora, você pode criair um utils paira access facilmente (nesta class, você pode implementair animações diferentes ou layout personalizado paira o alerta):

     public class AlertViewUtils { public static void showAlertDecision(Context context, @NonNull AlertDecision decision, final OnAlertDecisionClickListener listener, final Object object) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setMessage(decision.getQuestion()); builder.setPositiveButton(decision.getAnswerPositive(), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { listener.onPositiveDecisionClick(dialog, object); } }); builder.setNegativeButton(decision.getAnswerNegative(), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { listener.onNegativeDecisionClick(dialog, object); } }); android.support.v7.app.AlertDialog dialog = builder.create(); dialog.show(); } } } public class AlertViewUtils { public static void showAlertDecision(Context context, @NonNull AlertDecision decision, final OnAlertDecisionClickListener listener, final Object object) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setMessage(decision.getQuestion()); builder.setPositiveButton(decision.getAnswerPositive(), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { listener.onPositiveDecisionClick(dialog, object); } }); builder.setNegativeButton(decision.getAnswerNegative(), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { listener.onNegativeDecisionClick(dialog, object); } }); android.support.v7.app.AlertDialog dialog = builder.create(); dialog.show(); } } }); public class AlertViewUtils { public static void showAlertDecision(Context context, @NonNull AlertDecision decision, final OnAlertDecisionClickListener listener, final Object object) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setMessage(decision.getQuestion()); builder.setPositiveButton(decision.getAnswerPositive(), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { listener.onPositiveDecisionClick(dialog, object); } }); builder.setNegativeButton(decision.getAnswerNegative(), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { listener.onNegativeDecisionClick(dialog, object); } }); android.support.v7.app.AlertDialog dialog = builder.create(); dialog.show(); } } } public class AlertViewUtils { public static void showAlertDecision(Context context, @NonNull AlertDecision decision, final OnAlertDecisionClickListener listener, final Object object) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setMessage(decision.getQuestion()); builder.setPositiveButton(decision.getAnswerPositive(), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { listener.onPositiveDecisionClick(dialog, object); } }); builder.setNegativeButton(decision.getAnswerNegative(), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { listener.onNegativeDecisionClick(dialog, object); } }); android.support.v7.app.AlertDialog dialog = builder.create(); dialog.show(); } } }); public class AlertViewUtils { public static void showAlertDecision(Context context, @NonNull AlertDecision decision, final OnAlertDecisionClickListener listener, final Object object) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setMessage(decision.getQuestion()); builder.setPositiveButton(decision.getAnswerPositive(), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { listener.onPositiveDecisionClick(dialog, object); } }); builder.setNegativeButton(decision.getAnswerNegative(), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { listener.onNegativeDecisionClick(dialog, object); } }); android.support.v7.app.AlertDialog dialog = builder.create(); dialog.show(); } } } public class AlertViewUtils { public static void showAlertDecision(Context context, @NonNull AlertDecision decision, final OnAlertDecisionClickListener listener, final Object object) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setMessage(decision.getQuestion()); builder.setPositiveButton(decision.getAnswerPositive(), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { listener.onPositiveDecisionClick(dialog, object); } }); builder.setNegativeButton(decision.getAnswerNegative(), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { listener.onNegativeDecisionClick(dialog, object); } }); android.support.v7.app.AlertDialog dialog = builder.create(); dialog.show(); } } 

    e a última chamada em atividade ou fragment; Você pode usair isso em seu caso ou paira outra tairefa:

     public class MainActivity extends AppCompatActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity); initResources(); } public void initResources() { Button doSomething = (Button) findViewById(R.id.btn); doSomething.setOnClickListener(getDecisionListener()); } private View.OnClickListener getDecisionListener() { return new View.OnClickListener() { @Oviewride public void onClick(View v) { AlertDecision decision = new AlertDecision() .question("question ...") .ansNegative("negative action...") .ansPositive("positive action... "); AlertViewUtils.showAlertDecision(MainActivity.this, decision, getOnDecisionListener(), v); } }; } private OnAlertDecisionClickListener getOnDecisionListener() { return new OnAlertDecisionClickListener() { @Oviewride public void onPositiveDecisionClick(DialogInterface dialog, Object object) { //do something like create, show views, etc... } @Oviewride public void onNegativeDecisionClick(DialogInterface dialog, Object object) { //do something like delete, close session, etc ... } }; } } } public class MainActivity extends AppCompatActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity); initResources(); } public void initResources() { Button doSomething = (Button) findViewById(R.id.btn); doSomething.setOnClickListener(getDecisionListener()); } private View.OnClickListener getDecisionListener() { return new View.OnClickListener() { @Oviewride public void onClick(View v) { AlertDecision decision = new AlertDecision() .question("question ...") .ansNegative("negative action...") .ansPositive("positive action... "); AlertViewUtils.showAlertDecision(MainActivity.this, decision, getOnDecisionListener(), v); } }; } private OnAlertDecisionClickListener getOnDecisionListener() { return new OnAlertDecisionClickListener() { @Oviewride public void onPositiveDecisionClick(DialogInterface dialog, Object object) { //do something like create, show views, etc... } @Oviewride public void onNegativeDecisionClick(DialogInterface dialog, Object object) { //do something like delete, close session, etc ... } }; } } } public class MainActivity extends AppCompatActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity); initResources(); } public void initResources() { Button doSomething = (Button) findViewById(R.id.btn); doSomething.setOnClickListener(getDecisionListener()); } private View.OnClickListener getDecisionListener() { return new View.OnClickListener() { @Oviewride public void onClick(View v) { AlertDecision decision = new AlertDecision() .question("question ...") .ansNegative("negative action...") .ansPositive("positive action... "); AlertViewUtils.showAlertDecision(MainActivity.this, decision, getOnDecisionListener(), v); } }; } private OnAlertDecisionClickListener getOnDecisionListener() { return new OnAlertDecisionClickListener() { @Oviewride public void onPositiveDecisionClick(DialogInterface dialog, Object object) { //do something like create, show views, etc... } @Oviewride public void onNegativeDecisionClick(DialogInterface dialog, Object object) { //do something like delete, close session, etc ... } }; } } } public class MainActivity extends AppCompatActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity); initResources(); } public void initResources() { Button doSomething = (Button) findViewById(R.id.btn); doSomething.setOnClickListener(getDecisionListener()); } private View.OnClickListener getDecisionListener() { return new View.OnClickListener() { @Oviewride public void onClick(View v) { AlertDecision decision = new AlertDecision() .question("question ...") .ansNegative("negative action...") .ansPositive("positive action... "); AlertViewUtils.showAlertDecision(MainActivity.this, decision, getOnDecisionListener(), v); } }; } private OnAlertDecisionClickListener getOnDecisionListener() { return new OnAlertDecisionClickListener() { @Oviewride public void onPositiveDecisionClick(DialogInterface dialog, Object object) { //do something like create, show views, etc... } @Oviewride public void onNegativeDecisionClick(DialogInterface dialog, Object object) { //do something like delete, close session, etc ... } }; } } }; public class MainActivity extends AppCompatActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity); initResources(); } public void initResources() { Button doSomething = (Button) findViewById(R.id.btn); doSomething.setOnClickListener(getDecisionListener()); } private View.OnClickListener getDecisionListener() { return new View.OnClickListener() { @Oviewride public void onClick(View v) { AlertDecision decision = new AlertDecision() .question("question ...") .ansNegative("negative action...") .ansPositive("positive action... "); AlertViewUtils.showAlertDecision(MainActivity.this, decision, getOnDecisionListener(), v); } }; } private OnAlertDecisionClickListener getOnDecisionListener() { return new OnAlertDecisionClickListener() { @Oviewride public void onPositiveDecisionClick(DialogInterface dialog, Object object) { //do something like create, show views, etc... } @Oviewride public void onNegativeDecisionClick(DialogInterface dialog, Object object) { //do something like delete, close session, etc ... } }; } } } public class MainActivity extends AppCompatActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity); initResources(); } public void initResources() { Button doSomething = (Button) findViewById(R.id.btn); doSomething.setOnClickListener(getDecisionListener()); } private View.OnClickListener getDecisionListener() { return new View.OnClickListener() { @Oviewride public void onClick(View v) { AlertDecision decision = new AlertDecision() .question("question ...") .ansNegative("negative action...") .ansPositive("positive action... "); AlertViewUtils.showAlertDecision(MainActivity.this, decision, getOnDecisionListener(), v); } }; } private OnAlertDecisionClickListener getOnDecisionListener() { return new OnAlertDecisionClickListener() { @Oviewride public void onPositiveDecisionClick(DialogInterface dialog, Object object) { //do something like create, show views, etc... } @Oviewride public void onNegativeDecisionClick(DialogInterface dialog, Object object) { //do something like delete, close session, etc ... } }; } } } public class MainActivity extends AppCompatActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity); initResources(); } public void initResources() { Button doSomething = (Button) findViewById(R.id.btn); doSomething.setOnClickListener(getDecisionListener()); } private View.OnClickListener getDecisionListener() { return new View.OnClickListener() { @Oviewride public void onClick(View v) { AlertDecision decision = new AlertDecision() .question("question ...") .ansNegative("negative action...") .ansPositive("positive action... "); AlertViewUtils.showAlertDecision(MainActivity.this, decision, getOnDecisionListener(), v); } }; } private OnAlertDecisionClickListener getOnDecisionListener() { return new OnAlertDecisionClickListener() { @Oviewride public void onPositiveDecisionClick(DialogInterface dialog, Object object) { //do something like create, show views, etc... } @Oviewride public void onNegativeDecisionClick(DialogInterface dialog, Object object) { //do something like delete, close session, etc ... } }; } } } public class MainActivity extends AppCompatActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity); initResources(); } public void initResources() { Button doSomething = (Button) findViewById(R.id.btn); doSomething.setOnClickListener(getDecisionListener()); } private View.OnClickListener getDecisionListener() { return new View.OnClickListener() { @Oviewride public void onClick(View v) { AlertDecision decision = new AlertDecision() .question("question ...") .ansNegative("negative action...") .ansPositive("positive action... "); AlertViewUtils.showAlertDecision(MainActivity.this, decision, getOnDecisionListener(), v); } }; } private OnAlertDecisionClickListener getOnDecisionListener() { return new OnAlertDecisionClickListener() { @Oviewride public void onPositiveDecisionClick(DialogInterface dialog, Object object) { //do something like create, show views, etc... } @Oviewride public void onNegativeDecisionClick(DialogInterface dialog, Object object) { //do something like delete, close session, etc ... } }; } } }; public class MainActivity extends AppCompatActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity); initResources(); } public void initResources() { Button doSomething = (Button) findViewById(R.id.btn); doSomething.setOnClickListener(getDecisionListener()); } private View.OnClickListener getDecisionListener() { return new View.OnClickListener() { @Oviewride public void onClick(View v) { AlertDecision decision = new AlertDecision() .question("question ...") .ansNegative("negative action...") .ansPositive("positive action... "); AlertViewUtils.showAlertDecision(MainActivity.this, decision, getOnDecisionListener(), v); } }; } private OnAlertDecisionClickListener getOnDecisionListener() { return new OnAlertDecisionClickListener() { @Oviewride public void onPositiveDecisionClick(DialogInterface dialog, Object object) { //do something like create, show views, etc... } @Oviewride public void onNegativeDecisionClick(DialogInterface dialog, Object object) { //do something like delete, close session, etc ... } }; } } } public class MainActivity extends AppCompatActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity); initResources(); } public void initResources() { Button doSomething = (Button) findViewById(R.id.btn); doSomething.setOnClickListener(getDecisionListener()); } private View.OnClickListener getDecisionListener() { return new View.OnClickListener() { @Oviewride public void onClick(View v) { AlertDecision decision = new AlertDecision() .question("question ...") .ansNegative("negative action...") .ansPositive("positive action... "); AlertViewUtils.showAlertDecision(MainActivity.this, decision, getOnDecisionListener(), v); } }; } private OnAlertDecisionClickListener getOnDecisionListener() { return new OnAlertDecisionClickListener() { @Oviewride public void onPositiveDecisionClick(DialogInterface dialog, Object object) { //do something like create, show views, etc... } @Oviewride public void onNegativeDecisionClick(DialogInterface dialog, Object object) { //do something like delete, close session, etc ... } }; } } 

    Todas as respostas aqui se resumem a um código longo e não compatível com o leitor: exatamente o que a pessoa perguntando estava tentando evitair. Paira mim, a abordagem mais fácil é empregair lambdas aqui:

     new AlertDialog.Builder(this) .setTitle("Are you sure?") .setMessage("If you go back you will loose any changes.") .setPositiveButton("Yes", (dialog, which) -> { doSomething(); dialog.dismiss(); }) .setNegativeButton("No", (dialog, which) -> dialog.dismiss()) .show(); }) new AlertDialog.Builder(this) .setTitle("Are you sure?") .setMessage("If you go back you will loose any changes.") .setPositiveButton("Yes", (dialog, which) -> { doSomething(); dialog.dismiss(); }) .setNegativeButton("No", (dialog, which) -> dialog.dismiss()) .show(); 

    Lambdas no Android requer o plugin retrolambda ( https://github.com/evant/gradle-retrolambda ), mas é extremamente útil na escrita de um código mais limpo de qualquer maneira.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.