我怎样才能得到一个AlertDialog的结果? [英] How can I get the results from an AlertDialog?
问题描述
我使用的是AlertDialog.Builder显示一个对话框,提示用户输入密码,然后我要保存密码在preference,但我无法弄清楚如何从结果警告对话框中的输入法。
下面基本上是什么,我希望能够做到:
字符串的结果;
AlertDialog.Builder B =新AlertDialog.Builder(本);
b.setTitle(请输入密码);
最后的EditText输入=新的EditText(本);
b.setView(输入);
b.setPositiveButton(OK,新DialogInterface.OnClickListener()
{
@覆盖
公共无效的onClick(DialogInterface对话,诠释whichButton)
{
//我在这里得到一个编译错误,它想结果为最终决定。
。结果= input.getText()的toString();
}
});
b.setNegativeButton(取消,NULL);
。b.create()显示();
不过,我愿意做的事情,如的ShowDialog(INT);
然后用 onCreateDialog(INT)
方法并以某种方式设定的结果和接收它在其他一些方法,但我不知道如何去的最后部分。
公共类胡说延伸活动{
私人字符串的结果;
//其他活动的东西
无效的ShowDialog(){
AlertDialog.Builder B =新AlertDialog.Builder(本);
b.setTitle(请输入密码);
最后的EditText输入=新的EditText(本);
b.setView(输入);
b.setPositiveButton(OK,新DialogInterface.OnClickListener()
{
@覆盖
公共无效的onClick(DialogInterface对话,诠释whichButton)
{
//现在应该可以工作
。结果= input.getText()的toString();
}
});
b.setNegativeButton(取消,NULL);
。b.create()显示();
}
}
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();
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 Blah extends Activity {
private String result;
// other activity stuff
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)
{
// SHOULD NOW WORK
result = input.getText().toString();
}
});
b.setNegativeButton("CANCEL", null);
b.create().show();
}
}
这篇关于我怎样才能得到一个AlertDialog的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!