Tôi có một số nút mà tôi đặt màu nền là màu đỏ, xanh lục và xanh dương riêng biệt. Khi tôi bấm nút, bấm sự kiện được tạo ra, nhưng không có thay đổi nào trong gui cho người dùng biết nút được nhấn. Màu nền xám mặc định của nút Android thay đổi thành màu cam và trở lại màu xám sau khi phát hành trạng thái được nhấn. Làm thế nào để thực hiện điều này trên nút màu?Làm cách nào để thay đổi màu của nút màu khi được nhấn trong Android?
Trả lời
Điều đó được triển khai qua StateListDrawable
, được biểu thị bằng selector
trong XML. Tham khảo: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
Dưới đây là một ví dụ về một drawable rằng sẽ có màu trắng bởi mặc định, đen khi ép:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@android:color/black" /> <!-- pressed -->
<item android:drawable="@android:color/white" /> <!-- default -->
</selector>
Hãy thử cách này:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape>
<solid
android:color="#1E669B" />
<stroke
android:width="2dp"
android:color="#1B5E91" />
<corners
android:radius="6dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#1E669B"
android:endColor="#1E669B"
android:angle="270" />
<stroke
android:width="4dp"
android:color="#1B5E91" />
<corners
android:radius="7dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
Như đã đề cập bởi K-Ballo, bạn có thể sử dụng StateListDrawable để thực hiện một khung nhìn với các đồ họa khác nhau tùy thuộc vào trạng thái. Trong trường hợp của bạn, Button là chế độ xem trong đó hai trạng thái là nút được nhấn và nút không được nhấn.
Chúng ta cần tạo một file buttonselector.xml trong thư mục drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_not_pressed" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="@drawable/button_pressed" android:state_pressed="true"/>
</selector>
Tạo hai tập tin xml riêng cho trạng thái của nút
button_not_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FFFFFF"
android:centerColor="#FFFFFF"
android:endColor="#FFFFFF"
android:angle="270" />
</shape>
button_pressed .xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FF0000"
android:centerColor="#FF0000"
android:endColor="#FF0000"
android:angle="270" />
</shape>
Bạn sẽ thấy hai mã màu html # FF0000 & #FFFFFF thể hiện màu nền của nút theo trạng thái.
Trong bạn main.xml nơi bạn thiết lập các phong cách nút tùy chỉnh của bạn
<Button
android:id="@+id/customButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/buttonselector"
android:text="test"
android:textColor="#000000" />
Trong bạn lớp hoạt động thêm hai dòng sau
Button customButton = (Button) findViewById(R.id.customButton);
customButton.setBackground(getResources().getDrawable(R.drawable.buttonselector));
Hy vọng nó giúp
Câu trả lời kỹ lưỡng hơn nhiều! – Diesel
Thanks a lot Nó hoạt động hoàn hảo – amy