Android自定义工具栏onOptionsItemSelected不起作用

4

我有两个相似的按钮

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
    android:id="@+id/sync_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Sync"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    />

</RelativeLayout>

添加为菜单项

<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/toggle_item"
    android:title=""
    app:showAsAction="always"
    app:actionLayout="@layout/switch_button"
    />
<item
android:id="@+id/sync_item"
android:title=""
app:showAsAction="always"
app:actionLayout="@layout/sync"
/>
</menu>

我在onCreateOptionsMenu中使用inflate方法将项目添加到菜单中。

getMenuInflater().inflate(R.menu.menu_main, menu);

一切看起来都很好,但是当我点击应用程序时,因为onOptionsItemSelected(MenuItem item)从未被调用,所以没有任何反应。

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Integer test = item.getItemId();
    switch (item.getItemId()) {
        case R.id.toggle_item:
          ...
        case R.id.sync_item:
          ...
  return super.onOptionsItemSelected(item);
 }

添加工具栏

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);

我尝试在标准项旁边添加一个项目,该项目点击后会触发事件,而我的自定义项已经定义了actionLayout,但它不起作用。 - user2925656
2个回答

8
我终于找到了答案。这两个答案的组合解决了我的问题:

https://dev59.com/bWMm5IYBdhLWcg3wO9Hy#17764895

https://dev59.com/bWMm5IYBdhLWcg3wO9Hy#23936117

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    final Menu m = menu;
    final MenuItem item = menu.findItem(R.id.sync_button);
    item.getActionView().setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {   
            do_stuff;
        }
    });
    return true;
}

菜单项:

  <item
    android:id="@+id/myswitch"
    android:title=""
    app:showAsAction="always"
    app:actionLayout="@layout/toggle" />

活动布局必须实现:

android:clickable="false"

我的切换操作布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="false">
<ToggleButton
        android:id="@+id/actionbar_service_toggle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOn="Discovery"
    android:textOff="Favourite"
    android:clickable="false"/>
</RelativeLayout>

2
老兄,这简直和他们试图推销给我们的新材料库1.1.0+一样糟糕。 - lawonga
我发现在onClick事件中调用boolean onOptionsItemSelected(@NonNull MenuItem item)很有趣,正如这里所指出的:https://dev59.com/V18e5IYBdhLWcg3wCGu_ 或者,如果您想要传播到活动中的片段,则可以调用boolean onMenuItemSelected(int featureId, @NonNull MenuItem item),正如评论中提到的那样。 - marcRDZ

0

可以通过访问工具栏项上的菜单来实现。

//kotlin
toolbar.menu.getItem(itemIndex).setOnMenuItemClickLister {}

您可以对所有菜单项执行此操作


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接