使用微调器动态设置textview的颜色?

时间:2014-09-15 06:12:29

标签: android xml

我想创建一个textview,用户可以通过从微调器对象中选择颜色来更改该文本的颜色。

微调器对象包含颜色列表,这是用户动态更改textview颜色的选项。

借口我的英语并帮助我......

3 个答案:

答案 0 :(得分:1)

请尝试这种方式,希望这有助于您解决问题。

<强> activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

   <TextView
       android:id="@+id/textview"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="TextView"/>

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"/>

</LinearLayout>

<强>的strings.xml

<array name="colorname">
    <item>Red</item>
    <item>Yellow</item>
    <item>Green</item>
    <item>Blue</item>
    <item>Pink</item>
</array>

<array name="colorcode">
    <item>#FF0000</item>
    <item>#ffff00</item>
    <item>#00ff00</item>
    <item>#0000ff</item>
    <item>#FF0080</item>
</array>

<强> MainActivity.java

public class MainActivity extends Activity {

    private Spinner spinner;
    private TextView textview;
    private ArrayList<String> colorNameList;
    private ArrayList<String> colorCodeList;

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

        textview = (TextView) findViewById(R.id.textview);
        spinner = (Spinner) findViewById(R.id.spinner);

        colorNameList = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.colorname)));
        colorCodeList = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.colorcode)));

        final ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, colorNameList);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(dataAdapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                textview.setTextColor(Color.parseColor(colorCodeList.get(position)));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }
}

答案 1 :(得分:0)

YourSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
        {
           YourTextView.setTextColor(getResources().getColor(R.color.some_color));
            // your code here

        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
        }

    });

如果您尝试在Stack Site上搜索,那就太好了。

答案 2 :(得分:0)

public class MainActivity extends ActionBarActivity {

private TextView text;
private Spinner spin;
ArrayList al;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
text=(TextView)findViewById(R.id.textView1);
spin=(Spinner)findViewById(R.id.spinner1);
al=new ArrayList();
al.add("RED");
al.add("BLUE");
ArrayAdapter adapter=new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_activated_1,al);
spin.setAdapter(adapter);
spin.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub
        switch(arg2)
        {
        case 0:
            text.setText("srikanth");
            text.setTextColor(Color.RED);
            break;
        case 1:
            text.setText("SRIkANTH BLUE");
            text.setTextColor(Color.BLUE);
            break;

        }

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
});


}

}

我希望它可以帮助您