在java中填充十六进制字符串

时间:2014-08-24 18:28:08

标签: java android android-layout

我正在尝试构建一个十六进制字符串,用作颜色背景的十六进制代码。我完成了转换,但是当“转换”字符串小于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();
}
}

1 个答案:

答案 0 :(得分:0)

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