如何在Android上显示“是/否”对话框?

是的,我知道有AlertDialog.Builder,但是我很震惊地知道在Android中显示对话有多困难(至less不是程序员友好的)。

我曾经是一个.NET开发人员,我想知道是否有任何与Android相当的以下内容?

if (MessageBox.Show("Sure?", "", MessageBoxButtons.YesNo) == DialogResult.Yes){ // Do something... } 

AlertDialog.Builder真的不是很难使用。 一开始肯定有点吓人,但是一旦你使用了它,它既简单又强大。 我知道你已经说过你知道如何使用它,但是这里只是一个简单的例子:

 DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { @Override 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

如果您是从View.OnClickListener创build对话框,则可以使用view.getContext()来获取上下文。 或者,您可以使用yourFragmentName.getActivity()

尝试这个:

 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() { @Override public void onClick(DialogInterface dialog, int which) { // Do nothing dialog.dismiss(); } }); AlertDialog alert = builder.create(); alert.show(); 

Steve H的答案很有用,但是有更多的信息:对话框的工作方式是因为Android中的对话框是asynchronous的(当对话框显示时,对话框不会停止)。 因此,您必须使用callback来处理用户的select。

看看这个问题,在Android和.NET的差异(因为它涉及到对话框)之间的更长的讨论: Dialogs / AlertDialogs:如何在对话框启动时“阻止执行”(.NET风格)

这对我有用:

 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() { @Override public void onClick(DialogInterface dialog, int which) { // Do nothing dialog.dismiss(); } }); AlertDialog alert = builder.create(); alert.show(); 

史蒂夫的回答是正确的,虽然过时的碎片。 这是一个FragmentDialog的例子。

class上:

 public class SomeDialog extends DialogFragment { @Override 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() { @Override public void onClick(DialogInterface dialog, int which) { // do nothing (will close dialog) } }) .setPositiveButton(android.R.string.yes, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // do something } }) .create(); } } 

要开始对话:

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

您也可以让类实现onClickListener并使用它来代替embedded式侦听器。

感谢nikki你的答案帮助我改进了一个现有的简单的添加我想要的行动如下

 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() { @Override public void onClick(DialogInterface dialog, int which) { // I do not need any action here you might dialog.dismiss(); } }); AlertDialog alert = builder.create(); alert.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 { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first); ImageView imageViewCall = (ImageView) findViewById(R.id.ring_mig); imageViewCall.setOnClickListener(new OnClickListener() { @Override 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() { @Override public void onClick(DialogInterface dialog, int which) { Intent callIntent = new Intent(Intent.ACTION_DIAL);// (Intent.ACTION_CALL); callIntent.setData(Uri.parse("tel:" + phone)); startActivity(callIntent); dialog.dismiss(); } }); builder.setNegativeButton("Abort", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } } 

谢谢。 我使用API​​ Level 2(Android 1.1),而不是BUTTON_POSITIVEBUTTON_NEGATIVE我必须使用BUTTON1BUTTON2

 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(); 

你可以实现一个通用的决策解决scheme,并在另一个案例中使用,不仅适用于“是/否”,还可以自定义带有animation或布局的警报:

像这样的东西; 首先创build你的class级传输数据:

 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 interface OnAlertDecisionClickListener { /** * Interface definition for a callback to be invoked when a view is clicked. * * @param dialog the dialog that was clicked * @param object The object in the position of the view */ void onPositiveDecisionClick(DialogInterface dialog, Object object); void onNegativeDecisionClick(DialogInterface dialog, Object object); } 

现在你可以创build一个工具来轻松访问(在这个类中你可以实现不同的animation或自定义布局):

 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 MainActivity extends AppCompatActivity { @Override 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() { @Override 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() { @Override public void onPositiveDecisionClick(DialogInterface dialog, Object object) { //do something like create, show views, etc... } @Override public void onNegativeDecisionClick(DialogInterface dialog, Object object) { //do something like delete, close session, etc ... } }; } } 

这里的所有答案都归结为冗长的,而不是读者友好的代码:这个人所要求的是试图避免的。 对我来说,最简单的方法就是在这里使用lambdas:

 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(); 

Android中的Lambdas需要使用retrolambda插件( https://github.com/evant/gradle-retrolambda ),但是无论如何,这对于编写更简洁的代码非常有帮助。