我對Android/Java相對較新。感謝Stack Overflow,我能從這裏提出的問題中學到很多東西。但是,我現在停留在這個問題上。顯示多個AlertDialogs
我有一個密碼輸入AlertDialog
,當我們啓動應用程序時彈出。它從EditText
中讀取密碼並將其與存儲在文件中的密碼進行比較。我需要一個額外的AlertDialog
,當出現無效/錯誤密碼時顯示。這也是我實施的。
現在,這第二個對話框有兩個按鈕 - 重置和重試。當我們點擊重試時,我想讓該活動再次顯示第一個對話框。這是我無能爲力的地方。如果任何人都可以提供工作解決方案和一點解釋,我會非常感激。
這裏是我的代碼:
LayoutInflater li = LayoutInflater.from(context);
View passView = li.inflate(R.layout.authdialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(passView);
final EditText passInput = (EditText) passView.findViewById(
R.id.editTextDialogUserInput);
final TextView txtv = (TextView) findViewById(R.id.textv);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String data = "";
try {
FileInputStream fis = openFileInput("authfile");
InputStreamReader in = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(in);
data = br.readLine();
} catch (Exception e) {
e.printStackTrace();
}
if (data.toString().equals(
passInput.getText().toString())) {
txtv.setText("You Have Logged in");
} else {
LayoutInflater ln = LayoutInflater.from(context);
View invalidView = ln.inflate(R.layout.invdialog,
null);
AlertDialog.Builder invalidDialogBuild = new AlertDialog.Builder(
context);
invalidDialogBuild.setView(invalidView);
// set dialog message
invalidDialogBuild
.setCancelable(false)
.setPositiveButton(
"Retry",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int id) {
dialog.cancel();
}
})
.setNegativeButton(
"Reset",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int id) {
dialog.cancel();
}
});
dialog.cancel();
AlertDialog invalidDialog = invalidDialogBuild
.create();
// show it
invalidDialog.show();
}
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
我知道你已經有對話部分工作。但我實際上發現,爲此目的不使用對話框的用戶體驗更好。相反,我可能有我的登錄屏幕'活動'與'EditView'用戶名和密碼,提交按鈕等。我也有一個'TextView'消息,我更新爲紅色和消息(使用'theTextView.setText(「」 )'如果這個人沒有登錄,這樣你不彈出任何東西只是簡單地改變消息文本來提醒用戶他們的用戶名是無效的 – wired00 2013-03-06 05:43:57
實際上這是一個人只有一個應用程序,他們不需要用戶名,只需要密碼。我需要在啓動屏幕上完成,而不是顯示屏幕2秒或更少的密碼必須問。你的方法可以通過對話框來實現,只需改變頂部的文本視圖,而不是彈出一個新的消息。但那麼將沒有重置選項。我可以在一個警報對話框中有三個按鈕 – GunJack 2013-03-06 05:55:02