【问题标题】:setTextcolor by using getCurrentTextColor()使用 getCurrentTextColor() 设置文本颜色
【发布时间】:2015-11-06 07:17:12
【问题描述】:

我需要从 TextView 中获取当前文本颜色,然后将此值分配给 TextView.setTextColor()。但是我得到一个很大的 int -1979711488138,我怎样才能从中得到颜色?

【问题讨论】:

  • 您有什么要求?
  • tv.setTextColor(tv2.getTextColors());

标签: android colors textview int


【解决方案1】:
Integer intColor = -1979711488138;
String hexColor = "#" + Integer.toHexString(intColor).substring(2);

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

【讨论】:

  • 这只是因为整数溢出。
【解决方案2】:
 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);
    }

 }

【讨论】:

    【解决方案3】:

    假设您想将颜色从textView1 设置为textView,那么您可以这样做:

    textView.setTextColor(textView1.getCurrentTextColor());
    

    【讨论】:

      【解决方案4】:

      您不能将数字放入 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 值的黑色,因为这对系统来说更昂贵。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-15
        • 2011-11-23
        • 2013-07-29
        • 2023-03-11
        • 1970-01-01
        相关资源
        最近更新 更多