开关按钮默认不勾选
Switch button is not checked by default
我在操作栏中有开关并且工作正常,但我想通过 XML android:checked="true"
中的设置以及
中的代码进行默认检查
Switch switchButton=new Switch(getActivity());
switchButton.setEnabled(true);
当应用程序启动时,默认情况下始终未选中。这是我的开关代码
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.switch_menu,menu);
MenuItem switchItem=menu.findItem(R.id.toggle_loc);
Switch switchButton=new Switch(getActivity());
switchButton.setEnabled(true);
MenuItemCompat.setActionView(switchItem,switchButton);
switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
Log.d(TAG,"Switch Button Is Checked");
}
else {
Log.d(TAG,"Switch Button Is UnChecked");
}
}
});
R.menu.switch_menu
<?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/toggle_loc"
android:title=""
android:visible="true"
app:actionLayout="@layout/switch_button"
app:showAsAction="ifRoom">
</item>
</menu>
layout/switch_button"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Switch_Location"
android:enabled="true"
android:checked="true"
/>
</RelativeLayout>
请帮我把这个开关设为默认选中。
使用
switchButton.setChecked(true);
你也做错了,比如:
Switch switchButton=new Switch(getActivity());
你应该这样做
Switch switchButton = (Switch) switchItem.getActionView().findViewById(R.id.Switch_Location);
所以它应该看起来像:
Switch switchButton = (Switch) switchItem.getActionView().findViewById(R.id.Switch_Location);
switchButton.setChecked(true);
我在操作栏中有开关并且工作正常,但我想通过 XML android:checked="true"
中的设置以及
Switch switchButton=new Switch(getActivity());
switchButton.setEnabled(true);
当应用程序启动时,默认情况下始终未选中。这是我的开关代码
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.switch_menu,menu);
MenuItem switchItem=menu.findItem(R.id.toggle_loc);
Switch switchButton=new Switch(getActivity());
switchButton.setEnabled(true);
MenuItemCompat.setActionView(switchItem,switchButton);
switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
Log.d(TAG,"Switch Button Is Checked");
}
else {
Log.d(TAG,"Switch Button Is UnChecked");
}
}
});
R.menu.switch_menu
<?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/toggle_loc"
android:title=""
android:visible="true"
app:actionLayout="@layout/switch_button"
app:showAsAction="ifRoom">
</item>
</menu>
layout/switch_button"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Switch_Location"
android:enabled="true"
android:checked="true"
/>
</RelativeLayout>
请帮我把这个开关设为默认选中。
使用
switchButton.setChecked(true);
你也做错了,比如:
Switch switchButton=new Switch(getActivity());
你应该这样做
Switch switchButton = (Switch) switchItem.getActionView().findViewById(R.id.Switch_Location);
所以它应该看起来像:
Switch switchButton = (Switch) switchItem.getActionView().findViewById(R.id.Switch_Location);
switchButton.setChecked(true);