我想实现一个开关按钮,android.widget.Switch(可从 API v.14 获得)。
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Switch" />
但我不确定如何为按钮添加事件侦听器。它应该是一个“onClick”监听器吗?我怎么知道它是否“打开”?
原文由 Johan 发布,翻译遵循 CC BY-SA 4.0 许可协议
Switch 继承
CompoundButton
的属性,所以我会推荐 OnCheckedChangeListener