android - 如何在android中获取DialogResult?

标签 android

我想创建一个带有两个按钮"is"和“否”的 DialogAlert。我想在 bool 变量中捕获对话框的结果。假设用户单击"is"按钮,那么 DialogResult 应该返回一个真值,如果用户单击“否”按钮,那么 DialogResult 应该返回一个假值。请帮我解决这个问题。提前致谢。

最佳答案

我会使用 AlertDialog ( see documentation here )。如果您有自己的 DialogResult 类,代码可能如下所示:

DialogResult result = new DialogResult(); 
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
   .setCancelable(false)
   .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            // User clicked Yes: set the value to your result class here. E.g:
            MyActivity.result.setValue(true);
       }
   })
   .setNegativeButton("No", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            // User clicked No: set the value to your result class here. E.g: 
            MyActivity.result.setValue(false);
       }
   });
AlertDialog alert = builder.create();

关于android - 如何在android中获取DialogResult?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5945908/

相关文章:

Android 应用程序兼容性 21 : All my textview are white

android - 在 OS X 中开始 OpenGL 3.0+ 编程(再次),针对多个平台

java - 确保嵌入的字体资源已加载到 Android 上

java - 名称类需要完整路径,仅使用类名无法找到 - android

android - sleep 后关闭锁定并打开屏幕

android - 为 Android 应用程序重置 TextView 在 Listview 行中的位置

Android Edittext 验证所有数字

Android HelloWorld 应用程序在执行第二个 Activity 时崩溃

android - 为什么我的 ionic 模式不能在 android 4.4 设备上打开?

java - 滚动 Android 时 ListView 中的复选框不保存状态