简体   繁体   English

选中 CheckBox 时更改 ListView 行中的文本颜色

[英]When CheckBox is checked change the text color in ListView row

I have a TO DO List and when the CheckBox is ticked.我有一个待办事项列表,当勾选复选框时。 I want the color in the ListView row where the CheckBox is to change.我想要更改 CheckBox 的 ListView 行中的颜色。

This is the code I have done but it does not work.这是我完成的代码,但它不起作用。

public class CheckBoxCheck extends Activity{

CheckBox check;

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

   check = (CheckBox) findViewById(R.id.checkBox1);
   check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked){
            check.setBackgroundColor(Color.BLUE);
        }else{
            check.setBackgroundColor(Color.BLACK);
        }

    }
});

}
 }

Could You Please Help.能否请你帮忙。

You should try this:你应该试试这个:

checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(checkBox.isChecked()) {
                    listView.setBackgroundColor(Color.parseColor("#008000"));
                } else {
                    listView.setBackgroundColor(Color.parseColor("#FF0000"));
                }
            }
        });

This should be pretty easy.这应该很容易。 The checkbox can have a text associated.复选框可以关联文本。 So simply put the text in the checkbox and give the text a color which is a .因此,只需将文本放在复选框中并为文本指定颜色,即 . In the selector xml you can easily specify the colors for the normal and the checked state.在选择器 xml 中,您可以轻松指定正常和选中状态的颜色。

I have a TO DO List and when the CheckBox is ticked.我有一个待办事项列表,当勾选复选框时。 I want the color in the ListView row where the CheckBox is to change.我想要更改 CheckBox 的 ListView 行中的颜色。

The code you posted does not show any ListView.您发布的代码没有显示任何 ListView。 So that would be the first problem.所以这将是第一个问题。

If you're trying to change the row color of a listview, you need to implement getView() of the adapter that feeds the ListView and update the view that is returned appropriately.如果您尝试更改列表视图的行颜色,则需要实现提供给 ListView 的适配器的 getView() 并更新相应返回的视图。

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

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