0

我的应用程序中有一个搜索功能,我可以在菜单中上下移动图标。如果我点击向上和向下图标,我可以看到图标上的涟漪效果。现在,当我从键盘上点击搜索按钮时,我想执行向上的图标功能。为了以编程方式实现这一点,我制作了图标点击动作。我可以启动图标功能,但无法对图标产生连锁反应。任何解决方法可以在图标上产生连锁反应?

在下面的代码中,我习惯于以编程方式单击菜单。

 searchEdit.setOnEditorActionListener { _, actionId, _ ->
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
             actionUpMenuItem?.let {
                 onOptionsItemSelected(it)
             }
           true
        } else {
            false
        }
    }

我也试过这个,我看不到这两个函数的涟漪效应

searchEdit.setOnEditorActionListener { _, actionId, _ ->
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
           optionMenu?.performIdentifierAction(R.id.action_up, 0)
           true
        } else {
            false
        }
    }

这是菜单项代码

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/action_up"
    android:foreground="?attr/selectableItemBackground"
    android:icon="@drawable/ic_search_up_arrow"
    android:orderInCategory="94"
    android:title="@string/description_item_up"
    app:showAsAction="always" />

<item
    android:id="@+id/action_down"
    android:foreground="?attr/selectableItemBackground"
    android:icon="@drawable/ic_search_down_arrow"
    android:orderInCategory="95"
    android:title="@string/description_item_down"
    app:showAsAction="always" />
</menu>
4

1 回答 1

0

创建您的自定义涟漪效果ripple_effect_small_corner

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
     android:color="?android:attr/colorControlHighlight">
    <item android:id="@android:id/mask">
        <shape android:shape="rectangle">
            <solid android:color="#000000" />
            <corners android:radius="@dimen/_2sdp" />
        </shape>
    </item>
    <item android:drawable="@drawable/list_item_background_small_corner" />
</ripple>

创建可绘制的 list_item_background_small_corner

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/whitecolor"/>
    <stroke android:width="1dp" android:color="@color/divider" />

<corners android:radius="@dimen/_2sdp"></corners>

</shape>

你可以这样使用:

android:background="@drawable/ripple_effect_small_corner"

于 2021-05-26T15:06:57.047 回答