通常需要修改textview被选中时文字颜色,总是没效果,有以下几种方式可以实现:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:color="#FF111111"/> <!-- pressed --> <item android:state_focused="true" android:color="#FF222222"/> <!-- focused --> <item android:state_selected="true" android:color="#FF333333"/> <!-- selected --> <item android:state_active="true" android:color="#FF444444"/> <!-- active --> <item android:state_checkable="true" android:color="#FF555555"/> <!-- checkable --> <item android:state_checked="true" android:color="#FF666666"/> <!-- checked --> <item android:state_enabled="true" android:color="#FF777777"/> <!-- enabled --> <item android:state_window_focused="true" android:color="#FF888888"/> <!-- window_focused --> </selector>
在textview中,增加选择器: android:textColor="@drawable/text_wbcolor_selector"
1、可以增加点击事件,在后台设置,不管有没有点击事件:
textview.setOnClickListener(null);
2、在父控件中添加android:clickable=“true” android:focusable=“true”,而在子控件中添加android:duplicateParentState=“true”子控件就能获得父控件的点击事件。
3、在后台代码中设置ColorStateList,通过这个类也可以实现。
4、通过点击事件自己切换颜色(有点麻烦)。
二、下面通过后台代码创建选择器:
1、创建attrs文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="view_selector">
<attr name="view_image_normal" format="reference" />
<attr name="view_image_select" format="reference" />
<!-- 背景是否以图片显示,默认为false -->
<attr name="view_isimageable" format="boolean" />
<!--3 背景是颜色 -->
<attr name="view_bg_normal" format="color" />
<attr name="view_bg_select" format="color" />
<!-- 设置字体颜色 -->
<attr name="view_text_normal" format="color" />
<attr name="view_text_select" format="color" />
<!--在3的基础上是否显示圆角,默认是直角 -->
<attr name="view_bg_shape_cornerable" format="boolean" />
<!-- 圆角弧度 -->
<attr name="view_bg_shape_corners" format="dimension" />
<!-- 描边宽度 -->
<attr name="view_bg_shape_stroke_width" format="dimension" />
<!-- 描边颜色 -->
<attr name="view_bg_shape_stroke_color" format="color" />
</declare-styleable>
</resources>
2、基础Button类
package com.example.game2048.self;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.widget.Button;
/**
* 功能:
* 1、可以设置Button默认时的背景图片,选中时对应的背景图片
* 2、可以设置Button默认时的背景颜色,选中时对应的背景颜色
* 3、可以设置Button的圆角弧度
* 4、可以设置Button上面文字默认时的颜色,选中时对应的颜色
* 5、可以通过java代码创建selector
* @author libin
*
*/
public class StateButton extends Button {
private int STATE_PRESSED;
private int STATE_WINDOW_FOCUSED;
private int STATE_FOCUSED;
private int STATE_SELECTED;
private int shape_corner;
private int shape_stroke_width;
private int shape_stroke_color;
private int shape_normal;
private int shape_select;
private boolean shape_cornerable;
private boolean view_isimageable;
private Drawable normalDrawable;
private Drawable selectDrawable;
public StateButton(Context context) {
super(context);
}
public StateButton(Context context, AttributeSet attrs) {
super(context, attrs);
init(context,attrs);
}
private void init(Context context, AttributeSet attrs) {
STATE_PRESSED = android.R.attr.state_pressed;
STATE_WINDOW_FOCUSED = android.R.attr.state_window_focused;
STATE_FOCUSED = android.R.attr.state_focused;
STATE_SELECTED = android.R.attr.state_selected;
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.view_selector);//
view_isimageable = a.getBoolean(R.styleable.view_selector_view_isimageable, false);
if (view_isimageable) {//是图片
normalDrawable = a.getDrawable(R.styleable.view_selector_view_image_normal);
selectDrawable = a.getDrawable(R.styleable.view_selector_view_image_select);
if (normalDrawable==null || selectDrawable==null) {
new Throwable("background iamge normal or select color not set");
}
}else {
shape_normal = a.getColor(R.styleable.view_selector_view_bg_normal, 0);
shape_select = a.getColor(R.styleable.view_selector_view_bg_select, 0);
shape_stroke_color = a.getColor(R.styleable.view_selector_view_bg_shape_stroke_color, 0);
shape_corner = (int) a.getDimension(R.styleable.view_selector_view_bg_shape_corners, 0);
shape_stroke_width = (int) a.getDimension(R.styleable.view_selector_view_bg_shape_stroke_width, 0);
shape_cornerable = a.getBoolean(R.styleable.view_selector_view_bg_shape_cornerable, false);
if (shape_normal==0 || shape_select==0) {
new Throwable("background normal or select color not set");
}
}
//设置字体状态颜色
int text_normal = a.getColor(R.styleable.view_selector_view_text_normal, 0);
int text_select = a.getColor(R.styleable.view_selector_view_text_select, 0);
a.recycle();
backgroudColorState();
if (text_normal != 0 && text_select != 0) {
this.setTextColor(createColorStateList(text_normal, text_select));
}
}
/**
* 设置背景状态改变时的颜色或图片
*/
private void backgroudColorState() {
if (view_isimageable) {
this.setBackground(createStateListDrawable(normalDrawable,selectDrawable));
}else {
if (shape_cornerable) {
this.setBackground(createStateListDrawable(
createGradientDrawable(shape_normal),
createGradientDrawable(shape_select)));
} else {
this.setBackground(createStateListDrawable(getResources()
.getDrawable(R.drawable.red),
getResources().getDrawable(R.drawable.balck)));
}
}
}
/** 对View设置不同状态时其文字颜色。 */
private ColorStateList createColorStateList(int normal, int pressed) {
//colors里面存放的事颜色值而不是颜色id,如果是自定义颜色需要使用getResources().getColor(R.color.white)
int[] colors = new int[] { pressed, pressed, normal, pressed, normal };
int[][] states = new int[5][];
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[] {};
ColorStateList colorList = new ColorStateList(states, colors);
return colorList;
}
/**
* 创建StateListDrawable
* @param normal 默认显示的drawable
* @param select 选中显示的drawable
* @return
*/
private StateListDrawable createStateListDrawable(Drawable normal,Drawable select){
StateListDrawable stalistDrawable = new StateListDrawable();
// 触摸时
stalistDrawable.addState(new int[] { STATE_PRESSED, STATE_WINDOW_FOCUSED },select);
// 选中时
stalistDrawable.addState(new int[] { STATE_SELECTED },select);
// 获得焦点时
stalistDrawable.addState(new int[] { STATE_FOCUSED },select);
// 没有任何状态时显示的图片,我们给它设置我空集合 默认图片
stalistDrawable.addState(new int[] {},normal);
return stalistDrawable;
}
/**
* 对view加边框
* @param fillColor
* @return
*/
private GradientDrawable createGradientDrawable(int fillColor) {
GradientDrawable gd = new GradientDrawable();// 创建drawable
gd.setColor(fillColor);
gd.setCornerRadius(shape_corner);
gd.setStroke(shape_stroke_width, shape_stroke_color);
return gd;
}
/**
* 创建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;
}
}
3、布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:view="http://schemas.android.com/apk/res/com.example.game2048.self"
android:id="@+id/logoLayout_entry"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#f5f5f5"
android:orientation="vertical"
android:padding="20dip" >
<com.example.game2048.self.StateButton
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="匿名登陆"
android:textSize="30sp"
view:view_bg_normal="#C1EA4F"
view:view_bg_select="#8EBC16"
view:view_bg_shape_cornerable="true"
view:view_bg_shape_corners="10dip"
view:view_bg_shape_stroke_color="#FF0000"
view:view_text_normal="#53BEFC"
view:view_text_select="#026199"
view:view_bg_shape_stroke_width="1dip" />
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:background="@color/apply_tab_normal"
android:text="匿名登陆"
android:textColor="@drawable/test"
android:textSize="30sp" />
<com.example.game2048.self.StateButton
android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="匿名登陆"
android:textSize="30sp"
view:view_bg_normal="#C1EA4F"
view:view_bg_select="#8EBC16" />
<com.example.game2048.self.StateButton
android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="匿名登陆"
android:textSize="30sp"
view:view_isimageable="true"
view:view_image_normal="@drawable/red"
view:view_image_select="@drawable/white" />
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="匿名登陆"
/>
</LinearLayout>