我試圖在單擊時更改Button
的背景顏色,當我釋放Button
時按下Button
時,我可以更改背景顏色該按鈕返回到默認值顏色。我不知道該從哪裏出發,我一直在試圖找到解決這個問題的辦法。Android單擊後更改背景顏色
`
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.game.dice.MainActivity">
<Button
android:id="@+id/but1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
android:background="@drawable/button_color"
/>
<Button
android:id="@+id/but2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
android:background="@drawable/button_color"/>
</LinearLayout>
`
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@color/button_pressed"/>
<item android:state_focused="true"
android:drawable="@color/button_focused"/>
<item android:drawable="@color/button_default"/>
</selector>
是否必須通過XML?否則,您可以使用按鈕OnClickListener。 –