当复选框使用setEnabled(true)时更改.set文本颜色的颜色

时间:2015-04-06 09:18:33

标签: android checkbox

有些问题我自己无法解决。 我目前正在为我的培训制作一个简单的Android应用程序,它使用了多个复选框。

我想要做的是如标题所示,当条件为setEnabled(true)时,更改复选框文本的颜色。 我通过谷歌搜索过但无法找到解决方案。我已经添加了我在下面使用的示例代码。 Sonce,我是一个菜鸟,亲们的帮助对我来说意义重大。

public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    if (isChecked) {

                        buttonView.setTextColor(Color.GREEN);


                        buttonClicked.add(buttonView.getText()
                                .toString());
                        buttonView.setTextSize(18);
                        count += 1;
                        Log.d("TAG","is checked = "+isChecked);
                        if (count >= 7) {

                            for (int i = 0; i < checkbox.length; i++) {

                                String item = Integer.toString(i+1);
                                if (buttonClicked.indexOf(item)>-1) {
                                    checkbox[i].setEnabled(true);

                                    Log.d("TAG",i+" = 2is checked = "+isChecked);
                                //  buttonView.setTextColor(Color.YELLOW);
                                } else {

                                    Log.d("TAG",i+" = 3is checked = "+isChecked);
                                    checkbox[i].setEnabled(false);
                                //  buttonView.setTextColor(Color.RED);


                                }


                            }

                        }

                    } else {

                        if (count <= 7) {

                            for (int i = 0; i < checkbox.length; i++) {
                                checkbox[i].setEnabled(true);
                            //  buttonView.setTextColor(Color.BLACK);


                            }
                        }
                        count--;


                        buttonClicked.remove(buttonView.getText()
                                .toString());
                        buttonView.setTextSize(15);

                    }

3 个答案:

答案 0 :(得分:2)

创建选择器xml并将其放入 的 /res/color/text_my_checked.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#00ff00"/> 
<item android:color="#000000"/> <!-- anything else -->
</selector>

并在像这样的布局文件中使用它

    <CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Check"
    android:textColor="@color/text_my_checked"
    android:checked="true" />

答案 1 :(得分:0)

您可以按如下方式更改复选框的文字颜色,

yourCheckBox.setTextColor(Color.GREEN);

答案 2 :(得分:0)

这是一个选择:

checkboxbutton.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View v) {

            Checkbox b = (CheckBox)v;
            if (b.isChecked()) {
                  //your code
                  b.setTextColor(Color.GREEN);

            }
            else 
                 //your code
                 b.setTextColor(Color.BLACK);

          }
        });

此外,您可以选择color.xml文件中定义的颜色

b.setTextColor(getResources().getColor(R.color.blue));