问题描述
- android:自定义button继承自button, 调用setTextcolor无效
-
现在有个需求是当button失去焦点时, text文字的颜色会变化.
1. 在attrs.xml中我声明了两个属性normalColor和selectedColor,2. 然后在自定义button类中:
TypedArray typeArray = mContext.obtainStyledAttributes(attrs, R.styleable.LtButton);if (null != typeArray) {
int typeArrayLength = typeArray.length();
for (int i = 0; i < typeArrayLength; ++i) {
int idx = typeArray.getIndex(i);
switch (idx) {
case R.styleable.LtButton_lttext:
setText(typeArray.getString(idx)); //此方法没用不能设置button的text
break;
case R.styleable.LtButton_normalColor:
mNormalColor = typeArray.getColor(idx, Color.WHITE);
setTextColor(mNormalColor);
break;
case R.styleable.LtButton_selectedColor:
mSelectedColor = typeArray.getColor(idx, Color.BLACK);
这里会导致button没有了背景.
3. 重载setSelected方法, 没有用
@Override
public void setSelected(boolean selected) {
super.setSelected(selected);
if(selected) {
setTextColor(mSelectedColor);
} else {setTextColor(mNormalColor);
}}
解决方案
android按钮被点击文字颜色变化效果:http://blog.csdn.net/maylian7700/article/details/6978131
解决方案二:
Android 自定义Button
Androidの自定义圆角按钮button
解决方案三:
<?xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android">
解决方案四:
android:textColor 也可以设置selector的
<?xml version="1.0" encoding="utf-8"?>
<item android:color="@color/color_enable" android:state_enabled="true"/>
<item android:color="@color/color_checked" android:state_checked="true"/>
<item android:color="@color/default" />