Java Code Settings Selector

Source: Internet
Author: User

/** * Set Picture selector * * @param context * @param idnormal * @param idpressed * @param idfocused  * @param idunable * @return */public static statelistdrawable Newselector (context context, int idnormal, int            idpressed, int idfocused, int idunable) {statelistdrawable bg = new statelistdrawable (); 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_set bg.addstate (new int[] {Android. R.attr.state_pressed, Android.            r.attr.state_enabled}, pressed); View.enabled_focused_state_set bg.addstate (New int[] {aNdroid. R.attr.state_enabled, Android.            R.attr.state_focused}, focused); View.enabled_state_set bg.addstate (new int[] {Android.            R.attr.state_enabled}, Normal); View.focused_state_set bg.addstate (new int[] {Android.            R.attr.state_focused}, focused); View.window_focused_state_set bg.addstate (new int[] {Android.            R.attr.state_window_focused}, unable);            View.empty_state_set bg.addstate (new int[] {}, normal);    return BG; }
 usage: btn.setBackgroundDr Awable (Newselector (this, r.drawable.btn_normal, R.drawable.btn_selected,<span style= "White-space:pre" ></ span>                              r. drawable.btn_selected, r.drawable.btn_unable)); 
<pre name= "code" class= "HTML" >/** * Set a different state when its text color * @param normal * @param pressed * @param focused * @param unable  * @return */private colorstatelist createcolorstatelist (int normal, int pressed, int cheched, int focused, int unable)        {int[] colors = new int[] {pressed, cheched, focused, normal, focused, unable, normal};        Int[][] states = new int[7][]; States[0] = new int[] {Android. R.attr.state_pressed, Android.        R.attr.state_enabled}; STATES[1] = new int[] {Android. R.attr.state_checked, Android.        R.attr.state_enabled}; STATES[2] = new int[] {Android. R.attr.state_enabled, Android.        R.attr.state_focused}; STATES[3] = new int[] {Android.        R.attr.state_enabled}; STATES[4] = new int[] {Android.        R.attr.state_focused}; STATES[5] = new int[] {Android.        R.attr.state_window_focused};        STATES[6] = new int[] {};        Colorstatelist colorlist = new Colorstatelist (states, colors);    return colorlist; }

Usage: Radiobutton.settextcolor (createcolorstatelist (Color.Black, color.red, Color.Red, Color.Red, Color.BLACK));

Java Code Settings 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.