我想在代码中打开或关闭SwitchCompat小部件。我的意思是,当用户将SwitchCompat从打开更改为Off或其他方式时。我想用密码来做这个。我该怎么做?
SwitchCompat switchCompat = (SwitchCompat) findViewById(R.id.switch_compat);
发布于 2016-01-23 08:26:32
与任何CompoundButton一样,您可以以编程方式更改SwitchCompat的检查状态setChecked()。
发布于 2017-01-14 00:07:44
以编程方式使用SwitchCompat方法更改setChecked()控件的状态。
SwitchCompat switchCompat = (SwitchCompat) findViewById(R.id.switch_compat);
switchCompat.setChecked(true); //checked.或
switchCompat.setChecked(false); //unchecked.也可以直接在布局中定义选中的状态:
<android.support.v7.widget.SwitchCompat
android:id="@+id/myswitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked=true/>发布于 2020-08-28 09:31:25
在Kotlin中也可以使用switchCompat.isChecked = false。
https://stackoverflow.com/questions/34961252
复制相似问题