(+84) 236.3827111 ex. 402

Sử dụng Button trong Android


Sử dụng Button trong Android, thành phần biểu diễn nút bấm, bắt sự kiện click nên Button cũng như thiết lập màu nền, màu chữ, Drawable khi thay đổi trạng thái Button

Button trong Android

Button là một loại View, nó hiện thị nút bấm để chờ người dùng bấm vào. Button kế thừa từ TextView nên các thuộc tính, thiết lập cho TextView ở phần trước là có hiệu quả như đối với Button (Bạn nên đọc trước phần đó)

Cú pháp khai báo Button trong XML

<>
    android:id="@+id/mybutton_id"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="Xin chào" />

Lấy Button và bắt sự kiện Click

Từ Activity hoặc từ đối tượng ViewGroup chứa Button, để lấy Button và bắt sự kiện khi người dùng bấm vào thì làm như sau:

final Button button = findViewById(R.id.mybutton_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
        //Muốn làm gì khi bấm vào Button thì
        //Viết code ở đây - Ví dụ:
        Toast.makeText(MainActivity.this, "Hello!",
            Toast.LENGTH_SHORT).show();
    }
});

Một số thuộc tính

Do Button mở rộng từ TextView nên nó có đầy đủ các thuộc tính của TextView như: android:text, android:textColor, android:textSize, android:textStyle, android:textAllCaps ... Ngoài các thuộc tính được kế thừa đó ra có một số thuộc tính cần lưu ý:

  • Gán Drawable vào các biên nút bấm

Dùng các thuộc tính android:drawableLeft, android:drawableRight, android:drawableTop, android:drawableBottom để gán các ảnh Drawable vào biên trái, phải, trên, dưới của nút bấm.
Nếu muốn thiết lập khoảng cách các ảnh Drawable đến vùng nội dung dùng thuộc tính android:drawablePadding
Ví dụ có 2 drawable như sau:

drawable/button_top_bottom.xml (vẽ một hình chữ nhật 15dp x 100 dp)

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size android:height="15dp" android:width="100dp" />
    <solid android:color="#fdd109" />
shape>

drawable/button_left_right.xml (vẽ một hình tròn đường kính 30dp)

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <size android:height="30dp" android:width="30dp" />
    <solid android:color="#800789" />
shape>

Áp dụng vào Button như sau:

<>
    android:id="@+id/mybutton_id"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="Xin chào"
    android:drawablePadding="10dp"
    android:drawableLeft="@drawable/button_left_right"
    android:drawableRight="@drawable/button_left_right"
    android:drawableTop="@drawable/button_top_bottom"
    android:drawableBottom="@drawable/button_top_bottom"/>

  • hông bo viền Button

Mặc định các nút bấm có bo viền xung quanh và có các hiệu ứng khi bấm giữ, click vào. Nếu muốn Button không vẽ viền này nhưng vẫn thấy hiệu ứng khi nhấn vào thì thiết lập

style="?android:attr/borderlessButtonStyle"
  • Tùy biến nền Button

Màu nền

Có thể gán màu nền cho Button bằng thuộc tính android:background, ví dụ:

android:background="#edffac06"

Cách thiết lập vào background giá trị màu theo HEX, theo giá trị màu như vậy thì nền đã thay đổi như mong muốn, tuy nhiên hiệu ứng khi nhấn, hiệu ứng khi Button đang focus bị mất.

Nền Drawable / Ảnh Bitmap

android:background cũng dùng để gán một Drawable cho Button, ví dụ có:

drawable/button_oval_bg.xml (Drawable vẽ một hình oval)

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <size android:height="50dp" android:width="100dp" />
    <solid android:color="#c22ace" />
shape>

Gán nó cho Button bằng: android:background="@drawable/button_oval_bg"

Nếu có các ảnh PNG đạng 9-patch (tạo ảnh 9-patch bằng các phần mềm vẽ ảnh như Photoshop, xem thêm:

https://www.blog/NhatMinh.com/vidu/ButtonDemo

Bạn đưa vào dự án, và gán cho android:background, nó sẽ dựng nền theo dạng 9-patch (thu/phóng phần nội dung và các phần góc giữ nguyên)

android:background="@drawable/ic_orange_button"

Nền StateListDrawable

Tùy loại phần tử View có nhận các trạng thái: activated, enabled, checked, selected, focused ...

Với Button quan tâm đến ba trạng thái:

  • pressed khi nhấn vào nút
  • selected khi chọn nút
  • normal khi trạng thái bình thường
  • disabled khi nút bấm bị vô hiệu

Ứng với mỗi trạng thái này, bạn có thể thiết lập một Drawable để Button vẽ khi ở trạng thái đó. Các trạng thái muốn gán Drawable thì định nghĩa vào một file XML theo cấu trúc:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item />
    <item />
    
    <item />
selector>

Với mỗi item định nghĩa ra một Drawable cho trạng thái muốn gán. Ví dụ, selector có 2 phần tử cho trạng thái bình thường và trạng thái khi nhấn

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:drawable="@drawable/drawable_pressed">
 
    <item android:drawable="@drawable/button_normal">
selector>

Trong code trên thì drawable_pressed và button_normal là 2 Drawable, android:state_pressed="true" cho biết item đó dành cho trạng thái khi pressed, item không thiết lập state đó là dành cho trạng thái normal.

Ngay trong các item của selector cũng có thể viết XML Drawable mà không cần gán từ ngoài như thuộc tính android:drawable="@drawable/drawable_pressed" ở trên. Ví dụ sau sử dụng selector với 2 item, một dành cho ở trạng thái bình thường, một dành cho khi bấm, Drawable của item viết ngay trong item.

drawable/button_3state.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#ce3f1b" />
            <stroke
                android:width="1dp"
                android:color="#ce0f0f" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        shape>
    item>
     <item>
        <shape>
            <gradient
                android:startColor="#70c656"
                android:endColor="#53933f"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#53933f" />
            <corners
                android:radius="4dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        shape>
    item>
selector>

Gán vào Button bằng

android:background="@drawable/button_3state"

Colored Button

Mỗi Button có thể thiết lập màu nền nhanh chóng mà vẫn giữ khả năng vẽ ở nhiều trạng thái bằng cách thiết lập: android:backgroundTint để thiết lập màu nền, đồng thời thêm style="@style/Widget.AppCompat.Button.Colored"

<Button
    android:id="@+id/mybutton_id"
    android:layout_height="wrap_content"
    android:layout_width="220dp"
    android:text="Xin chào"
    android:backgroundTint="#d50000"
    style="@style/Widget.AppCompat.Button.Colored" />
<Button
    android:layout_height="wrap_content"
    android:layout_width="220dp"
    android:text="Xin chào"
    android:backgroundTint="#9c27b0"
    style="@style/Widget.AppCompat.Button.Colored" />
<Button
    android:layout_height="wrap_content"
    android:layout_width="220dp"
    android:text="Xin chào"
    android:backgroundTint="#311b92"
    style="@style/Widget.AppCompat.Button.Colored" />
<Button
    android:layout_height="wrap_content"
    android:layout_width="220dp"
    android:text="Xin chào"
    android:backgroundTint="#00acc1"
    style="@style/Widget.AppCompat.Button.Colored" />
<Button
    android:layout_height="wrap_content"
    android:layout_width="220dp"
    android:text="Xin chào"
    android:backgroundTint="#00c853"
    style="@style/Widget.AppCompat.Button.Colored" />
<Button
    android:layout_height="wrap_content"
    android:layout_width="220dp"
    android:text="Xin chào"
    android:backgroundTint="#00c853"
    style="@style/Widget.AppCompat.Button.Colored" />
<Button
    android:layout_height="wrap_content"
    android:layout_width="220dp"
    android:text="Xin chào"
    android:backgroundTint="#ffd600"
    style="@style/Widget.AppCompat.Button.Colored" />
<Button
    android:layout_height="wrap_content"
    android:layout_width="220dp"
    android:text="Xin chào"
    android:backgroundTint="#000000"
    style="@style/Widget.AppCompat.Button.Colored" />

  • android:minHeight="0dp"android:minWidth="0dp" giảm kích thước của Button (loại bỏ khoảng giống padding của Button)

android:maxLines="1" thiết lập Button chỉ hiện thị một dòng Text

(còn tiếp)