简体   繁体   English

按下按钮时更改Android微调器文本颜色

[英]Change Android spinner text color when button pressed

I have a series of ToggleButtons which represent a series of topics. 我有一系列ToggleButtons代表了一系列主题。 When toggled, the ToggleButton changes its background color to indicate its state. 切换时,ToggleButton会更改其背景颜色以指示其状态。 When in the checked state, the ToggleButton has a darker color. 处于checked状态时,ToggleButton颜色较深。

选定的州。在此输入图像描述

A Spinner overlays the ToggleButton and allows the user to select a difficulty for the topic. Spinner覆盖ToggleButton并允许用户选择主题的难度。

How do I change the text color (to white) of the Spinner when the ToggleButton is pressed? 按下ToggleButton时,如何更改Spinner的文本颜色(白色)? I think I can handle changing the spinner selector, but I'm struggling to work out a way to change the text color. 我想我可以处理更改微调器选择器,但我正在努力找到一种方法来改变文本颜色。

On Spinner onItemSelected Method you have to change like this: 在Spinner onItemSelected方法中你必须改变如下:

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



  }

Try the following way. 尝试以下方式。

  1. Create a xml named 创建一个名为的xml

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. 我希望这对你也有用。

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. 它适用于任何背景。

The answer from Gunaseelan helped point me in the right direction. Gunaseelan的答案帮助我指出了正确的方向。 As he suggested, working with the ToggleButton is the way to go. 正如他所建议的那样,使用ToggleButtonToggleButton的方法。 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 . ToggleButton控件有一个android:onClick ,您可以使用指定的方法时切换按钮被触发运行XML属性/点击(如描述在这里

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. 这仅更改选择微调器时显示的文本。

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

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