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

Android自定义工具栏onOptionsItemSelected不起作用,第1张

概述我有两个类似的按钮<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apkes/android"android:layout_width="fill_parent"android:layout_height="match_parent"

我有两个类似的按钮

<?xml version="1.0" enCoding="utf-8"?><relativeLayoutxmlns: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"    /><itemandroID: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)

@OverrIDepublic 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);

解决方法:

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

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

@L_419_1@

@OverrIDepublic 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>
总结

以上是内存溢出为你收集整理的Android自定义工具栏onOptionsItemSelected不起作用全部内容,希望文章能够帮你解决Android自定义工具栏onOptionsItemSelected不起作用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1120607.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存