在菜单工具栏中切换底部单击的事件

时间:2016-07-23 18:07:15

标签: java android

我想在工具栏中添加切换按钮。所以我做了这个布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_centerInParent="true">

    <TextView
        android:id="@+id/textview_filter"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="@string/filter"
        android:gravity="center"
        android:textSize="15dp"
        android:layout_marginRight="-10dp"/>

    <com.kyleduo.switchbutton.SwitchButton
        android:id="@+id/sb_ios"
        android:paddingTop="15dp"
        style="@style/SwitchButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:kswAnimationDuration="300"
        app:kswBackDrawable="@drawable/ios_back_drawable"
        app:kswBackMeasureRatio="1.4"
        app:kswThumbDrawable="@drawable/ios_thumb_selector"
        app:kswThumbMarginBottom="-8dp"
        app:kswThumbMarginLeft="-5dp"
        app:kswThumbMarginRight="-5dp"
        app:kswThumbMarginTop="-2.5dp"/>

</LinearLayout>

和菜单

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:id="@+id/filterButton"
    android:title="Open Filter"
    android:icon="@drawable/ic_action_filter"
    android:orderInCategory="100"
    android:visible="false"
    app:showAsAction="always"/>

<item
    android:id="@+id/myswitch"
    android:title=""
    android:orderInCategory="200"
    app:showAsAction="always"/>

和片段

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    menu.clear();
    inflater.inflate(R.menu.menu_cvs_fav, menu);
    MenuItem item = menu.findItem(R.id.myswitch);
    item.setActionView(R.layout.switch_button_toolbar);
    item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            Toast.makeText(getContext(), "Clicked", Toast.LENGTH_SHORT).show();
            return true;
        }
    });
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    Log.i("Whatt", "enter");
    int id = item.getItemId();

    if (id == R.id.myswitch) {
        Log.i("Whatt", "enter1");
        if (bSwitch) {
            Log.i("Whatt", "enter2");
            Toast.makeText(getContext(), "On", Toast.LENGTH_SHORT).show();
            bSwitch = false;
        } else {
            Log.i("Whatt", "enter3");
            Toast.makeText(getContext(), "Off", Toast.LENGTH_SHORT).show();
            bSwitch = true;
        }
    }
    return false;
}

但问题是 setOnMenuItemClickListener 无法在项目中工作,并且在尝试 onOptionsItemSelected 时未输入 log.i()未显示的功能任何东西,虽然在菜单项上添加 请帮助!!

0 个答案:

没有答案