【问题标题】:Android custom toolbar onOptionsItemSelected not workingAndroid自定义工具栏onOptionsItemSelected不起作用
【发布时间】:2016-05-17 10:34:34
【问题描述】:

我有两个相似的按钮

<?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 中将项目膨胀到菜单
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);

【问题讨论】:

标签: android android-actionbar onclicklistener toolbar


【解决方案1】:

访问工具栏项目上的menu 可以工作。

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

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

【讨论】:

    【解决方案2】:

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

    https://stackoverflow.com/a/17764895/2925656

    https://stackoverflow.com/a/23936117/2925656

    @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>
    

    【讨论】:

    • 伙计,这和他们试图推给我们的新材料库 1.1.0+ 一样糟糕
    猜你喜欢
    • 1970-01-01
    • 2021-12-13
    • 2014-12-25
    • 1970-01-01
    • 2012-03-20
    • 2015-09-07
    • 1970-01-01
    • 2014-12-22
    • 1970-01-01
    相关资源
    最近更新 更多