개발(Android)/android studio

[Android Studio/Kotlin] Buttons (2) Radio Group, Radio Button, Image Button, Floating Action Button, Snack Bar

shinyelee 2022. 5. 4. 16:55

[안드로이드 스튜디오/코틀린] 라디오 그룹과 라디오 버튼, 이미지 버튼, 플로팅 액션 버튼, 스낵바

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()
    }

}

RadioGroup, Radio Button

<!-- ConstraintLayout 하위 -->
    <RadioGroup
        android:id="@+id/radioGroup"
        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">

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="one" />

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="two" />

        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="three" />
            
    </RadioGroup>
// override fun onCreate 하위
        // 체크 기본값
        binding.radioGroup.check(R.id.radioButton3)
        // 체크 해제
//        binding.radioGroup.clearCheck()

        when(binding.radioGroup.checkedRadioButtonId) {
            R.id.radioButton1 -> println("1번")
            R.id.radioButton2 -> println("2번")
            R.id.radioButton3 -> println("3번")
        }


ImageButton

<!-- drawable 하위의 Vector Asset 삽입 -->
    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="100dp"
        android:layout_height="100dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:src="@drawable/ic_baseline_departure_board_24" />
        <!-- app:srcCompat 대신 android:src 사용해야 에러 안 뜸 -->


FloatingActionButton, Snackbar

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:tint="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_baseline_create_24"
        />
        binding.fab.setOnClickListener { view ->
            Snackbar.make(view, "스낵바 등장!", Snackbar.LENGTH_LONG).show()
        }

버튼을 클릭하면
화면 하단에 스낵바가 튀어나옴


참고

반응형