1

我有一个 parsecolor 异常的问题,当我的值低于 20 时,应用程序崩溃,其余的都很好......你能告诉我为什么吗?

我的简单代码是:

公共类 ColoreTesto 扩展 Activity {

private SeekBar seekred, seekgreen, seekblu;
private TextView seekred_txt, seekgreen_txt, seekblu_txt;
private TextView esempio;
private int redtxt, greentxt, blutxt;
private int progressred, progressgreen, progressblu;

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

    /* RIFERIMENTO AL GRUPPO DI VARIABILI SHARED*/
    final SharedPreferences settings = getSharedPreferences("settings", 0);
    final SharedPreferences.Editor editor = settings.edit();

    esempio = (TextView)findViewById(R.id.txt_esempio);

    /****
     * SEEKBAR RED
     ****/
    seekred = (SeekBar) findViewById (R.id.seekRed);
    seekred.setMax(255);
    seekred_txt = (TextView)findViewById(R.id.textRed);
    /*CARICARE IL VALORE DELLA SEEKBAR*/
    redtxt = settings.getInt("redtxt", 255); /*UNA VARIABILE DEL GRUPPO*/
    seekred_txt.setText(redtxt + getString(R.string.red));
    seekred.setProgress(redtxt);
    // CAMBIAMENTO DEL VALORE DELLA SEEKBAR
    seekred.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        //spostamento
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
            seekred_txt.setText(progress + getString(R.string.red));
            esempio.setTextColor(Color.parseColor("#" + Integer.toHexString(progress)  +  Integer.toHexString(greentxt) + Integer.toHexString(blutxt)));
        }

        //inizio dello spostamento
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        //fine dello spostamento
        public void onStopTrackingTouch(SeekBar seekBar) {
            redtxt = seekBar.getProgress();

        }
    });


    /****
     * SEEKBAR GREEN
     ****/
    seekgreen = (SeekBar) findViewById (R.id.seekGreen);
    seekgreen.setMax(255);
    seekgreen_txt = (TextView)findViewById(R.id.textGreen);
    /*CARICARE IL VALORE DELLA SEEKBAR*/
    greentxt = settings.getInt("greentxt", 255); /*UNA VARIABILE DEL GRUPPO*/
    seekgreen_txt.setText(greentxt + getString(R.string.green));
    seekgreen.setProgress(greentxt);
    // CAMBIAMENTO DEL VALORE DELLA SEEKBAR
    seekgreen.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        //spostamento
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
            seekgreen_txt.setText(progress + getString(R.string.green));
            esempio.setTextColor(Color.parseColor("#" + Integer.toHexString(redtxt) + Integer.toHexString(progress) + Integer.toHexString(blutxt)));
        }

        //inizio dello spostamento
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        //fine dello spostamento
        public void onStopTrackingTouch(SeekBar seekBar) {
            greentxt = seekBar.getProgress();               
        }
    });


    /****
     * SEEKBAR BLU
     ****/
    seekblu = (SeekBar) findViewById (R.id.seekBlu);
    seekblu.setMax(255);
    seekblu_txt = (TextView)findViewById(R.id.textBlu);
    /*CARICARE IL VALORE DELLA SEEKBAR*/
    blutxt = settings.getInt("blutxt", 255); /*UNA VARIABILE DEL GRUPPO*/
    seekblu_txt.setText(blutxt + getString(R.string.blu));
    seekblu.setProgress(blutxt);
    // CAMBIAMENTO DEL VALORE DELLA SEEKBAR
    seekblu.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        //spostamento
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
            seekblu_txt.setText(progress + getString(R.string.blu));
            esempio.setTextColor(Color.parseColor("#" + Integer.toHexString(redtxt) + Integer.toHexString(greentxt) + Integer.toHexString(progress)));
        }

        //inizio dello spostamento
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        //fine dello spostamento
        public void onStopTrackingTouch(SeekBar seekBar) {
            blutxt = seekBar.getProgress();             
        }
    });
}

}

谢谢。

4

2 回答 2

0

那是因为您尝试分配 RGB 格式而不是十六进制,请尝试以下代码:

  Color mColor = new Color();
  mColor.red(redvalue);
  mColor.green(greenvalue);
  mColor.blue(bluevalue);
  esempio.setTextColor(mColor);
于 2013-03-30T11:34:28.537 回答
-1

可能是因为如果你往下走 16(不包括在内),颜色的十六进制代码可以用一位数表示。

因此,如果您尝试:

Integer.toHexString(15) --> 输出 = "F"。一位数 --> 错误

Integer.toHexString(16) --> 输出 = "10"。两位数 --> 很好

于 2013-03-30T11:35:57.193 回答