从程序中的RGB值更改背景颜色

时间:2014-09-30 04:19:02

标签: java android numberpicker

我正在使用我已定义并正在工作的三个数字选择器,从0到255,代表R G和B值。我无法让它改变背景颜色。以下是我到目前为止的情况:

package com.example.colorpicker;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnValueChangeListener;
import android.widget.TextView;
import android.graphics.Color;

public class MainActivity extends ActionBarActivity 
{
  NumberPicker NPR, NPG, NPB;
  TextView textRed, textGreen, textBlue;
  int BGColor; 


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

    NPR = (NumberPicker) findViewById(R.id.numberPicker1);
    NPR.setMinValue(0);  NPR.setMaxValue(255);
    NPR.setOnValueChangedListener(null);

    NPB = (NumberPicker) findViewById(R.id.numberPicker2);
    NPB.setMinValue(0);  NPB.setMaxValue(255);
    NPB.setOnValueChangedListener(null);

    NPG = (NumberPicker) findViewById(R.id.numberPicker3);
    NPG.setMinValue(0);  NPG.setMaxValue(255);
    NPG.setOnValueChangedListener(null);

    BGColor = Color.rgb(NPR.getValue(), NPB.getValue(), NPG.getValue());
    //.setBackgroundColor(BGColor);




    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

我感觉我使用的是onValueChangeListener错误。我之前评论过的那行是

v.setBackgroundColor(BGColor)

View.v;

主要定义。然而,这只会导致我的应用程序在启动时立即崩溃。我也觉得我的布局缺少一些东西可以使背景颜色初始化?如果有人愿意帮助我开展这项工作,并向我解释,我将非常感激。提前谢谢。

0 个答案:

没有答案
相关问题