問題描述
我有一個執行一些驗證的對話框(如下).你的問題是,顯示 Toast 后對話框被關閉,而我沒有調用關閉.我需要展示 toast 并保持對話框打開以更正錯誤.
I have a dialog which performs some validation (below). Thee problem is, the dialog is dismissed after the Toast is displayed, without me calling dismiss. I need to show the toast and keep the dialog open to correct the error.
final EditText txtName = new EditText(this);
AlertDialog.Builder dlgAdd = new AlertDialog.Builder(this)
.setTitle(R.string.create_category)
.setMessage(R.string.name)
.setView(txtName)
.setPositiveButton(R.string.ok, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String newCatName = txtName.getText().toString().trim(); // Converts the value of getText to a string.
if (newCatName != null && newCatName .length() ==0)
{
Toast.makeText(ManageCategories.this, R.string.err_name_required, 3500).show();
} else {
try {
boolean alreadyExists = mDatabaseAdapter.getCategoryIDs(newCatName).length > 0;// ids of cats with this name
if(alreadyExists) {
Toast.makeText(ManageCategories.this, R.string.categoryAlreadyExists, 3500).show();
} else {
mDatabaseAdapter.addCategory(newCatName);
}
}catch(Exception ex){
Toast.makeText(ManageCategories.this, R.string.error+':'+ ex.getLocalizedMessage(), 3500).show();
}
}
}
}).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
dlgAdd.show();
推薦答案
我猜你并沒有像這里的 Android 文檔中提到的那樣創建和顯示對話框 http://developer.android.com/guide/topics/ui/dialogs.html 使用 OnCreateDialog 函數
My guess is that you are not creating and showing dialog as mentioned in the Android docs here http://developer.android.com/guide/topics/ui/dialogs.html using OnCreateDialog functions
請按照文檔中的說明進行操作,如果仍然無法正常工作,請告知我們.
Please do as mentioned in the docs and let us know if it still does not work.
這篇關于Android:對話框關閉而不調用關閉的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!