如何获得在Android中的DialogResult? [英] How to get the DialogResult in android?

查看:164
本文介绍了如何获得在Android中的DialogResult?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个DialogAlert有两个按钮'YES'和'NO'。我想捕捉的对话框的结果布尔变量。假设用户点击是按钮,然后DialogResult的应该返回真值,如果用户单击否按钮,然后DialogResult的应该返回False值。请帮我在做这个。先谢谢了。

I want to create a DialogAlert with two buttons 'YES' and 'NO'. I want to capture the result of the Dialog in a boolean variable. Suppose the user clicks the 'YES' button then the DialogResult Should return a true Value and if the user clicks the 'NO' button then the DialogResult should return a False value. Please help me out in doing this. Thanks in advance.

推荐答案

我会用一个AlertDialog(的看到文档这里)。如果你有自己的类的DialogResult的的code可能是这个样子:

I would use a AlertDialog (see documentation here). If you have your own class DialogResult the the code could look something like this:

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中的DialogResult?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆