4

这是我的代码,它对我不起作用。我使用示例中的代码。

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));
                    }
                }
            });
4

1 回答 1

1

setTextOff()尝试使用andsetTextOn()方法,而不是使用监听器。当检查状态更改时,该开关将自动处理正确的设置。

于 2015-11-16T14:52:50.000 回答