1
1

More than 3 years have passed since last update.

【Kotlin/Android】switch(トグル)を非活性にする

Posted at
  • switchをクリックしたら、3秒間の間クリックできないように実装しました。
  • isClickableが正しいです。
  • isEnableは3秒間の間、意図していないのに色が濃いグレーになってしまいます。 ※isClickableのグレーは、自分で指定した温度です。  2019-11-19 16.11.39.png
MainFragment.kt
val switch: Switch = view.findViewById(R.id.main_switch)
// switchの最小の長さを設定しています。
switch.switchMinWidth = PhoneInfo.getPhoneWidth(this.context) / 6
switch.isChecked = true
switch.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
// ONのとき
switch.isClickable = false
Handler().postDelayed({ switch.isClickable = true }, Constants.CLICKABLE_TIME)
} else {
// ONのとき
}
main_fragment.xml
<!-- themeでswitchの色を設定しています -->
<Switch
android:id="@+id/main_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/CustomSwitch"></Switch>
styles.xml
<resources>
<style name="CustomSwitch" parent="Theme.AppCompat.Light">
<!--ボタンON時のボタンの通り道・ボタンとタッチフィードバックカラー-->
<item name="colorControlActivated">@color/switch_on_color</item>
<!--ボタンOFF時のボタンの通り道-->
<item name="android:colorForeground">@color/switch_off_color</item>
<!-- OFFボタンを押したときのタッチフィードバックカラー -->
<item name="colorControlHighlight">@color/switch_off_color</item>
</style>
</resources>
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1