Android:对话框关闭而不调用关闭

发布于 2024-10-10 08:04:38 字数 1594 浏览 3 评论 0原文

我有一个执行一些验证的对话框(如下)。问题是,显示 Toast 后对话框被关闭,而我没有调用关闭。我需要显示 toast 并保持对话框打开以更正错误。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

起风了 2024-10-17 08:04:38

我的猜测是,您没有像 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.

妄司 2024-10-17 08:04:38

我认为无论你想要实现什么目标,AlertDialog.bilder 都是不可能实现的
相反,您可以创建

  1. 对话框对象。
  2. 设置对话框的布局。
  3. 设置适当的监听器。

例子。

dialog_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent" android:orientation="vertical">

<EditText 
            android:layout_height="wrap_content" 
            android:id="@+id/EditText01" android:layout_width="300dip" android:ellipsize="none"/>

<LinearLayout 
        android:id="@+id/LinearLayout01" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">

        <Button 
                android:id="@+id/Button01" 
                android:layout_height="wrap_content" 
                android:text="Yes" 
                android:layout_width="100dip"/>

        <Button 
                android:id="@+id/Button02" 
                android:layout_height="wrap_content" 
                android:text="No" 
                android:layout_width="100dip"/>
</LinearLayout>

  </LinearLayout>

帮助.java

public class Help extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    d =  new Dialog(Help.this,
            android.R.style.Theme_InputMethod);

    createMyDialog();
}
  private Dialog d;
private void createMyDialog() {
    d.setContentView(R.layout.dialog_view);
    Button b1 = (Button)findViewById(R.id.Button01);
    Button b2 = (Button)findViewById(R.id.Button02);
    EditText t = (EditText) findViewById(R.id.EditText01);
    OnTouchListener listner1 = null;
    OnTouchListener listner2 = null;
    b1.setOnTouchListener(listner1);
    b2.setOnTouchListener(listner2);
    listner1 = new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            return false;
        }
    };
    listner2 = new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            return false;
        }
    };
            d.show();
}

}

I think whatever you are trying to achieve is not possible with AlertDialog.bilder
instead of that you can make

  1. object of Dialog.
  2. Set your layout for your dialog.
  3. Set the appropriate listener.

Example.

dialog_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent" android:orientation="vertical">

<EditText 
            android:layout_height="wrap_content" 
            android:id="@+id/EditText01" android:layout_width="300dip" android:ellipsize="none"/>

<LinearLayout 
        android:id="@+id/LinearLayout01" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">

        <Button 
                android:id="@+id/Button01" 
                android:layout_height="wrap_content" 
                android:text="Yes" 
                android:layout_width="100dip"/>

        <Button 
                android:id="@+id/Button02" 
                android:layout_height="wrap_content" 
                android:text="No" 
                android:layout_width="100dip"/>
</LinearLayout>

  </LinearLayout>

Help.java

public class Help extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    d =  new Dialog(Help.this,
            android.R.style.Theme_InputMethod);

    createMyDialog();
}
  private Dialog d;
private void createMyDialog() {
    d.setContentView(R.layout.dialog_view);
    Button b1 = (Button)findViewById(R.id.Button01);
    Button b2 = (Button)findViewById(R.id.Button02);
    EditText t = (EditText) findViewById(R.id.EditText01);
    OnTouchListener listner1 = null;
    OnTouchListener listner2 = null;
    b1.setOnTouchListener(listner1);
    b2.setOnTouchListener(listner2);
    listner1 = new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            return false;
        }
    };
    listner2 = new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            return false;
        }
    };
            d.show();
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文