java 如何在android中将RGB转换为HEXADECIMAL颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17739502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:50:20  来源:igfitidea点击:

how to convert RGB to HEXADECIMAL color in android?

javaandroidcolorshexargb

提问by ibu

How to convert ARGB(255 0 255 0)color to HEXADECIMALcolor. I have ARGB color in database and I retrieve using webservices in JSON format.
I want to put color in the text field TAG_DIFF_P (R.id.l7)

如何将ARGB(255 0 255 0)颜色转换为HEXADECIMAL颜色。我在数据库中有 ARGB 颜色,我使用 JSON 格式的网络服务进行检索。
我想在文本字段中添加颜色TAG_DIFF_P (R.id.l7)

here it my code, how to add color in background in the text field

这是我的代码,如何在文本字段的背景中添加颜色

try {

        JSONObject json1 = jParser.getJSONFromUrl(myUrl);
        // Getting Array of Contacts
        JSONArray list = json1.getJSONArray(TAG_JSONDataResult);


        // looping through All Contacts
        for(int i = 0; i < list.length(); i++){
            JSONObject c = list.getJSONObject(i);


            String GRPNAME = c.getString(TAG_GRPNAME);
            String QTY = c.getString(TAG_QNT);
            String BUDGET = c.getString(TAG_BUDGET);
            String STOCK = c.getString(TAG_STOCK);
            String DIFF = c.getString(TAG_DIFF);
            String DIFF_P = c.getString(TAG_DIFF_P);                
            String COLOR = c.getString(TAG_COLOR);


            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();

            // adding each child node to HashMap key => value

            map.put(TAG_GRPNAME, GRPNAME);
            map.put(TAG_QNT, QTY);
            map.put(TAG_BUDGET, BUDGET);
            map.put(TAG_STOCK, STOCK);
            map.put(TAG_DIFF, DIFF);
            map.put(TAG_DIFF_P, DIFF_P);


            // adding HashList to ArrayList
            contactList.add(map);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    /**
     * Updating parsed JSON data into ListView
     * */
     ListAdapter adapter = new SimpleAdapter(this, contactList,
            R.layout.list_item,
            new String[] {  TAG_GRPNAME, TAG_QNT, TAG_BUDGET, TAG_STOCK, TAG_DIFF, TAG_DIFF_P, },
            new int[] {
                     R.id.l2, R.id.l3, R.id.l4, R.id.l5, R.id.l6, R.id.l7});



            lv2.setAdapter(adapter);



}

回答by Veera

I think your json should has it as String. If it is so you can try this

我认为您的 json 应该将其作为字符串。如果是这样你可以试试这个

String hex = String.format("#%02x%02x%02x", r, g,b);

回答by Kumar Bibek

Check this Documentation on Color class

检查此颜色类的文档

http://developer.android.com/reference/android/graphics/Color.html#parseColor(java.lang.String)

http://developer.android.com/reference/android/graphics/Color.html#parseColor(java.lang.String)

Color color = Color.parse("#AARRGGBB");

If you have the colors in decimal, you can use.

如果你有十进制的颜色,你可以使用。

String hexValue = Integer.toHexString(255)