我正在使用AlertDialog.Builder来显示对话框以提示用户输入密码,然后我希望将该密码保存在偏好设置中,但是我无法找到如何从警报对话框的输入方法中获取结果。
这是我想要实现的基本操作:
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)
方法设置结果并在某些其他方法中接收它,但我不知道如何完成最后一部分。