Change Android spinner text color when button pressed

房东的猫 提交于 2019-12-12 06:31:07

问题


I have a series of ToggleButtons which represent a series of topics. When toggled, the ToggleButton changes its background color to indicate its state. When in the checked state, the ToggleButton has a darker color.

A Spinner overlays the ToggleButton and allows the user to select a difficulty for the topic.

How do I change the text color (to white) of the Spinner when the ToggleButton is pressed? I think I can handle changing the spinner selector, but I'm struggling to work out a way to change the text color.


回答1:


Try the following way.

  1. Create a xml named

spinnertext.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/spinnerText"
    style="?android:attr/spinnerItemStyle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:paddingBottom="2dp"
    android:paddingLeft="6dp"
    android:textColor="#41f2f3" />

Now in code.

ArrayAdapter<String> sp_adapter = new ArrayAdapter<String>(this, R.layout.spinnertext, your_array);

sp.setAdapter(sp_adapter);

Then work with toggle button

    ToggleButton tb = (ToggleButton) findViewById(R.id.toggleButton1);
    tb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
            TextView tv = (TextView) findViewById(R.id.spinnerText);
            if (isChecked)
                tv.setTextColor(Color.RED);
            else
                tv.setTextColor(Color.BLUE);

        }
    });

Reference.

Try this way. This works for me. I hope this will work you too.




回答2:


On Spinner onItemSelected Method you have to change like this:

public void onItemSelected(AdapterView<?> parent, View arg1, int arg2,
    long arg3) {
// TODO Auto-generated method stub

item = (String) parent.getItemAtPosition(arg2);
((TextView) parent.getChildAt(0)).setTextColor(0x00000000);



  }



回答3:


One thing that we have on our app is a custom spinner view. It has a translucent black rounded square behind it and slightly larger than it. It works over any background.




回答4:


The answer from Gunaseelan helped point me in the right direction. As he suggested, working with the ToggleButton is the way to go. The ToggleButton widget has an android:onClick XML attribute which you can use to specify a method to run when the ToggleButton is toggled/clicked (as described here.

Setting the color of the spinner text and the spinner selector can be a bit difficult. To change the spinner text color:

ToggleButton cardiologyToggle = (ToggleButton) findViewById(R.id.cardiology_toggle);
        if (cardiologyToggle.isChecked()) {
            spinnerText.setTextColor(Color.WHITE);
        } else {
            spinnerText.setTextColor(Color.BLACK);
        }

This changes only the text displayed when the spinner has been selected.



来源:https://stackoverflow.com/questions/17417755/change-android-spinner-text-color-when-button-pressed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!