How can I get the results from an AlertDialog?(如何从 AlertDialog 获取结果?)
问题描述
我正在使用 AlertDialog.Builder 显示一个对话框以提示用户输入密码,然后我想将该密码保存在首选项中,但是我不知道如何从警报对话框中获取结果输入法.
I am using an AlertDialog.Builder to display a dialog to prompt the user to enter a password, I then want to save that password in a preference, however I can't figure out how to get the result from the alert dialog's input method.
这基本上是我想做的事情:
Here is essentially what I would like to be able to do:
String result;
AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setTitle("Please enter a password");
final EditText input = new EditText(this);
b.setView(input);
b.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int whichButton)
{
//I get a compile error here, it wants result to be final.
result = input.getText().toString();
}
});
b.setNegativeButton("CANCEL", null);
b.create().show();
但是,我愿意做诸如 showDialog(int);
之类的事情,然后使用 onCreateDialog(int)
方法并以某种方式设置结果并以某种方式接收它其他方法,但我不知道如何进行最后一部分.
However, I am open to doing something such as showDialog(int);
then using the onCreateDialog(int)
method and somehow setting the result and receiving it in some other method, but I have no idea how to go about the last part.
推荐答案
public class MyActivity extends Activity {
private String result;
void showDialog() {
AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setTitle("Please enter a password");
final EditText input = new EditText(this);
b.setView(input);
b.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
result = input.getText().toString();
}
});
b.setNegativeButton("CANCEL", null);
b.show();
}
}
这篇关于如何从 AlertDialog 获取结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从 AlertDialog 获取结果?
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01