SoFunction
Updated on 2025-03-11

Analysis of the instance code of the instance when the image and button presses the status of the button in Android

1. Set the background selector for the picture so that you can click or set whether to select or not, and switch background

res/drawable/selector_settings_item_back.xml 

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:andro> 
  <item android:state_focused="true" android:drawable="@color/settingsSelectedItem"/> 
  <item android:state_pressed="true" android:drawable="@color/settingsSelectedItem"/> 
  <item android:state_selected="true" android:drawable="@color/settingsSelectedItem"/> 
  <item android:state_focused="false" android:drawable="@color/settingsItem"/> 
</selector > 

Color value definition:

res/values/ 

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
  <color name="settingsItem">#ffffff</color> 
  <color name="settingsSelectedItem">#FFA500</color> 
</resources> 

2. Round corner button, press and lift to switch background, and switch text color at the same time

res/layout/activity_xxx.xml 

&lt;Button 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_weight="1" 
android:background="@drawable/selector_shape_corner_button" 
  android:text="Approval" android:textColor="@drawable/selector_font_style_corner_button" 
  android:textSize="13sp" 
  /&gt; 

It refers to two selectors under res/drawable/.

One is the background image toggles with the click and lift state, and the other is the text color toggles with the click and lift state.

res/drawable/selector_shape_corner_button.xml 

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:andro> 
  <item android:state_focused="true" android:drawable="@drawable/shape_corner_button_fill"/> 
  <item android:state_pressed="true" android:drawable="@drawable/shape_corner_button_fill"/> 
  <item android:state_selected="true" android:drawable="@drawable/shape_corner_button_fill"/> 
  <item android:state_focused="false" android:drawable="@drawable/shape_corner_button"/> 
</selector >
 res/drawable/selector_font_style_corner_button 

shape_corner_button.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:andro 
  android:shape="rectangle"> 
  <corners 
    android:radius="5dp" /> 
  <solid 
    android:color="#001da1f2" /> 
  <stroke 
    android:width="1dp" 
    android:color="#1da1f2" /> 
</shape> 

shape_corner_button_fill.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape 
xmlns:andro 
  android:shape="rectangle"> 
  <corners 
    android:radius="5dp" /> 
  <solid 
    android:color="#ff1da1f2" /> 
  <stroke 
    android:width="1dp" 
    android:color="#1da1f2" /> 
</shape> 
<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:andro> 
  <item android:state_focused="true" android:color="#ffffff"/> 
  <item android:state_pressed="true" android:color="#ffffff"/> 
  <item android:state_selected="true" android:color="#ffffff"/> 
  <item android:state_focused="false" android:color="#1da1f2"/> 
</selector > 

Summarize

The above is the code analysis of the example code of the picture and button press status change in Android introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!