写过android的代码相信大家对Selector并不陌生吧,下面来看看这段xml文件是如何定义的
<?xml version="1.0" encoding="utf-8" ?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 触摸时并且当前窗口处于交互状态 --> <item android:state_pressed="true" android:state_window_focused="true" android:drawable= "@drawable/pic1" /> <!-- 触摸时并且没有获得焦点状态 --> <item android:state_pressed="true" android:state_focused="false" android:drawable="@drawable/pic2" /> <!--选中时的图片背景--> <item android:state_selected="true" android:drawable="@drawable/pic3" /> <!--获得焦点时的图片背景--> <item android:state_focused="true" android:drawable="@drawable/pic4" /> <!-- 窗口没有处于交互时的背景图片 --> <item android:drawable="@drawable/pic5" /> </selector>
上面的代码估计很多人都会经常的使用到,除了在xml里面写之外,我们还可以在代码中通过ColorStateList和StateListDrawable来设置Selector,ColorStateList主要是用于颜色的状态,StateListDrawable这个主要是用于设置图片的状态,当然使用这种代码编写的方法我们可以动态设置TextView、Button、ImageView等组件在不同状态下的背景/前景显示效果。而不需要固定死。
package lab.sodino.statelist; import android.app.Activity; import android.content.Context; import android.content.res.ColorStateList; import android.graphics.drawable.Drawable; import android.graphics.drawable.StateListDrawable; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.TextView; /** * 对TextView设置ColorStateList使其在Normal、Pressed、Focused、Unable四种状态下显示不同的颜色。<br/> * StateListDrawable可直接使用图片应用在相似场合。 */ public class ActColorStateList extends Activity implements OnClickListener { private TextView txtShow; public void onCreate(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); } /** 对TextView设置不同状态时其文字颜色。 */ private ColorStateList createColorStateList(int normal, int pressed, int focused, int unable) { 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 = new ColorStateList(states, colors); return colorList; } /** 设置Selector。 */ 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; } @Override public void onClick(View v) { if (v == txtShow) { txtShow.setEnabled(false); } } }
我们看到在上面中出现 states[5] = new int[] {};和bg.addState(new int[] {}, normal);这些就是表示在不是上面任何一种状态的时候默认的值,像上面的这种状态android.R.attr.state_pressed,android.R.attr.state_enabled对应都是在xml中的值都是为true,即android:state_pressed="true"类似这个值,那么如果我这里想要设置为false时可以在前面加上一个 “-”负数的符号,比如bg.addState(new int[] {-android.R.attr.state_focused }, focused);那么这个就是表示state_focused=false时的值为focused。
还有我们可以读取xml中设定的颜色值:比如
ColorStateList mIdleColorState = getResources().getColorStateList(idleStateSelector); int colorNormal=colorStateList.getColorForState(new int[]{android.R.attr.state_enabled}, 0); int colorDisabled=colorStateList.getColorForState(new int[]{-android.R.attr.state_enabled}, 0);//“-”负号表示对应的属性值为false
itchq