弹出窗口按钮不起作用

时间:2017-10-25 13:50:03

标签: android android-layout android-popupwindow

我正在开发app,其中弹出窗口显示成功但内部按钮无法正常工作。单击该按钮时,事件未在侦听。代码如下所示。

private void showPopup() {
    //LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    View customView = inflater.inflate(R.layout.popup_layout,null);



    mPopupWindow = new PopupWindow(
            customView,
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT
    );

    Button btn_popup_submit = (Button)customView.findViewById(R.id.btn_popup_submit);
    Button btn_popup_cancel = (Button)customView.findViewById(R.id.btn_popup_cancel);

    btn_popup_submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getApplicationContext(), "aaa", Toast.LENGTH_SHORT).show();
            Log.d("LOG","aaaa");
        }
    });

    btn_popup_cancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getApplicationContext(), "bbbb", Toast.LENGTH_SHORT).show();
            Log.d("LOG","bbbb");
        }
    });

    if(Build.VERSION.SDK_INT>=21){
        mPopupWindow.setElevation(5.0f);//5.0f
    }



    mPopupWindow.showAtLocation(mRelativeLayout, Gravity.CENTER,0,0);
    mPopupWindow.setFocusable(true);

    mPopupWindow.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(mContext, android.R.color.transparent)));
    mPopupWindow.setOutsideTouchable(false);
    mPopupWindow.setTouchable(false);

    mPopupWindow.update();
}

弹出式布局代码为

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_custom_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp"
android:background="#5b5e93"
>

<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Email Sent."
    android:textSize="20sp"
    android:textColor="@color/colorWhite"

    android:padding="25sp"
    />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="5dp"
    android:layout_above="@+id/editText"
    android:layout_marginBottom="50dp"
    android:background="@color/colorGray"
    android:orientation="horizontal"></LinearLayout>

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/tv"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="76dp"
    android:textSize="16sp"
    android:textColor="@color/colorWhite"
    android:text="@string/email_sent" />

<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView2"
    android:ems="4"
    android:maxLength="4"
    android:minLines="4"
    android:inputType="number" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/editText"

    android:layout_marginBottom="10dp"
    android:orientation="horizontal"
    android:weightSum="2">

    <Button
        android:id="@+id/btn_popup_submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_weight="1"
        android:background="@drawable/buttoncolor"
        android:text="Submit"
        android:textAllCaps="false" />

    <Button
        android:id="@+id/btn_popup_cancel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_weight="1"
        android:background="@drawable/buttoncolor"
        android:text="Cancel"
        android:textAllCaps="false" />
</LinearLayout>

</RelativeLayout>

按钮名称为btn_popup_submitbtn_popup_cancel。 我尝试了不同的方法,但问题。我不知道面临的问题在哪里。请帮助我。

1 个答案:

答案 0 :(得分:1)

替换此行代码。允许popWindow Touch。

mPopupWindow.setTouchable(false);

mPopupWindow.setTouchable(true);