单击事件在自定义popupwindow中不起作用

9wbgstp7  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(416)

我使用popupwindow,自定义视图有两个文本视图,我试图响应click事件,但click事件没有触发,下面是我使用的代码

/* Get the binding object */
    val binding: XYZBinding = XYZBinding.inflate(
        LayoutInflater.from(applicationContext),
        null,
        false
    )
    binding.clickHandler = this

    /* Set the popup menu window */
    popupWindow = PopupWindow(binding.root)
    popupWindow.contentView = LayoutInflater.from(applicationContext)
        .inflate(R.layout.popup_menu_gang, null, false)

    popupWindow.isTouchable = true
    popupWindow.isFocusable = true
    popupWindow.isOutsideTouchable = true
    popupWindow.width = WindowManager.LayoutParams.WRAP_CONTENT
    popupWindow.height = WindowManager.LayoutParams.WRAP_CONTENT
    popupWindow.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

    popupWindow.setOnDismissListener(PopupWindow.OnDismissListener {
        window.attributes.alpha = alpha
        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
        window.decorView.invalidate()
    })
    popupWindow.showAsDropDown(binding.layoutHeader.imageViewMenu)

我也尝试过一个直接的onclick监听器,但效果不太好

binding.textViewGangPopupRenameButtons.setOnClickListener(View.OnClickListener {
        Toast.makeText(applicationContext, "working", Toast.LENGTH_SHORT).show()
    })

下面是我正在使用的xml

<?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">

        <data>

            <import type="xyz.BaseActivity" />

            <variable
                name="clickHandler"
                type="BaseActivity" />

        </data>

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <androidx.cardview.widget.CardView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:layout_marginEnd="16dp"
                app:cardCornerRadius="16dp">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <TextView
                        android:id="@+id/textViewGangPopupDelete"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:clickable="true"
                        android:focusable="true"
                        android:fontFamily="@font/montserrat_medium"
                        android:onClick="@{(view) -> clickHandler.onClick(view)}"
                        android:paddingStart="16dp"
                        android:paddingTop="16dp"
                        android:paddingEnd="16dp"
                        android:paddingBottom="8dp"
                        android:text="@string/delete"
                        android:textColor="@color/textColorBlue"
                        android:textSize="14sp"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />

                    <TextView
                        android:id="@+id/textViewGangPopupRenameButtons"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:clickable="true"
                        android:focusable="true"
                        android:fontFamily="@font/montserrat_medium"
                        android:onClick="@{(view) -> clickHandler.onClick(view)}"
                        android:paddingStart="16dp"
                        android:paddingTop="8dp"
                        android:paddingEnd="16dp"
                        android:paddingBottom="16dp"
                        android:text="@string/rename_buttons"
                        android:textColor="@color/textColorBlue"
                        android:textSize="14sp"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/textViewGangPopupDelete" />

                </androidx.constraintlayout.widget.ConstraintLayout>

            </androidx.cardview.widget.CardView>
        </FrameLayout>

    </layout>
uhry853o

uhry853o1#

使用popupview设置文本视图的id。。这样地

` val layoutInflater = activity!!
                .getSystemService(AppCompatActivity.LAYOUT_INFLATER_SERVICE) as 
           LayoutInflater
            val popupView: View = 
      layoutInflater.inflate(R.layout.custom_grid_bottomnav, null)
            val popupWindow = PopupWindow(
                popupView,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )

            // popupWindow.dismiss()
            val tx = popupView.findViewById<TextView>(R.id.text1) //change
            tx.setOnClickListener(object : View.OnClickListener {
                override fun onClick(v: View?) {
                   //Do what you want

} }`

相关问题