Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 来自holocolorpicker的setTextColor和getcolor_Android - Fatal编程技术网

Android 来自holocolorpicker的setTextColor和getcolor

Android 来自holocolorpicker的setTextColor和getcolor,android,Android,我正在使用holocolorpicker库。我想获取颜色并将其设置为textview,但colorpicker返回的颜色无效(返回的颜色与我的颜色不同)。例: 我在文本视图(masoud)上的文本与彩色圆圈的颜色不同 public class MainActivity extends ActionBarActivity implements OnColorChangedListener { TextView txt ; @Override protected voi

我正在使用holocolorpicker库。我想获取颜色并将其设置为textview,但colorpicker返回的颜色无效(返回的颜色与我的颜色不同)。例:

我在文本视图(masoud)上的文本与彩色圆圈的颜色不同

public class MainActivity extends ActionBarActivity implements OnColorChangedListener  {
    TextView txt ; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ColorPicker picker = (ColorPicker) findViewById(R.id.picker);
//      SVBar svBar = (SVBar) findViewById(R.id.svbar);
        OpacityBar opacityBar = (OpacityBar) findViewById(R.id.opacitybar);
        SaturationBar saturationBar = (SaturationBar) findViewById(R.id.saturationbar);
        ValueBar valueBar = (ValueBar) findViewById(R.id.valuebar);

//      picker.addSVBar(svBar);
        picker.addOpacityBar(opacityBar);
        picker.addSaturationBar(saturationBar);
        picker.addValueBar(valueBar);

        //To get the color
        picker.getColor();

        //To set the old selected color u can do it like this
        picker.setOldCenterColor(picker.getColor());
        // adds listener to the colorpicker which is implemented
        //in the activity
        picker.setOnColorChangedListener(this);

        //to turn of showing the old color
        picker.setShowOldCenterColor(false);

        //adding onChangeListeners to bars
//      opacitybar.setOnOpacityChangeListener(new OnOpacityChangeListener)
//      valuebar.setOnValueChangeListener(new OnValueChangeListener …)
//      saturationBar.setOnSaturationChangeListener(new OnSaturationChangeListener …)   

        txt = (TextView)findViewById(R.id.txt);
    }

    @Override
    public void onColorChanged(int color) {
        // TODO Auto-generated method stub

        final int c = color ;
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub

                txt.setTextColor(Color.rgb(c, c, c));
            }
        }, 2000);

    }

}
my layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.ex9_holocolorpicker.MainActivity" >

    <com.larswerkman.holocolorpicker.ColorPicker
        android:id="@+id/picker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>


    <com.larswerkman.holocolorpicker.OpacityBar
        android:id="@+id/opacitybar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <com.larswerkman.holocolorpicker.SaturationBar
        android:id="@+id/saturationbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <com.larswerkman.holocolorpicker.ValueBar
        android:id="@+id/valuebar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>  

    <TextView 
        android:id="@+id/txt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="masoud"
        />
</LinearLayout>

Color.rgb(c,c,c)
表示您将
c
设置为红色、绿色和蓝色的值

您可以使用
Color.rgb(Color.red(c)、Color.green(c)、Color.blue(c))例如:)

编辑:这里有更多

EDIT2:setTextColor(c)对:p执行此操作
(thx对Vikram)

您有例外吗?当你改变颜色时会发生什么?一个技巧可能是在设置侦听器之前定义TextView(使用
findViewById()
方法)。这可能不会导致问题,但这是一个很好的做法。@s.M_Emamian实际上,使用
txt.setTextColor(c)
也会产生同样的效果。你说得对@Vikram。我使用.rgb作为例子,因为他也使用了它:p