简体   繁体   English

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

[英]onOptionsItemSelected in Menu is not clickable for item using actionLayout

I have an android app with options menu, and the first item is defined with "actionLayout":我有一个带有选项菜单的 android 应用程序,第一个项目是用“actionLayout”定义的:

menu.xml snippet: 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 snippet: 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>

then I set the menu up:然后我设置了菜单:

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

}

however, the然而

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

is never called when I click on the menu item.当我单击菜单项时从不调用。

I looked up the solution for the sherlockactionbar from 2012 for java, but the solution does not apply well: onOptionsItemSelected not called when using actionLayout (SherlockActionBar)我从 2012 年为 java 查找了 sherlockactionbar 的解决方案,但该解决方案并不适用: 使用 actionLayout (SherlockActionBar) 时未调用 onOptionsItemSelected

It seems related to the action Layout of the item, Without this, it is working well.它似乎与项目的动作布局有关,没有这个,它运行良好。 how to make the onOptionsItemSelected working?如何使 onOptionsItemSelected 工作?

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
}

The above snippet should perform onClick event for you上面的代码段应该为您执行 onClick 事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OnOptionsItemSelected 没有为具有自定义 actionLayout 的操作栏菜单项调用 - OnOptionsItemSelected not being called for Action Bar Menu Item with custom actionLayout 单击设置了actionLayout的菜单项时未调用onOptionsItemSelected() - onOptionsItemSelected() not called when clicking on menu item which has an actionLayout set on it 使用actionLayout(SherlockActionBar)时未调用onOptionsItemSelected - onOptionsItemSelected not called when using actionLayout (SherlockActionBar) 使用actionLayout触摸菜单项上的反馈 - Touch feedback on menu item with actionLayout 如果设置了 actionLayout,则菜单项不显示 - menu Item not showing if actionLayout is set 使用actionLayout属性时,为什么不能单击我的操作栏菜单项? - Why can't I click my action bar menu item when using actionLayout property? Android - 菜单项无法解析为onOptionsItemSelected()中的类型 - Android - Menu Item cannot be resolved to a type in onOptionsItemSelected() Android操作栏菜单项,actionLayout无法正常工作 - Android action bar menu item with actionLayout not working properly 为什么onPrepareOptionsMenu(菜单菜单)和onOptionsItemSelected(MenuItem项目)不显示任何菜单? - Why are onPrepareOptionsMenu(Menu menu) and onOptionsItemSelected(MenuItem item) not displaying any menu? 更改菜单的actionLayout值? - Change the menu actionLayout value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM