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

วิธีเปลี่ยนสีปุ่ม (Button) เมื่อคลิกใน Android

การเปลี่ยนสีพื้นหลัง Button เมื่อถูกกด (Click)

ตัวอย่างนี้แสดงวิธีเปลี่ยนสี Button ทันทีที่ผู้ใช้คลิก โดยใช้ setBackgroundColor() และดึงสีจาก Resource

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

Android Studio → File → New Project → กรอกรายละเอียด → Finish

ขั้นตอนที่ 2: Layout - res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="คลิกเพื่อเปลี่ยนสี!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="HardcodedText" />
</androidx.constraintlayout.widget.ConstraintLayout>

ขั้นตอนที่ 3: MainActivity.java

package app.com.sample;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button button = findViewById(R.id.button);
        button.setOnClickListener(v -> 
            button.setBackgroundColor(getResources().getColor(R.color.colorPrimary))
        );
    }
}

ขั้นตอนที่ 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>

วิธีอื่นๆ ในการเปลี่ยนสี Button

1. ใช้ StateListDrawable (Selector) - แนะนำสำหรับ Production

สร้าง res/drawable/button_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/colorPrimaryDark" />
    <item android:state_focused="true" android:drawable="@color/colorAccent" />
    <item android:drawable="@color/colorPrimary" /> <!-- default -->
</selector>

ใช้ใน Layout: android:background="@drawable/button_selector"

2. ใช้ ColorStateList สำหรับ Text Color

<selector xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@android:color/white" />
    <item android:color="@android:color/black" />
</selector>

3. MaterialButton (Material Components)

<com.google.android.material.button.MaterialButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Material Button"
    app:backgroundTint="@color/colorPrimary"
    app:rippleColor="@color/colorAccent" />

4. ใช้ setBackgroundTintList (API 21+)

ColorStateList csl = ColorStateList.valueOf(Color.RED);
button.setBackgroundTintList(csl);

การทดสอบ

รันแอป → คลิกปุ่ม "คลิกเพื่อเปลี่ยนสี!" → สีพื้นหลังเปลี่ยนเป็น colorPrimary