在Android上,我试图将Edittext
转换为字符串。 toString()
方法不起作用,打印出来时playerName为空。有没有其他方法可以将edittext转换为字符串?Edittext to String
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setMessage("Your Name");
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
playerName = input.getText().toString();
}
});
alert.show();
我试图将值保存到一个变量,所以如果我在alert.show()之前声明另一个变量来保存它,那会起作用吗? – daveeloo 2011-05-10 08:28:29
一般问题是,alert.show()不会暂停调用void或function的执行。 show()的代码将立即执行,并不等待OK。如果你想使用这个变量,你可以将这个值存储到一个公共变量中,但是你只能在其他void/function中使用它,而不是在调用它的对话框中使用它(如在vb或c#中)。如果你想立即使用它,你需要在对话框的onclick方法中调用using函数,并且每次都必须处于可运行状态 – 2red13 2011-05-10 09:18:21