机器人:对话驳回,而无需调用驳回机器人

2023-09-05 05:17:26 作者:勉强可爱.

我有进行一些验证(下图)的对话框。你的问题是,在显示吐司之后被驳回的对话,没有我呼吁解雇。我需要显示敬酒,并保持该对话框打开来纠正错误。

 最后的EditText txtName的=新的EditText(本);
AlertDialog.Builder dlgAdd =新AlertDialog.Builder(本)
    .setTitle(R.string.create_category)
    .setMessage(R.string.name)
    .setView(txtName的)
    .setPositiveButton(R.string.ok,新OnClickListener(){
        @覆盖
        公共无效的onClick(DialogInterface对话,诠释它){
            字符串newCatName = txtName.getText()的toString()修剪()。 //的getText的值转换为字符串。
            如果(newCatName = NULL和放大器;!&安培; newCatName .length()== 0)
            {
                Toast.makeText(ManageCategories.this,R.string.err_name_required,3500).show();

            } 其他 {
                尝试 {
                    布尔alreadyExists = mDatabaseAdapter.getCategoryIDs(newCatName).length> 0; //猫这个名字IDS
                    如果(alreadyExists){
                        Toast.makeText(ManageCategories.this,R.string.categoryAlreadyExists,3500).show();
                    } 其他 {
                        mDatabaseAdapter.addCategory(newCatName);
                    }
                }赶上(例外前){
                    Toast.makeText(ManageCategories.this,R.string.error +:+ ex.getLocalizedMessage(),3500).show();
            }
            }
        }
    })。setNegativeButton(R.string.cancel,新DialogInterface.OnClickListener(){
           公共无效的onClick(DialogInterface对话框,INT ID){
                dialog.cancel();
           }
});
dlgAdd.show();
 

解决方案

我的猜测是,你没有创建和显示对话框中提到了Android文档这里的 http://developer.android.com/guide/topics/ui/dialogs.html 使用OnCreateDialog功能

请做中提到的文档,并让我们知道,如果它仍然无法正常工作。

智能机器人对话

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();

解决方案

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.