簡體   English   中英

如何將 String 轉換為 Int? 這個值是十六進制還是 dex?

[英]How can I convert String to Int? will this value hex or dex?

在java中我有

color_value="FFAAFF";

我試過了:

int color = Integer.parseInt(color_value);`

如何將其轉換為 int(hex) 以添加到im_view.setBackgroundColor(int color); ?

由於我們必須猜測您正在使用的 lang,我假設它是 java,您實際需要的是從 HEX 轉換為 RGB,如果您對 AWT 感到滿意,那么您可以使用內置的 func:

String color_value = "FFAAFF";
im_view.setBackgroundColor(Color.decode(color_value));

如果不想使用AWT,那么你可以做像這樣

/**
 * 
 * @param colorStr e.g. "#FFFFFF"
 * @return 
 */
public static Color hex2Rgb(String colorStr) {
    return new Color(
            Integer.valueOf( colorStr.substring( 1, 3 ), 16 ),
            Integer.valueOf( colorStr.substring( 3, 5 ), 16 ),
            Integer.valueOf( colorStr.substring( 5, 7 ), 16 ) );
}



im_view.setBackgroundColor(hex2Rgb(color_value));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM