iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 21
0
Mobile Development

菜雞 Android APP 從安裝 Android Studio 到上架 Google Play系列 第 21

Day21 - Switch 控制靜音

我們也可以用 switch 調整是否靜音

xml

<Switch
        android:id="@+id/switch1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Switch"
        android:layout_margin="20dp"
        android:textSize="22dp"
        app:layout_constraintBottom_toTopOf="@id/textView"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="開啟"
        android:textSize="22dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

和昨天一樣傳入 Service 控制

這邊我們只貼文字改變使用方法,用同樣的邏輯去處理就行了

Fragment

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    switch1.setOnCheckedChangeListener { buttonView, isChecked ->

        when(isChecked){
            true -> textView.text = "開啟"
            false -> textView.text = "關閉"
        }
    }

}

Yes


上一篇
Day20 - SeekBar 控制音量
下一篇
Day22 - 下載安裝 Eclipse ( 聊天室 )
系列文
菜雞 Android APP 從安裝 Android Studio 到上架 Google Play30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言