[안드로이드 스튜디오/코틀린] 스위치, 토글 버튼, 체크 박스
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
package com.shinyelee.android_study2
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.shinyelee.android_study2.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private var vBinding : ActivityMainBinding? = null
private val binding get() = vBinding!!
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
vBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
}
override fun onDestroy() {
vBinding = null
super.onDestroy()
}
}
Switch
<!-- ConstraintLayout 하위 -->
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
// override fun onCreate 하위
binding.switch1.setOnCheckedChangeListener { compoundButton, b ->
println(b)
}
binding.switch1.isChecked = true
ToggleButton
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
binding.toggleButton.setOnCheckedChangeListener { compoundButton, b ->
println(b)
}
binding.toggleButton.isChecked = true
CheckBox
<CheckBox
android:id="@+id/checkBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CheckBox"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
binding.checkBox.setOnCheckedChangeListener { compoundButton, b ->
println(b)
}
binding.checkBox.isChecked = true
참고
반응형
'개발(Android) > android studio' 카테고리의 다른 글
[Android Studio/Kotlin] Views (1) Image View, Web View (0) | 2022.05.05 |
---|---|
[Android Studio/Kotlin] Buttons (2) Radio Group, Radio Button, Image Button, Floating Action Button, Snack Bar (0) | 2022.05.04 |
[Android Studio/Kotlin] Tab Layout (0) | 2022.05.03 |
[Android Studio/Kotlin] Bottom Navigation View and View Pager (0) | 2022.05.03 |
[Android Studio/Kotlin] List View (0) | 2022.05.03 |