개발/코뮤니티

[앱 안드로이드] 7일차 : 6개 버튼 텍스트, 색깔 변경하기

Hyunsun 2021. 11. 18. 13:31
728x90

코뮤니티 모각코+ 앱 안드로이드 출석 인증 7일차

 

✅ 오늘의 문제 : 6개 버튼 텍스트, 색깔 변경하기

 

👉 여러분의 코틀린 코드와 실행 결과 캡쳐 이미지를 올려주세요.

 

✔ 6개의 버튼을 각각 변수 (val)로 만들어주세요.

✔ 우측 하단의 floatingActionButton(id는 runButton)을 클릭했을 때 6개 버튼의 텍스트와 배경색이 바뀌도록 만들어주세요.

 

코드

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">

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/runButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginEnd="32dp"
        android:layout_marginBottom="32dp"
        app:srcCompat="@android:drawable/ic_menu_rotate" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Button
            android:id="@+id/button1"
            android:layout_width="70dp"
            android:layout_height="80dp"
            android:backgroundTint="#99C8C2C2"
            app:cornerRadius="80dp" />

        <Button
            android:id="@+id/button2"
            android:layout_width="70dp"
            android:layout_height="80dp"
            android:backgroundTint="#99C8C2C2"
            app:cornerRadius="80dp" />

        <Button
            android:id="@+id/button3"
            android:layout_width="70dp"
            android:layout_height="80dp"
            android:backgroundTint="#99C8C2C2"
            app:cornerRadius="80dp" />

        <Button
            android:id="@+id/button4"
            android:layout_width="70dp"
            android:layout_height="80dp"
            android:backgroundTint="#99C8C2C2"
            app:cornerRadius="80dp" />

        <Button
            android:id="@+id/button5"
            android:layout_width="70dp"
            android:layout_height="80dp"
            android:backgroundTint="#99C8C2C2"
            app:cornerRadius="80dp" />

        <Button
            android:id="@+id/button6"
            android:layout_width="70dp"
            android:layout_height="80dp"
            android:backgroundTint="#99C8C2C2"
            app:cornerRadius="80dp" />

    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

 

MainActivity.kt

package com.cookandroid.myapplication

import android.content.res.ColorStateList
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import com.google.android.material.floatingactionbutton.FloatingActionButton
import java.util.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val bnt1 = findViewById<Button>(R.id.button1)
        val bnt2 = findViewById<Button>(R.id.button2)
        val bnt3 = findViewById<Button>(R.id.button3)
        val bnt4 = findViewById<Button>(R.id.button4)
        val bnt5 = findViewById<Button>(R.id.button5)
        val bnt6 = findViewById<Button>(R.id.button6)
        val array = arrayOf(bnt1, bnt2, bnt3, bnt4, bnt5, bnt6)

        findViewById<FloatingActionButton>(R.id.runButton).setOnClickListener {
            val random = Random()
            val list = mutableListOf<Int>()
            while (list.size<6){
                val num = random.nextInt(45)+1 // 1부터 시작
                if (list.contains(num)){ // 같은 번호가 있다면 넘어가기
                    continue
                }
                list.add(num)
            }

            for (i in 0..5){
                array[i].text = list[i].toString();

                val red = random.nextInt(256);
                val green = random.nextInt(256);
                val blue = random.nextInt(256);
                array[i].backgroundTintList = ColorStateList.valueOf(Color.rgb(red, green, blue))
            }
        }
    }
}

 

결과

 

 

안드로이드 APP 메이트 - 코틀린 기초편 :: #7-1. 코틀린 코드로 속성 변경하기 | 코드메이트

오늘은 드디어 코틀린 코드를 써볼 시간입니다. 우리가 열심히 만든 화면 레이아웃을 기반으로 코드를 추가하여 원하는 컴포넌트의 생성과 텍스트를 코드로 바꿔볼게요! 📌버튼이 눌렸는지

codemate.kr

 

728x90