Selectorhepler Tool Class
Generally need to click on the effect or selected, the focus of the state change after the change effect, you need to create a new XML file under the Drawable folder, and then write a selector. Relatively troublesome, so I wrote a quick code to build a Selector tool class.
Selectorhepler Code:
ImportAndroid.content.Context;ImportAndroid.content.res.ColorStateList;Importandroid.graphics.drawable.AnimationDrawable;Importandroid.graphics.drawable.Drawable;Importandroid.graphics.drawable.InsetDrawable;Importandroid.graphics.drawable.LayerDrawable;Importandroid.graphics.drawable.StateListDrawable; Public class selectorhepler { Private StaticSelectorhepler util; Public StaticSelectorheplergetinstance() {if(Util = =NULL) {util =NewSelectorhepler (); }returnUtil }Private Selectorhepler() {Super(); }/** * Incoming drawable ID, get a selector, general to setbackgrounddrawable use * @param context * @param ID Normal * @param idpressed * @param idfocused * @param idunable * @return * * PublicStatelistdrawablegetselectordrawable(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 }/** * Get Click to change the status of selector, generally to setbackgrounddrawable use * @param context * @param idnormal * @param idpressed * @return * * PublicStatelistdrawablegetpressedselectordrawable(Context context,intIdnormal,intidpressed) {drawable normal = Idnormal = =-1?NULL: Context.getresources (). getdrawable (Idnormal); drawable pressed = idpressed = =-1?NULL: Context.getresources (). getdrawable (idpressed); Statelistdrawable bg = getpressedselectordrawable (normal, pressed);returnBg }/** * Get Click to change the status of selector, generally to setbackgrounddrawable use * @param normal * @param pressed * @return * * PublicStatelistdrawablegetpressedselectordrawable(drawable Normal, drawable pressed) {Statelistdrawable BG =NewStatelistdrawable (); Bg.addstate (New int[] {Android. R.attr.state_pressed, Android. r.attr.state_enabled}, pressed); Bg.addstate (New int[] {Android. R.attr.state_enabled}, Normal); Bg.addstate (New int[] {}, normal);returnBg }/** get selected to change the state of selector, generally to setbackgrounddrawable use * @param context * @param idnormal * @param idchecked * @return * * PublicStatelistdrawablegetcheckedselectordrawable(Context context,intIdnormal,intidchecked) {drawable normal = Idnormal = =-1?NULL: Context.getresources (). getdrawable (Idnormal); drawable checked = idchecked = =-1?NULL: Context.getresources (). getdrawable (idchecked); Statelistdrawable bg = getcheckedselectordrawable (normal, checked);returnBg }/** * Get selected to change the status of selector, generally to setbackgrounddrawable use * @param normal * @param checked * @return * * PublicStatelistdrawablegetcheckedselectordrawable(drawable Normal, drawable checked) {Statelistdrawable BG =NewStatelistdrawable (); Bg.addstate (New int[] {Android. R.attr.state_checked, Android. r.attr.state_enabled}, checked); Bg.addstate (New int[] {Android. R.attr.state_enabled}, Normal); Bg.addstate (New int[] {}, normal);returnBg }/** * Get focus change that changes the state of selector, generally to setbackgrounddrawable use * @param context * @param idnormal * @param idchecked * @return * * PublicStatelistdrawablegetfocusedselectordrawable(Context context,intIdnormal,intidchecked) {drawable normal = Idnormal = =-1?NULL: Context.getresources (). getdrawable (Idnormal); drawable checked = idchecked = =-1?NULL: Context.getresources (). getdrawable (idchecked); Statelistdrawable bg = getfocusedselectordrawable (normal, checked);returnBg }/** * Get focus changes that change the state of the selector, generally to setbackgrounddrawable use * @param normal * @param focused * @return * * PublicStatelistdrawablegetfocusedselectordrawable(drawable Normal, drawable focused) {Statelistdrawable BG =NewStatelistdrawable (); Bg.addstate (New int[] {Android. R.attr.state_enabled, Android. R.attr.state_focused}, focused); Bg.addstate (New int[] {Android. R.attr.state_focused}, focused); Bg.addstate (New int[] {Android. R.attr.state_enabled}, Normal); Bg.addstate (New int[] {}, normal);returnBg }/** * Get selector that can change the status, generally to settextcolor use * @param normal * @param pressed * @pa Ram focused * @param Unable * @param checked * @return * * * PublicColorstatelistgetcolorstatelist(intNormalintPressedintFocusedintUnable,intChecked) {int[] Colors =New int[] {pressed, focused, normal, focused, unable, checked, 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_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[] {Android. R.attr.state_checked, Android. R.attr.state_enabled}; states[6] =New int[] {}; Colorstatelist colorlist =NewColorstatelist (states, colors);returnColorList; }/** * Get Click to change the status of selector, generally to settextcolor use * @param normal * @param pressed * @re Turn * * PublicColorstatelistGetpressedselectorcolor(intNormalintPressed) {int[] Colors =New int[] {pressed, normal, normal};int[] states =New int[3][]; states[0] =New int[] {Android. R.attr.state_pressed, Android. R.attr.state_enabled}; states[1] =New int[] {Android. R.attr.state_enabled}; states[2] =New int[] {}; Colorstatelist colorlist =NewColorstatelist (states, colors);returnColorList; }/** * Get selected to change the status of selector, generally to settextcolor use * @param normal * @param pressed * @re Turn * * PublicColorstatelistGetcheckedselectorcolor(intNormalintPressed) {int[] Colors =New int[] {pressed, normal, normal};int[] states =New int[3][]; states[0] =New int[] {Android. R.attr.state_checked, Android. R.attr.state_enabled}; states[1] =New int[] {Android. R.attr.state_enabled}; states[2] =New int[] {}; Colorstatelist colorlist =NewColorstatelist (states, colors);returnColorList; }/** * Combine multiple images to generate a drawable * @param down * @param up * @return */ PublicLayerdrawablegetlayerdrawable(drawable down, drawable up) {drawable[] layerlist = {NewInsetdrawable (Down,0,0,0,0),NewInsetdrawable (UP,0,0,0,0) };return NewLayerdrawable (layerlist); }/** * Create a continuous drawable animation with multiple images * @param time * @param Dr * @return */
PublicAnimationdrawablegetanimationdrawable(intTime, drawable ... dr) {Animationdrawable animationdrawable =NewAnimationdrawable (); for(inti =0; i < dr.length; i++) {animationdrawable.addframe (dr[i], time); } animationdrawable.setoneshot (false); Animationdrawable.start ();returnanimationdrawable; }}
How to use:
Colorstatelist color = Selectorhepler. getinstance(). Getpressedselectorcolor(Color. White, Color. BLUE);Statelistdrawable drawable = Selectorhepler. getinstance(). Getpressedselectordrawable(This, R. drawable. IC_normal, R. drawable. IC_pressed);Tv. SetTextColor(color);Tv. Setbackgrounddrawable(drawable);
Quickly build a selector tool class with Code Selectorhepler