Android 如何从菜单项获取开关的选中状态
我已在菜单项中添加了开关,并试图访问其选中状态。但我无法访问其选中状态。有人能帮我吗 这是我的菜单_crime.xmlAndroid 如何从菜单项获取开关的选中状态,android,android-layout,menuitem,Android,Android Layout,Menuitem,我已在菜单项中添加了开关,并试图访问其选中状态。但我无法访问其选中状态。有人能帮我吗 这是我的菜单_crime.xml <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/crime_toggle"
android:title="Crime"
android:enabled="true"
app:showAsAction="always"
app:actionLayout="@layout/toolbar_crime_toggle" />
</menu>
您可以使用SwitchCompat.isChecked()方法在任何地方找到SwitchCompat的选中状态
如果它给出true则表示开关打开,否则关闭您可以使用SwitchCompat.isChecked()方法在任何地方找到SwitchCompat的选中状态
如果它给出true则表示开关打开否则关闭您如何尝试访问它?共享更多代码我想在我的活动中访问它显示不起作用的代码我已编辑了问题请检查您如何尝试访问它?共享更多代码我想在我的活动中访问它显示不工作的代码我已编辑问题请检查
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.SwitchCompat
android:id="@+id/crime_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/SCBSwitch"
android:padding="10dp"
android:text="Crime"
android:switchPadding="50dp"/>
</RelativeLayout>
mSwitchShowSecure = (SwitchCompat) menu.findItem(R.id.crime_toggle).getActionView().findViewById(R.id.crime_switch);
mSwitchShowSecure.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d("ischecked", String.valueOf(isChecked));
}
});