Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีเปลี่ยนสีของ CheckBox ใน Android ด้วย Kotlin

ภาพรวม

ตัวอย่างนี้สาธิตวิธีการเปลี่ยนสีข้อความและสีพื้นหลังของ CheckBox ใน Android โดยโปรแกรมด้วย Kotlin

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

สร้างโปรเจกต์ Android ใหม่ใน Android Studio

ขั้นตอนที่ 2: จัดรูปแบบหน้าจอหลัก (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp"
        android:background="#008080"
        android:padding="5dp"
        android:text="TutorialsPoint"
        android:textColor="#fff"
        android:textSize="24sp"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Click the check box"
        android:textColor="@android:color/holo_blue_dark"
        android:textSize="16sp"
        android:textStyle="bold" />
    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView"
        android:layout_centerInParent="true"
        android:text="Check box" />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/checkbox"
        android:layout_centerInParent="true"
        android:text="Change Color" />
</RelativeLayout>

ขั้นตอนที่ 3: เขียนโค้ด MainActivity.kt

import android.graphics.Color
import android.os.Bundle
import android.widget.Button
import android.widget.CheckBox
import androidx.appcompat.app.AppCompatActivity

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

        val checkBox: CheckBox = findViewById(R.id.checkbox)
        val button: Button = findViewById(R.id.button)

        button.setOnClickListener {
            // เปลี่ยนสีข้อความเป็นมะจันตา (MAGENTA)
            checkBox.setTextColor(Color.MAGENTA)
            // เปลี่ยนสีพื้นหลังเป็นสีเขียวอ่อน (#cbff75)
            checkBox.setBackgroundColor(Color.parseColor("#cbff75"))
        }
    }
}

ขั้นตอนที่ 4: ตั้งค่า AndroidManifest.xml

ใช้การตั้งค่ามาตรฐาน

ทดสอบการทำงาน

รันแอป กดปุ่ม "Change Color" CheckBox จะเปลี่ยนสีข้อความและพื้นหลังทันที

วิธีอื่นๆ สำหรับการปรับแต่ง CheckBox

  • XML Attribute: ใช้ android:buttonTint (API 21+) สำหรับสีเครื่องหมายถูก
  • Theme/Style: กำหนด style ใน styles.xml ใช้ colorAccent หรือ colorControlActivated
  • Compound Drawables: ใช้ setButtonDrawable() สำหรับรูปภาพกำหนดเอง
  • MaterialCheckBox: ใช้ Material Components library เพื่อการปรับแต่งที่ทันสมัยกว่า