1
我的作品基本上在我的修改後的示例下面我有2個編輯框我希望兩個值都顯示在警告框中,其餘代碼工作正常,警報框確實顯示,但沒有輸入的值。如何顯示用戶在Android對話框中輸入EditText值
public class Main extends Activity {
EditText username = null;
EditText password = null;
Button login;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText usern = (EditText) findViewById(R.id.username);
EditText pass = (EditText) findViewById(R.id.password);
Button login = (Button) findViewById(R.id.login);
String usernm = usern.getText().toString();
String passnm = pass.getText().toString();
final Builder alert = new AlertDialog.Builder(this)
.setTitle("SHOW FIELDS")
.setMessage("USERNAME:" + usernm + " PASSWORD:" + passnm)
.setNegativeButton("CLOSE", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
closeContextMenu();
}
});
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alert.show();
}
});
@ user1910290:同時聲明'字符串usernm'和'字符串passnm'在類級別意味着'onCreate'之前,那麼你將能夠在整個班級訪問這些變量 –
謝謝...代碼運行,但值顯示爲空 – user1910290
@ user1910290:請參閱我的編輯答案 –