在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