警报对话框中的按钮不可单击

时间:2012-12-20 20:41:53

标签: android alertdialog

我有一个警告对话框,在对话框的布局中有一个我想要点击的按钮,但是当我使用onclick监听器时,按钮仍然无效。

我的警报对话框就是这样构建的。

AlertDialog.Builder alert = new AlertDialog.Builder(this);

    Context mContext = getApplicationContext();
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.distro_editor_popup, (ViewGroup) findViewById(R.id.layout_root));

    txt_Name = (EditText) layout.findViewById(R.id.txt_LinuxName);
    txt_Image = (EditText) layout.findViewById(R.id.txt_ImageName);
    filemanger = (Button) layout.findViewById(R.id.fileselector); 

    txt_Name.setText(selected_Name);
    txt_Image.setText(selected_Image);

    final String oldName = selected_Name;
    final String oldImage = selected_Image;

    filemanger.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("file/*");
            startActivityForResult(intent,PICKFILE_RESULT_CODE);
        }
    });


    alert.setView(layout);

    alert.setPositiveButton(R.string.dialog_button_ok, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            String name = txt_Name.getText().toString();
            String image = txt_Image.getText().toString();

            // Make sure the user entered a name
            if (name.equals("")) {
                return;
            }

            if (!oldName.equals(name)) {
                // Name was changed so we have to delete the old one from the profiles first!
                profiles.remove(oldName);
            }

            if (!oldImage.equals(image)) {
                // Image name has changed so we rename the mounts and config files
                file_Rename(oldImage + ".mounts", image + ".mounts");
                file_Rename(oldImage + ".config", image + ".config");
            }

            profiles.put(name, image);
            lastSelected = name;

            fillSpinner();
            savePrefs();
        }
    });
    alert.setNegativeButton(R.string.dialog_button_cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
        }
    });

    alert.show();

然后对话框的XML布局是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>

<RelativeLayout android:layout_alignBaseline="@+id/layout_root" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginTop="8sp" android:id="@+id/RelativeLayout1">
    <TextView
        android:id="@+id/label_Name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/inputLabel_Name"
        android:layout_centerVertical="true"/>

    <EditText
        android:id="@+id/txt_LinuxName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/label_Name"
        android:hint="@string/hint_EnterName"
        android:textColor="#FF111111" >

        <requestFocus></requestFocus>
    </EditText>
</RelativeLayout>

<RelativeLayout
    android:id="@+id/RelativeLayout3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/layout_root"
    android:layout_marginTop="8sp" >

    <TextView
        android:id="@+id/label_Image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:text="@string/launcher_Label_ImageName" />

    <EditText
        android:id="@+id/txt_ImageName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/fileselector"
        android:layout_toRightOf="@id/label_Image"
        android:textColor="#FF111111" >

        <requestFocus></requestFocus>
    </EditText>

    <Button
        android:id="@+id/fileselector"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/txt_ImageName"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="..." />

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

你没有在否定按钮上调用dismiss:

alert.setNegativeButton(R.string.dialog_button_cancel, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
          dialog.dismiss();
    }
});

至于积极的,你的onClick被解雇了吗?将日志语句放入并查看是否存在。

答案 1 :(得分:0)

这只是一个猜测所以它可能是错误的,但是当您设置onClickListener时,请尝试将行fileSelector.setOnClickListener(new OnClickListener)替换为fileSelector.setOnClickListener(new View.OnClickListener)