这是我的代码,它对我不起作用。我使用此示例中的代码。
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));
}
}
});