android - 如何在android sdk中使用单选按钮关闭AlertDialog

标签 android

我创建了一个带有两个单选按钮的警报对话框。当用户选择一个选项时,我需要关闭警报对话框,但我无法关闭它。

 final CharSequence[] items = {"First Option", "Second Option"};

 AlertDialog.Builder builder = new AlertDialog.Builder(context);
 builder.setTitle("Choose an option"); 
 builder.setSingleChoiceItems(items, -1, new  DialogInterface.OnClickListener() { 
       public void  onClick(DialogInterface dialog, int item) {
            Toast.makeText(getApplicationContext(), items[item],  Toast.LENGTH_SHORT).show(); 
       } 
 });
 final AlertDialog alert = builder.create();
 alert.show();

有人可以帮助我如何做到这一点。

谢谢

最佳答案

请试试这个..

final CharSequence[] items = {"First Option", "Second Option"};

 AlertDialog.Builder builder = new AlertDialog.Builder(context);
 builder.setTitle("Choose an option"); 
 builder.setSingleChoiceItems(items, -1, new  DialogInterface.OnClickListener() { 
   public void  onClick(DialogInterface dialog, int item) {
        dialog.dismiss();
        Toast.makeText(getApplicationContext(), items[item],  Toast.LENGTH_SHORT).show(); 
   } 
});
final AlertDialog alert = builder.create();
alert.show();

关于android - 如何在android sdk中使用单选按钮关闭AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6421196/

相关文章:

android - 在 Android 中使用 future 日期限制日期选择器

android - Firestore Core初始化错误,没有任何作用

Android 谷歌地图 v2. getMapAsync() 不在单独的线程中运行?

android - Android 中的 notificationManager.notify 和 startForeground 有什么区别?

android - Android 上带边距的单选按钮左侧的文本

java - 即使未选中,首选项复选框也保持不变

android - 当我使用android文件 "com.android.internal.R cannot be resolved"时如何解决错误 'MultiAutoCompleteTextView.java',

android - 单击该图像时如何从 ImageView 检索图像?

android - 无法执行 dex : java. nio.BufferOverflowException。检查 Eclipse 日志以获取堆栈跟踪

android - 使用 fabric 跟踪 android 应用程序的卸载记录