Android Colorstatelist and statelistdrawable settings selector

Source: Internet
Author: User

I've written Android code and I'm sure you're not a stranger to selector, so let's see how this XML file is defined.

    <?XML version= "1.0" encoding= "Utf-8"?>         <selectorxmlns:android= "Http://schemas.android.com/apk/res/android">         <!--when touched and the current window is in interactive state -          <Itemandroid:state_pressed= "true"android:state_window_focused= "true"android:drawable= "@drawable/pic1" />        <!--touch and do not get focus state -          <Itemandroid:state_pressed= "true"android:state_focused= "false"android:drawable= "@drawable/pic2" />          <!--picture Background When selected -          <Itemandroid:state_selected= "true"android:drawable= "@drawable/pic3" />           <!--picture background when getting focus -          <Itemandroid:state_focused= "true"android:drawable= "@drawable/pic4" />          <!--The window does not have a background image at the time of interaction -          <Itemandroid:drawable= "@drawable/pic5" />       </selector>  

The above code estimates that many people will often use, in addition to writing in the XML, we can also in the code through the Colorstatelist and statelistdrawable to set the selector,colorstatelist Mainly for the state of color, statelistdrawable this is mainly used to set the state of the picture, of course, using this code to write the method we can dynamically set the TextView, Button, ImageView and other components in different states of the background/foreground display effect. Without the need to fix death.

 Packagelab.sodino.statelist;Importandroid.app.Activity;ImportAndroid.content.Context;Importandroid.content.res.ColorStateList;Importandroid.graphics.drawable.Drawable;Importandroid.graphics.drawable.StateListDrawable;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.TextView;/*** Set Colorstatelist to TextView to show different colors in normal, Pressed, Focused, unable four states. <br/> * statelistdrawable can be used in similar situations using images directly. */ Public classActcolorstatelistextendsActivityImplementsOnclicklistener {PrivateTextView txtshow;  Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); Txtshow=(TextView) Findviewbyid (r.id.txtshow); Txtshow.settext ("sodino\nnormal:0xffffffff\npressed:0xffffff00\nfocused:0xff0000ff\nunable:0xffff0000"); Txtshow.settextcolor (Createcolorstatelist (0xFFFFFFFF, 0xffffff00, 0XFF0000FF, 0xffff0000)); Txtshow.setonclicklistener ( This); }    /**The text color of the TextView when different states are set. */    PrivateColorstatelist Createcolorstatelist (intNormalintPressedintFocusedintunable) {        int[] Colors =New int[] {pressed, focused, normal, focused, unable, normal}; int[] states =New int[6][]; states[0] =New int[] {Android. R.attr.state_pressed, Android.        R.attr.state_enabled}; states[1] =New int[] {Android. R.attr.state_enabled, Android.        R.attr.state_focused}; states[2] =New int[] {Android.        R.attr.state_enabled}; states[3] =New int[] {Android.        R.attr.state_focused}; states[4] =New int[] {Android.        R.attr.state_window_focused}; states[5] =New int[] {}; Colorstatelist colorlist=Newcolorstatelist (states, colors); returncolorlist; }    /**set the selector. */     Public StaticStatelistdrawable Newselector (Context context,intIdnormal,intIdpressed,intidfocused,intidunable) {statelistdrawable bg=Newstatelistdrawable (); drawable Normal= Idnormal = =-1?NULL: Context.getresources (). getdrawable (Idnormal); Drawable pressed= Idpressed = =-1?NULL: Context.getresources (). getdrawable (idpressed); Drawable focused= Idfocused = =-1?NULL: Context.getresources (). getdrawable (idfocused); drawable unable= Idunable = =-1?NULL: Context.getresources (). getdrawable (idunable); //View.pressed_enabled_state_setBg.addstate (New int[] {Android. R.attr.state_pressed, Android.        r.attr.state_enabled}, pressed); //View.enabled_focused_state_setBg.addstate (New int[] {Android. R.attr.state_enabled, Android.        R.attr.state_focused}, focused); //View.enabled_state_setBg.addstate (New int[] {Android.        R.attr.state_enabled}, Normal); //View.focused_state_setBg.addstate (New int[] {Android.        R.attr.state_focused}, focused); //View.window_focused_state_setBg.addstate (New int[] {Android.        R.attr.state_window_focused}, unable); //View.empty_state_setBg.addstate (New int[] {}, normal); returnBG; } @Override Public voidOnClick (View v) {if(v = =txtshow) {txtshow.setenabled (false); }    }}

We see it appearing in the abovestates[5] = new int[] {}; and Bg.addstate (new int[] {}, normal); These are the default values that indicate when not in any of the above states, like this one aboveAndroid. R.attr.state_pressed,Android. R.attr.state_enabledThe corresponding values in the XML are all true, i.e.android:state_pressed= "true" Similar to this value, then if I want to set to false here can be preceded by a "-" negative sign, such as Bg.addstate (new int[] {-android. R.attr.state_focused}, focused); then this is the value of focused that represents state_focused=false.

And we can read the color values set in the XML: for example,

Colorstatelist midlecolorstate = getresources (). Getcolorstatelist (idlestateselector); int colornormal=colorstatelist.getcolorforstate (newint[]{android. R.attr.state_enabled}, 0);  int colordisabled=colorstatelist.getcolorforstate (newint[]{-android. R.attr.state_enabled}, 0); // "-" minus sign indicates that the corresponding property value is False

Colorstatelist and statelistdrawable settings in Android 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.