lbcab 2016-04-11 06:35 采纳率: 0%
浏览 2474
已结题

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);
}

}
  • 写回答

3条回答

  • 小小Q_Forler 2016-04-11 06:42
    关注

    android按钮被点击文字颜色变化效果:http://blog.csdn.net/maylian7700/article/details/6978131

    评论

报告相同问题?

悬赏问题

  • ¥15 slam rangenet++配置
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊