"Android UI" case 04 configuration control Click Effect (selector)

Source: Internet
Author: User

This example uses XML (selector) to configure the implementation of the control Click Effect, that is, when the control is triggered or clicked to get to the focus, the change in style occurs in order to give a better user experience and operational sense. This example requires the introduction of the selector.xml of the core knowledge points. Please refer to Learning:http://blog.csdn.net/mahoking/article/details/23690857. This example shows the control for the click Effect as TextView, Button.

"Reproduced use, please specify the source: http://blog.csdn.net/mahoking "
The first step is to configure the Selector.xml file, because the test control required for this example contains the TextView and button, so this case requires two XML files to be configured under/res/drawable. respectively: Seletor_04_click_textview.xml, Seletor_04_click_button.xml.
Seletor_04_click_textview.xml

<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android ">    <item android:state_pressed=" true "android:color=" #18A960 "/>    <item Android:state_ Focused= "true" android:color= "#18A960"/>    <item android:state_selected= "true" android:color= "#18A960"/ >    <item android:color= "@android: Color/black" ></item></selector>


Seletor_04_click_button.xml

<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "/http Schemas.android.com/apk/res/android "> <item android:state_pressed=" true "android:drawable=" @drawable/button _pressed "/> <item android:state_focused=" true "android:drawable=" @drawable/button_pressed "/> <item Android Oid:state_selected= "true" android:drawable= "@drawable/button_pressed"/> <item android:drawable= "@drawable/ Button_default "></ITEM></SELECTOR> 

Seletor_04_click_textview.xml is designed to change the font color of TextView, Seletor_04_click_ Button.xml is designed to change the background image of a button, so we need to prepare two different images for the button to show the effect of the change.
Then the main layout file activity_04_seletor.xml. Special attention is paid to the following areas:
Configuration of the TextView control (text color)
Android:textcolor= "@drawable/seletor_04_click_textview"
Configuration of the button control (background picture)
android:background= "@drawable/seletor_04_click_button"

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:gravity=" center "and roid:orientation= "vertical" > <textview android:layout_height= "wrap_content" android:layout_width= "Match_par Ent "android:layout_marginbottom=" 10DP "android:text=" The following demo TextView, click the text color change "android:gravity=" center "/& Gt;<textview android:layout_height= "30DP" android:layout_width= "match_parent" android:id= "@+id/activity_04_       Seletor_textview "android:layout_margintop=" 10DP "android:text=" test using TextView "android:textsize=" 25DP " Android:textcolor= "@drawable/seletor_04_click_textview" android:gravity= "center" Android:layout_marginbott om= "40DP"/> <textview android:layout_height= "wrap_content" android:layout_width= "Match_parent" Android oid:layout_margintop= "10DP" aNdroid:text= "The following demo button, click on the background picture change" android:textcolor= "@drawable/seletor_04_click_textview" Android:layout_marg Inbottom= "10DP" android:gravity= "center"/> <button android:layout_width= "Wrap_content" Android:layo ut_height= "Wrap_content" android:id= "@+id/activity_04_seletor_button" android:gravity= "center" android:b ackground= "@drawable/seletor_04_click_button" android:layout_marginbottom= "60DP"/></linearlayout>

      the last Write activity (selectoractivity) is as follows and runs the project after completion.

public class Selectoractivity extends Activity {private TextView textview;private button button; @Overrideprotected void O Ncreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_04_ Seletor); Initviews ();} /** * Initialize view */private void Initviews () {TextView = (TextView) Findviewbyid (r.id.activity_04_seletor_textview); button = (B Utton) Findviewbyid (R.id.activity_04_seletor_button); Mainonclicklistener Clicklistener = new Mainonclicklistener (); Textview.setonclicklistener (ClickListener); Button.setonclicklistener (Clicklistener);} Private class Mainonclicklistener implements view.onclicklistener{@Overridepublic void OnClick (View v) {int viewId = v.ge TId (); switch (viewId) {case R.id.activity_04_seletor_button:toast.maketext (Getapplicationcontext (), "background picture changed", Toast.length_short). Show (), Break;case R.id.activity_04_seletor_textview:toast.maketext (GetApplicationContext (), "Text color Changed", Toast.length_short). Show (); break;default:break;}}}


Effect:


"Reproduced use, please specify the source: http://blog.csdn.net/mahoking "


"Android UI" case 04 configuration control Click Effect (selector)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.