簡體   English   中英

在Java中填充十六進制字符串

[英]padding a hex string in java

我試圖建立一個十六進制字符串,用作彩色背景的十六進制代碼。 我已經完成了轉換,但是“轉換后的”字符串小於16時出現了一個問題-因為該數字只有一位數字長。 您將在我的代碼中看到我嘗試制作一種方法來檢查ID是否為短字符串,如果是,則在其前面填充一個0 ...但是我無法進行填充。 所以我最后仍然是長度為1的字符串。

package com.example.android.test;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnValueChangeListener;

public class MainActivity extends Activity {

static final int MIN_VAL = 0;
static final int MAX_VAL = 255;
NumberPicker alpha, red, green, blue;
View view;
String redVal = "00", blueVal = "00", greenVal = "00", alphaVal = "FF";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    alpha = (NumberPicker) findViewById(R.id.alphaPicker);
    red = (NumberPicker) findViewById(R.id.redPicker);
    green = (NumberPicker) findViewById(R.id.greenPicker);
    blue = (NumberPicker) findViewById(R.id.bluePicker);

    alpha.setMinValue(MIN_VAL);
    alpha.setMaxValue(MAX_VAL);
    alpha.setWrapSelectorWheel(false);

    red.setMinValue(MIN_VAL);
    red.setMaxValue(MAX_VAL);
    red.setWrapSelectorWheel(false);

    green.setMinValue(MIN_VAL);
    green.setMaxValue(MAX_VAL);
    green.setWrapSelectorWheel(false);

    blue.setMinValue(MIN_VAL);
    blue.setMaxValue(MAX_VAL);
    blue.setWrapSelectorWheel(false);

    view = findViewById(R.id.color_box);
    view.setBackgroundColor(0xFF808000);

    alpha.setOnValueChangedListener(new OnValueChangeListener() {

        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            // TODO Auto-generated method stub

            alphaVal = Integer.toHexString(newVal);
            pad(alphaVal);
            String color = generateColor(alphaVal, redVal, greenVal, blueVal);
//                view.setBackgroundColor(color);
            Log.v(color, "was selected");
        }
    });

    red.setOnValueChangedListener(new OnValueChangeListener() {

        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {


            redVal = Integer.toHexString(newVal);
            pad(redVal);


            String color = generateColor(alphaVal, redVal, greenVal, blueVal);
//                view.setBackgroundColor(color);
            Log.v(redVal, "was selected");
        }
    });

}
//  if the integer is less than 16, you need to convert the int to a hex string and pad a 0 in
front
private static final String pad(String s) {

    if(s.length() < 2){
        StringBuilder theColor = new StringBuilder("0")
                .append(s);
        return theColor.toString();
    }
    return s;
}

public String generateColor(String alphaVal, String redVal, String greenVal, String blueVal) {


    StringBuilder theColor = new StringBuilder("0x")
            .append(alphaVal)
            .append(redVal)
            .append(greenVal)
            .append(blueVal);
    return theColor.toString();
}
}

您可以使用String.format("%02x", newVal)代替使用Integer.toHexString(newVal)

暫無
暫無

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

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