kotlin 对于使用actionLayout的项目,菜单中的onOptionsItemSelected不可单击

ha5z0ras  于 2023-11-21  发布在  Kotlin
关注(0)|答案(2)|浏览(180)

我有一个带有选项菜单的Android应用程序,第一个项目是用“actionLayout”定义的:
menu.xml代码段:

<item
    android:id="@+id/Cart"
    android:title="Cart"
    app:actionLayout="@layout/custom_action_item_layout"
    app:showAsAction="always"/>
<item
    android:id="@+id/settings"
    android:icon="@drawable/ic_settings_white_24dp"
    android:title="@string/settings"
    app:showAsAction="ifRoom" />

字符串
custom_action_item_layout片段:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
style="?attr/actionButtonStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:focusable="true"
android:clickable="true">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:src="@drawable/ic_shopping_cart_white_24dp"/>

<TextView
    android:id="@+id/cart_badge"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_gravity="right|end|top"
    android:layout_marginEnd="-5dp"
    android:layout_marginRight="-5dp"
    android:layout_marginTop="3dp"
    android:background="@drawable/badge_background"
    android:gravity="center"
    android:padding="1dp"
    android:textColor="@android:color/white"

    android:textSize="12sp"/>

</FrameLayout>


然后我设置菜单:

override fun onCreateOptionsMenu(menu_: Menu): Boolean {
    menu = menu_
    val inflater = menuInflater
    inflater.inflate(R.menu.uppermenu, menu)

    var cart = menu!!.findItem(R.id.Cart);
    var actionView = getActionView(cart);

    textCartItemCount = actionView.findViewById(R.id.cart_badge) as TextView

    return true

}


override fun onOptionsItemSelected(item: MenuItem): Boolean { }


当我点击菜单项时,
我查找了2012年针对java的sherlockactionbar的解决方案,但该解决方案并不适用:onOptionsItemSelected not called when using actionLayout (SherlockActionBar)
它似乎与项目的操作布局有关,没有这个,它工作得很好。如何使onOptionsItemSelected工作?

eqqqjvef

eqqqjvef1#

override fun onCreateOptionsMenu(menu_: Menu): Boolean {
    menu = menu_
    menuInflater.inflate(R.menu.uppermenu, menu)
    var cartMenuItem = menu!!.findItem(R.id.Cart)
    cartMenuItem.actionView?.cart.setOnClickListener {
          cartMenuItem.actionView?.cart_badge.text = "content update in the textview"
    }
    return true
}

字符串
上面的代码片段应该为您执行onClick事件

fumotvh3

fumotvh32#

使用新的MenuProvider接口:

requireActivity().addMenuProvider(
    object : MenuProvider {
        override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
            menuInflater.inflate(R.menu.uppermenu, menu)
            val cartMenuItem = menu.findItem(R.id.Cart)

            cartMenuItem.actionView?.cart.setOnClickListener {
                cartMenuItem.actionView?.cart_badge.text = "content update in the textview"
            }
        }

        override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
            // click action handled in onCreateMenu.
            return false
        }
    }, viewLifecycleOwner, Lifecycle.State.STARTED
)

字符串
注意,操作视图需要设置android:clickable="true"android:focusable="true"

相关问题