繁体   English   中英

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

[英]How could I change background color of a button from a 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>

您可以尝试在“ onItemSelected”方法内部使用一个开关,如下所示:

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));
            }
        });

获取所选项目的价值,并根据价值可以设置按钮的颜色。检查此链接

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

    <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));
            }

        });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM