这是我的代码,它对我不起作用。我使用this示例中的代码。
Xml -
<android.support.v7.widget.SwitchCompat
android:id="@+id/visibility_switch"
android:layout_width="wrap_content"
android:layout_height="55dp"
android:layout_marginRight="@dimen/mat_card_padding"
app:showText="false" />和代码:
mVisibilitySwitchCompat = (SwitchCompat) findViewById(R.id.visibility_switch);
mVisibilitySwitchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// this method doesn't work!
if (isChecked) {
mVisibilityTextView.setText(getString(R.string.visibility_on));
} else {
mVisibilityTextView.setText(getString(R.string.visibility_off));
}
}
});发布于 2015-11-16 22:52:50
尝试使用setTextOff()和setTextOn()方法,而不是使用侦听器。当选中状态改变时,开关将自动处理设置正确的状态。
https://stackoverflow.com/questions/33738016
复制相似问题