AlertDialog自定义布局按钮无法单击

时间:2016-03-03 22:53:04

标签: android android-layout onclick alertdialog

我正在尝试在AlertDialog类中创建一个登录按钮。我正在用按钮给自定义布局充气。

问题是按钮什么都不做。代码编译,没有错误,只是不可点击

这是我的班级。

public class LoginDialogFragment extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    //create inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    //set view with inflater
    final View dialogView = inflater.inflate(R.layout.login_dialog, null);
    builder.setView(inflater.inflate(R.layout.login_dialog, null));

    //set dialog to be non-cancelable
    setCancelable(false);

    final Dialog alertDialog = builder.create();

    //remove title bar
    alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

    //get editText fields to set font
    EditText editUser = (EditText) dialogView.findViewById(R.id.user);
    EditText editPass = (EditText) dialogView.findViewById(R.id.pass);
    EditText editIp   = (EditText) dialogView.findViewById(R.id.ip_addr);
    Button   login    = (Button)   dialogView.findViewById(R.id.login_button);

    //get the font
    Typeface typeFace = Typeface.createFromAsset(getContext().getAssets(), "fonts/arial.ttf");

    //set the font
    editIp.setTypeface(typeFace);
    editPass.setTypeface(typeFace);
    editUser.setTypeface(typeFace);

    //set background and no dim affect
    alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
    alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

    //create onClick listener
    login.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getActivity(), "message", Toast.LENGTH_LONG).show();

        }
    });

    // Create the AlertDialog object and return it
    return alertDialog;
}

继承我的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"
    android:padding="15dp"
    android:background="@drawable/dialog_bg"
    android:backgroundDimEnabled="false">

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/imageView"
        android:src="@drawable/optanix_logo"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:layout_alignParentTop="true"
        android:padding="8dp"
        android:layout_centerHorizontal="true" />

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:backgroundDimEnabled="false">

        <ImageView
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:src="@drawable/ic_action_user"
            android:adjustViewBounds="true"
            android:layout_marginTop="10dp"
            android:padding="4dp"
            android:background="@drawable/icon_bg"
            android:layout_centerHorizontal="true" />

        <EditText
            android:id="@+id/user"
            android:layout_width="fill_parent"
            android:layout_height="32dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/edit_text_bg"
            android:hint="Username"
            android:textSize="10sp"
            android:padding="4dp" />

    </LinearLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:backgroundDimEnabled="false">

        <ImageView
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:src="@drawable/ic_action_lock_closed"
            android:adjustViewBounds="true"
            android:layout_marginTop="10dp"
            android:padding="4dp"
            android:background="@drawable/icon_bg"
            android:layout_centerHorizontal="true" />

        <EditText
            android:id="@+id/pass"
            android:layout_width="fill_parent"
            android:layout_height="32dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/edit_text_bg"
            android:hint="Password"
            android:inputType="textPassword"
            android:textSize="10sp"
            android:padding="4dp" />
    </LinearLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:backgroundDimEnabled="false">

        <ImageView
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:src="@drawable/ic_action_location"
            android:adjustViewBounds="true"
            android:layout_marginTop="10dp"
            android:padding="4dp"
            android:background="@drawable/icon_bg"
            android:layout_centerHorizontal="true" />

        <EditText
            android:id="@+id/ip_addr"
            android:layout_width="fill_parent"
            android:layout_height="32dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/edit_text_bg"
            android:hint="IP address"
            android:textSize="10sp"
            android:padding="4dp" />

    </LinearLayout>


    <Button
        android:id="@+id/login_button"
        android:layout_width="match_parent"
        android:layout_height="32dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/login_button_bg"
        android:textSize="10sp"
        android:textAllCaps="false"
        android:textColor="#FFFFFF"
        android:text="@string/login_string"/>

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

这是问题所在:

final View dialogView = inflater.inflate(R.layout.login_dialog, null);
builder.setView(inflater.inflate(R.layout.login_dialog, null));

您正在对视图进行两次充气,并将点击侦听器设置为不在对话框中的视图。

这样做:

final View dialogView = inflater.inflate(R.layout.login_dialog, null);
builder.setView(dialogView);