简体   繁体   English

如何使用Color.rgb?

[英]How do I use Color.rgb?

I was following a tutorial, and I was experimenting with some pieces of code, and found that my Application kept crashing when I input "aditya". 我正在学习一个教程,并且正在尝试一些代码,发现当我输入“ aditya”时我的应用程序一直崩溃。 I know I'm using Color.rgb wrong, but I don't know how. 我知道我使用Color.rgb错误,但是我不知道如何。

chkCmd.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String check = input.getText().toString();
            display.setText(check);
            f (check.equals("left")) {
                display.setGravity(Gravity.LEFT);
            } else if (check.equals("center")) {
                display.setGravity(Gravity.CENTER);
            } else if (check.equals("right")) {
                display.setGravity(Gravity.RIGHT);
            } **else if (check.equals("aditya")) {
                display.setText(Color.rgb(184, 134, 011));**

This line 这条线

display.setText(Color.rgb(184, 134, 011))

is trying to set the text on the display to Color.rgb(184, 134, 011) . 正在尝试将显示器上的文本设置为Color.rgb(184, 134, 011)

What you are trying to do is set the color of the text. 您想要做的是设置文本的颜色。 To accomplish this, write 为此,请写

display.setTextColor(Color.rgb(184, 134, 011))

before you set the text on the display. 在显示屏上设置文本之前。

You are trying to set the text to an integer of the parsed color as opposed to setting the color of the text. 您正在尝试将文本设置为解析后颜色的整数,而不是设置文本的颜色。 Try using this: 尝试使用此:

display.setTextColor(Color.rgb(184, 134, 011));

I think it crashes because Android sees an int as a Resource id, so it's not finding the resource and it crashes, try replacing 我认为它崩溃是因为Android将int作为资源ID,因此它没有找到资源并且崩溃了,请尝试替换

display.setText(Color.rgb(184, 134, 011));

to

display.setText(String.valueOf(Color.rgb(184, 134, 011)));

It should work 它应该工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM