弹出 window 按钮不起作用

popup window button not working

我正在开发一个弹出窗口 window 显示成功但里面的按钮不起作用的应用程序。当按钮被点击时,事件没有被监听。代码如下所示。

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。 我尝试了不同的方法,但问题所在。我不知道问题在哪里。请帮助我谢谢。

替换 code.Allow popWindow Touch 的这一行。

mPopupWindow.setTouchable(false);

mPopupWindow.setTouchable(true);