使用getCurrentTextColor()设置文本颜色

6
我需要从TextView获取当前文本颜色,然后将此值分配给TextView.setTextColor()。 但我获得了一个大整数-1979711488138,如何从中获取颜色?

你的需求是什么? - Mahesh Suthar
1
tv.setTextColor(tv2.getTextColors()); - Rustam
4个回答

4
Integer intColor = -1979711488138;
String hexColor = "#" + Integer.toHexString(intColor).substring(2);

或者

String hexColor = String.format("#%06X", (0xFFFFFF & intColor));

这仅仅是因为整数溢出才会产生某种效果。 - headsvk

2
假设您想将从 textView1textView 的颜色设置为红色,那么您可以这样做:
textView.setTextColor(textView1.getCurrentTextColor());

0

你无法将该数字放入int中,我得到的是-1979711488,即#8A000000,即带有138个alpha的黑色。您可以像这样获取颜色的所有部分:

int color = getCurrentTextColor();
int a = Color.alpha(color);
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);

为什么默认文本颜色不是一种纯色,而是带有 alpha 值的黑色,这对系统来说更加昂贵,这真的很奇怪。


0
 public class MainActivity extends Activity
 {

    private TextView txtViewIpLable,textView1;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        textView1.setTextColor(txtViewIpLable.getTextColors());
    }
    private void init() 
    {
        // TODO Auto-generated method stub
       txtViewIpLable = (TextView) findViewById(R.id.txtViewIpLable);
       textView1 = (TextView) findViewById(R.id.textView1);
    }

 }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接