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

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

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

ตัวอย่างนี้สาธิตวิธีเปลี่ยนสีข้อความ (Text Color) และสีพื้นหลัง (Background Color) ของ CheckBox โดยโปรแกรมเมื่อกดปุ่ม

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

เปิด Android Studio เลือก File → New Project และกรอกข้อมูลที่จำเป็น

ขั้นตอนที่ 2: จัดทำ Layout (res/layout/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:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="คลิกที่ CheckBox" />
    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Check Box"
        android:layout_below="@id/tv"
        android:paddingRight="15dp"/>
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="เปลี่ยนสี"
        android:layout_below="@id/checkbox" />
</RelativeLayout>

ขั้นตอนที่ 3: เขียนโค้ดหลัก (src/MainActivity.java)

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        final CheckBox checkBox = findViewById(R.id.checkbox);
        Button button = findViewById(R.id.button);
        
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // เปลี่ยนสีข้อความเป็นแดง
                checkBox.setTextColor(Color.RED);
                // เปลี่ยนสีพื้นหลังเป็นเขียวอ่อน
                checkBox.setBackgroundColor(Color.parseColor("#cbff75"));
            }
        });
    }
}

ขั้นตอนที่ 4: กำหนดค่าใน AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

การทดสอบ

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

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

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

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

  • Button Tint: ใช้ android:buttonTint="@color/your_color" ใน XML (API 21+)
  • Custom Drawable: สร้าง selector drawable สำหรับ android:button เพื่อปรับรูปแบบถูก/ไม่ถูกเอง
  • Material CheckBox: ใช้ com.google.android.material.checkbox.MaterialCheckBox รองรับ theme attributes