繁体   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