2

我有一个警报对话框,在对话框的布局中有一个我希望可以点击的按钮,但是当我使用 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>

4

2 回答 2

0

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

于 2012-12-20T21:00:59.663 回答
0

您没有在否定按钮上调用驳回:

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

至于积极的一面,你onClick被解雇了吗?把Log语句放进去看看是不是。

于 2012-12-20T20:45:43.673 回答