简体   繁体   中英

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". I know I'm using Color.rgb wrong, but I don't know how.

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) .

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

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

to

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

It should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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