Android: Dialog dismisses without calling dismiss(Android:对话框关闭而不调用关闭)
问题描述
我有一个执行一些验证的对话框(如下).你的问题是,显示 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:对话框关闭而不调用关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android:对话框关闭而不调用关闭
基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01