英文:
How to create a button on click the background color changes
问题
我尝试使用按钮和点击事件来更改背景颜色。此外,我不知道如何在点击时添加复选标志,以及如何在点击下一个按钮时将其恢复为默认状态。
英文:
enter image description here
Check image
I tried using button and on click event to change the background color. Also I don't know how to add the check icon on click and also how to return it to default state clicking the next button
答案1
得分: 2
将索引初始设置为-1,并且当你点击该项时,它会改变特定项的颜色
index = -1;
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
index = position;
notifyDataSetChanged();
}
});
if (index == position) {
holder.view.setBackgroundColor(Color.parseColor("#567845"));
holder.tv1.setTextColor(Color.parseColor("#ffffff"));
} else {
holder.view.setBackgroundColor(Color.parseColor("#ffffff"));
holder.tv1.setTextColor(Color.parseColor("#000000"));
}
}
英文:
Set index to -1 initially and when you click the item it will change color of particular item
index=-1;
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
index=position;
notifyDataSetChanged();
}
});
if(index==position){
holder.view.setBackgroundColor(Color.parseColor("#567845"));
holder.tv1.setTextColor(Color.parseColor("#ffffff"));
}
else
{
holder.view.setBackgroundColor(Color.parseColor("#ffffff"));
holder.tv1.setTextColor(Color.parseColor("#000000"));
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论