【发布时间】:2017-01-02 22:45:32
【问题描述】:
以下代码在按钮的 onClick 方法中运行,并弹出一个对话框。我可以输入文本并按确定,但我的字符串文件名(如 Log.d 中所示)始终为空。我不明白为什么。
如何获取对话框中输入的文字进行保存?
我的代码在活动中运行(无片段),字符串文件名和 EditText 输入都是类成员变量。
// Get filename from user
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Enter filename");
// EditText to get user input
this.input = new EditText(this);
dialog.setView(input);
dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
filename = input.getText().toString();
}
});
dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// cancelled
}
});
dialog.show();
Log.d(LOG_TAG, "Filename is : " + filename);
我的代码基于android prompt user's input using a dialog,但我的问题不同。
【问题讨论】:
标签: android android-edittext android-dialog