問題描述
我正在使用 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 獲取結果?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!