本文介绍了AlertDialog这是一个错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序创建用户在其中输入名称,以保存AlertDialog。当用户点击保存按钮,该onClickListener将检查重复的名字。如果该名称已存在,另一个对话框会弹出提醒,现有的数据将被替换的用户。然后,用户可以选择取消,回到更改为新的名称,或继续前进,有数据替换。
当第二个对话框出现时,我想到的第一个对话框仍是可见的,直到我打电话解雇。但是,第一个AlertDialog消失第二AlertDialog才会出现。这就是解雇一个按钮被点击时,会自动被调用。这是一个错误或设计?
我写测试用例低于我查了3设备:歌Nexus S的Andr​​oid 4.0,HTC Rezound的Andr​​oid 2.3和摩托罗拉Droid仿生的Andr​​oid 2.3。

My app creates an AlertDialog in which a user enters the name to save. When a user clicks the save button, the onClickListener will check for duplicated name. If the name already exists, another dialog box will pop up to alert the user that existing data will be replaced. The user then have a choice to cancel and go back to change to a new name or go ahead and have the data replace.
When the second dialog appears, I expect the first dialog box is still visible until I call dismiss. However, the first AlertDialog disappeared before the second AlertDialog appears. That is dismiss will automatically be called when a button is clicked. Is this a bug or by design?
I wrote the test case below which I checked on 3 devices: Nexus S android 4.0, HTC Rezound android 2.3 and Motorola Droid Bionic android 2.3.

布局

<?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" >

    <TextView
        android:id="@+id/message"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Some message will be here"
    />

    <Button
        android:id="@+id/show_btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Show"
    />

</LinearLayout>

code

public class AlertDialogBug extends Activity
{
    static final int DIALOG_ALERT_ID = 1;
    AlertDialog alertDlg;
    TextView message;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        message = (TextView) findViewById(R.id.message);
        Button showButton = (Button) findViewById(R.id.show_btn);
        showButton.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                showDialog(DIALOG_ALERT_ID);
            }
         });
     }

    private AlertDialog createAlertDialog()
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Bug?");

        builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
        {

            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                // No dismiss, cancel, finish, or removeDialog,
                // but the dialog will disappear when this button is clicked.
            }

          });

         alertDlg = builder.create();
         alertDlg.setOnDismissListener(new OnDismissListener()
         {

             @Override
             public void onDismiss(DialogInterface dialog)
             {
                message.setText("onDismiss was called");
             }

          });

          return alertDlg;
    }

    @Override
    protected Dialog onCreateDialog(int id)
    {
        switch (id)
        {
            case DIALOG_ALERT_ID:
                return createAlertDialog();

            default:
                return super.onCreateDialog(id);
        }
    }

}

我原来写的保存对话框与Android的活动:主题=@安卓风格/ Theme.Dialog。该UI看起来很好了Nexus S和Rezound上,但看起来很糟糕大家对Droid仿生(编辑框和按钮,只占据了宽度的一半,另一半是空的)。

I originally wrote the save dialog box as an activity with android:theme="@android:style/Theme.Dialog". The UI looks fine on the Nexus S and Rezound but look terrible on the Droid Bionic (the edit box and button only occupied half of the width, the other half is blank).

推荐答案

这是设计使然。如果你不希望取消由点击按钮的对话框,在这里下面是一些codeS为您服务。在setPositiveButton方法添加这个时候你不希望取消该对话框。

This is by design.If you don't want to cancel the dialog by click the button, here below is some codes for you.Add this in your setPositiveButton method when you don't want to cancel the dialog.

try {
Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
field.set(dialog, false);

} catch (Exception e) {
e.printStackTrace();
}

然后,如果你想取消该对话框,只需要添加此下方。

Then if you want to cancel the dialog, just need to add this below.

try {
Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
field.set(dialog, true);
} catch (Exception e) {
e.printStackTrace();
}

顺便说一句,你的XML它永远不会被你的警告对话框。由于的setTitle()和setMessage方法由警告对话框提供的。

By the way, your xml is never called by your alert dialog. As the setTitle() and setMessage method are provided by alert dialog.

如果你想提供自定义对话框,叫setCustomeView(布局)。

If you want to provide custom dialog, call setCustomeView(layout).

任何questiones,让我知道。

Any questiones, let me know.

这篇关于AlertDialog这是一个错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 01:56
查看更多