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

    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)