Android菜单项switchCompat状态通过其他菜单项单击更改

时间:2018-11-30 09:50:47

标签: android dynamic menu oncreateoptionsmenu

我的菜单包含两个项目(switchCompat和常规项目包括位置图像)。当我单击位置图像时,switchCompat生效并更改其状态。这就是我所不想要的。我该如何解决这个问题?

我的菜单:

  <item
    android:id="@+id/myGps_image"
    android:title="@string/location"
    app:showAsAction="always"
    android:icon="@drawable/ic_location_off"/>
<item
    android:id="@+id/myswitch"
    android:title=""
    app:showAsAction="always"
    android:actionLayout="@layout/switch_layout"
    />

switch_layout:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize">

<android.support.v7.widget.SwitchCompat
    android:id="@+id/switchForActionBar"
    android:layout_width="?attr/actionBarSize"
    android:layout_height="?attr/actionBarSize"
    android:gravity="center_vertical"
    android:theme="@style/SwithButtonMf"/>

和我的onCreateOptionsMenu代码:

@Override
public void onCreateOptionsMenu(final Menu menu, MenuInflater inflater) {
    menu.clear();

    inflater.inflate(R.menu.switch_menu,menu);
    MenuItem item=menu.findItem(R.id.myswitch);

    item.setActionView(R.layout.switch_layout);
    switchCompat= menu.findItem(R.id.myswitch).getActionView().findViewById(R.id.switchForActionBar);

    //Objects.requireNonNull(getActivity()).invalidateOptionsMenu();
    switchCompat.setChecked(x);

    switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            if(isChecked){
                if(checkSelfPermission(Objects.requireNonNull(getContext()),permissions[0])==PackageManager.PERMISSION_GRANTED
                        &&checkSelfPermission(getContext(),permissions[1])==PackageManager.PERMISSION_GRANTED){

                    new android.app.AlertDialog.Builder(getActivity(),R.style.AlertDialogCustom).setTitle(R.string.enable_automatic_message)
                            .setMessage(Objects.requireNonNull(getActivity()).getString(R.string.enable_switch_button_for_automatic_mesage))
                            .setPositiveButton(getActivity().getString(R.string.okey), new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    switch_state_basma_olumlu();
                                    switchCompat.setChecked(true);
                                    dialogInterface.dismiss();
                                }
                            }).setCancelable(false)
                            .setNegativeButton(getActivity().getString(R.string.cancel), new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {

                                    switch_state_basma_olumsuz();
                                    switchCompat.setChecked(false);
                                    dialogInterface.dismiss();
                                }
                            }).create().show();

                   //switch_state_basma_olumlu();
                }else {
                    switchCompat.setChecked(false);
                    requestSWITCHPermissions();

                    if(checkSelfPermission(getContext(),permissions[0])==PackageManager.PERMISSION_GRANTED
                            &&checkSelfPermission(getContext(),permissions[1])==PackageManager.PERMISSION_GRANTED){
                       switch_state_basma_olumlu();
                    }else {
                            switch_state_basma_olumsuz();
                    }

                }



            }else  {
                editorSwitch=getActivity().getSharedPreferences("com.contacttofile.smslocation", Context.MODE_PRIVATE).edit();
                editorSwitch.putBoolean("MainFragmentSwitchState",false);
                editorSwitch.apply();
            }

        }
    });







    super.onCreateOptionsMenu(menu, inflater);

}

onPrepareOptionsMenu(我根据gps状态更改位置图片):

 @Override
public void onPrepareOptionsMenu(Menu menu) {

    MenuItem iconNetwork = menu.findItem(R.id.myGps_image);
    if(iconNetwork!=null){
        iconNetwork.setIcon(is_gps_enable?R.drawable.ic_location_on:R.drawable.ic_location_off);

        iconNetwork.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                // menu.getItem(0).setIcon(ContextCompat.getDrawable(getActivity(), R.drawable.ic_location_on));
                Objects.requireNonNull(getActivity()).invalidateOptionsMenu();
                if(!is_gps_enable){
                    android.app.AlertDialog.Builder builder=new android.app.AlertDialog.Builder(getActivity(),R.style.AlertDialogCustom);
                    builder.setTitle(R.string.open_gps);
                    builder.setCancelable(false);
                    builder.setIcon(R.drawable.ic_location_red);

                    builder.setMessage(R.string.gps_mode_high_accuracy);
                    builder.setPositiveButton(getActivity().getString(R.string.ayarlar), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                            getActivity().startActivity(intent);
                            dialogInterface.dismiss();
                        }
                    });

                    builder.setNegativeButton(getActivity().getString(R.string.cancel), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    });

                    builder.create().show();

                }else {
                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    getActivity().startActivity(intent);
                }


                return true;
            }
        });

    }





    super.onPrepareOptionsMenu(menu);
}

`

0 个答案:

没有答案