android根据View的不同状态更换不同的背景

来源:互联网 发布:淘宝首页都设置在哪 编辑:程序博客网 时间:2024/05/20 02:24

本文出自:http://www.androidkaifa.com/thread-244-1-1.html

StateListDrawable可以根据View的不同状态,更换不同的背景
可以应用如EditText,Button等中,以Button为例
系统中默认的按钮被按下的颜色和未点击时的颜色不一样,这种效果我们的实现可以用Java代码或是XML实现,以前我们一般是通过XML布局文件设置VIEW的背景,现在我们用java代码实现一下这效果,
以Java代码:
public class TestButton extends Activity {
   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Integer[] mButtonState = { R.drawable.defaultbutton,
                R.drawable.focusedpressed, R.drawable.pressed };
        Button mButton = (Button) findViewById(R.id.button);
        MyButton myButton = new MyButton(this);
        mButton.setBackgroundDrawable(myButton.setbg(mButtonState));
    }

    class MyButton extends View {

        public MyButton(Context context) {
            super(context);
        }
        //以下这个方法也可以把你的图片数组传过来,以StateListDrawable来设置图片状态,来表现button的各中状态。未选 中,按下,选中效果。
        public StateListDrawable setbg(Integer[] mImageIds) {
            StateListDrawable bg = new StateListDrawable();
            Drawable normal = this.getResources().getDrawable(mImageIds[0]);
            Drawable selected = this.getResources().getDrawable(mImageIds[1]);
            Drawable pressed = this.getResources().getDrawable(mImageIds[2]);
            bg.addState(View.PRESSED_ENABLED_STATE_SET, pressed);
            bg.addState(View.ENABLED_FOCUSED_STATE_SET, selected);
            bg.addState(View.ENABLED_STATE_SET, normal);
            bg.addState(View.FOCUSED_STATE_SET, selected);
            bg.addState(View.EMPTY_STATE_SET, normal);
            return bg;
        }
    }

}
下面再一个完整的示例:
/**
* 对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);  
        }  
    }  
}  
下面我们把XML的实现方式也贴出来,大家可以对两种实现方式对比一下,以便我们在编写代码时,在合适的时候使用合适的方式 实现:
//注意一下这个文件是在放在drawable文件夹内的,
<?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/btn_selected"/>  
    <item android:state_focused="true" android:drawable="@drawable/btn_selected"/>  
    <item android:state_enabled="true" android:drawable="@drawable/btn_normal"/>  
    <item  android:drawable="@drawable/btn_normal" />  
在Button的xml中进行加载:
<Button  
           android:id="@+id/canel"  
           android:layout_width="wrap_content"  
           android:layout_height="wrap_content"   
           android:text="@string/btn_cancel"  
           android:layout_margin="10dip"  
           android:layout_weight="1"  
           android:textColor="#ffffffff"  
           android:textSize="15sp"  
           android:background="@drawable/button_drawable"  
           />
也可以在JAVA代码中加载它:
canel.setBackgroundDrawable(R.drawable.button_drawable);  //它并不是一个图片,是一个xml文件,
</selector>