我有一個在xml文件中定義的視圖。它包含兩個Edittext字段(其他東西如文本)Android - 從alertbuilder對話框中檢索文本輸入
我使用AlertBuilder觸發用戶在兩個edittext字段中輸入文本(如用戶名和密碼)的對話框。當我嘗試檢索字符串並將它們發送給Login()時,兩個字符串都只是空值。到底是怎麼回事?
它似乎不知道字符串數據沒有保存?
這裏是當我展示的對話框中我的應用程序:
SignInDialog.show(ScreenMain.this,
"Login",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
LayoutInflater inflater = (LayoutInflater) ScreenMain.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.screen_dialog_login, null);
LogIn(((EditText) layout.findViewById(R.id.screen_dialog_login_username_edit)).getText().toString(),
((EditText) layout.findViewById(R.id.screen_dialog_login_password_edit)).getText().toString());
}
},
"Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
這裏有一個類我用來實例化一個對話框:
/* login dialog*/
static class SignInDialog {
public static void show(Context context, String positiveText, DialogInterface.OnClickListener positive, String negativeText, DialogInterface.OnClickListener negative){
AlertDialog.Builder builder;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.screen_dialog_login, null);
builder = new AlertDialog.Builder(context);
builder.setView(layout);
if(positive != null && positiveText != null){
builder.setPositiveButton(positiveText, positive);
}
if(negative != null && negativeText != null){
builder.setNegativeButton(negativeText, negative);
}
builder.create().show();
}
}