如何从微调器中更改按钮的背景颜色?

时间:2016-10-15 13:37:13

标签: android colors spinner

我必须创建一个包含一些颜色的微调器,当您选择其中一个颜色时,按钮的背景颜色会发生变化。 这就是我试过的:

    <resources>
        <string name="app_name">MyFirst</string>


            <string-array name="colors_array">
                <item>red</item>
                <item>green</item>
                <item>blue</item>
                <item>pink</item>
            </string-array>
    </resources>
 ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.colors_array, android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                bClick.setBackgroundColor( getResources().getColor(R.color.red));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                bClick.setBackgroundColor( getResources().getColor(R.color.blue));
            }

        });

现在它使我的按钮变红但我不知道如何将其更改为所选颜色,我如何通过名称(作为字符串)获取颜色值。这里是我的colors.xml

<resources>

    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="red">#FF0000</color>
    <color name="green">#00FF00</color>
    <color name="blue">#0000FF</color>
    <color name="pink">#FF4081</color>
</resources>

3 个答案:

答案 0 :(得分:1)

您可以尝试使用内部开关&#34; onItemSelected&#34;方法如下:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                switch (position) {
                    case 0:
                        bClick.setBackgroundColor(getResources().getColor(R.color.red));
                        break;
                    case 1:
                        bClick.setBackgroundColor(getResources().getColor(R.color.green));
                        break;
                    case 2:
                        bClick.setBackgroundColor(getResources().getColor(R.color.blue));
                        break;
                    case 3:
                        bClick.setBackgroundColor(getResources().getColor(R.color.pink));
                        break;
                    default:
                        bClick.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                bClick.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
            }
        });

答案 1 :(得分:0)

获取所选项目的值,并根据值设置按钮的颜色。检查此link

答案 2 :(得分:0)

我通过使用另一个数组解决了这个问题。

    <string-array name="colors_array">
        <item>red</item>
        <item>green</item>
        <item>blue</item>
        <item>pink</item>
    </string-array>
<string-array name="colors1_array">
    <item>@color/red</item>
    <item>@color/green</item>
    <item>@color/blue</item>
    <item>@color/pink</item>
</string-array>

并像这样使用它:

 Resources res = getResources();
        final int[] rainbow = this.getResources().getIntArray(R.array.colors1_array);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                int i = spinner.getSelectedItemPosition();
                bClick.setBackgroundColor(rainbow[i]);

            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                bClick.setBackgroundColor( getResources().getColor(R.color.blue));
            }

        });