在代码中实现背景单击变换颜色和TextView变换文字颜色

效果:
点击前:
[img]http://dl2.iteye.com/upload/attachment/0093/6747/421353dc-c6b1-3518-8f00-13d7ba03cc37.png[/img]
点击中:
[img]http://dl2.iteye.com/upload/attachment/0093/6749/78eb3e2e-6064-36fb-8067-eaa78486830f.png[/img]

布局文件中代码:

...
<LinearLayout android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="60dip" android:orientation="vertical" android:clickable="true">
<TextView android:textSize="20sp" android:gravity="center" android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="match_parent" android:text="按钮"/>
</LinearLayout>
...


在代码中设置这些颜色:

...
@Override
protected void onCreate(Bundle savedInstanceState) {
...
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
TextView textView = (TextView) findViewById(R.id.textView);

setPressedBg(layout,"#00ffff","#00ff00","#00ff00");
setTextPressedBg(textView,"#000000","#ffffff","#ffffff");
textView.setDuplicateParentStateEnabled(true); //子视图与父控件perssed 等状态保持一致
}

/**
* 设置单击背景色
* @param view 控件
* @param normal 未选择时的背景色
* @param focused 选择时的背景色
* @param pressed 选择时的背景色
*/
public void setPressedBg(View view, String normal,String focused, String pressed) {
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 ,android.R.attr.state_window_focused};
states[4] = new int[] { android.R.attr.state_window_focused };

StateListDrawable bg = new StateListDrawable();
bg.addState(states[0], new ColorDrawable(Color.parseColor(pressed)));
bg.addState(states[3], new ColorDrawable(Color.parseColor(focused)));
bg.addState(states[2], new ColorDrawable(Color.parseColor(normal)));

view.setBackground(bg);
}

/**
* 对TextView设置不同状态时其文字颜色。
* @param textView 控件
* @param normal 未选择时的文字颜色
* @param pressed 选择时的文字颜色
* @param focused 选择时的文字颜色
*/
private void setTextPressedBg(TextView textView,String normal, String pressed, String focused) {
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[] {};

int normalColor = Color.parseColor(normal);
int pressedColor = Color.parseColor(pressed);
int focusedColor = Color.parseColor(focused);

ColorStateList colorList = new ColorStateList(states, new int[] {pressedColor,focusedColor,normalColor,focusedColor,focusedColor,normalColor});

textView.setTextColor(colorList);
}
...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值