我是一个新的 Android 开发者,有一个可能很愚蠢的问题。
在我的活动创建中,我想制作一个弹出对话框来提示用户输入。然后我想获取该输入并将其保存为全局变量以供以后使用。目前我有
public class MgenActivity extends Activity {
// Instance Variables
String ip = "";
/**
* On Create
*/
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
String box = dialogBoxStart();
this.ip = box;
//more code
}
public String dialogBoxStart()
{
String returned = "";
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("MGEN Setup");
alert.setMessage("Please enter the MGEN IP address such as \n 9.42.68.69");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
//send user input to global?
//this doesnt work: this.ip = input.getText().toString();
}
};
alert.setPositiveButton("Ok", listener);
alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
returned = input.getText().toString();
alert.show();
return returned;
}
问题是我的 EditText 'input' 没有保存用户输入,所以我不能把它保存在外面。同样,我无法从我的 onClick 方法中与我的外部变量交谈。
所以 TLDR:你如何保存来自对话框的输入?