使用getCurrentTextColor()设置setTextcolor

时间:2015-08-13 10:54:29

标签: android colors textview int

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

4 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:1)

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

textView.setTextColor(textView1.getCurrentTextColor());

答案 2 :(得分: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);
    }

 }

答案 3 :(得分: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值,因为系统更贵,这真的很奇怪。