OnClick 更改表格行背景颜色
因此,我试图找到一种简单的方法来在单击时更改背景颜色或表格行。我一直在尝试找到一种方法来调用背景颜色并检查它,但我还没有找到一种方法来调用颜色。这是我现在所拥有的。
RowName = (TableRow) findViewById(R.id.RowName);
RowName.setBackgroundColor(Color.TRANSPARENT);
RowName.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (RowName.equals(Color.TRANSPARENT))
RowName.setBackgroundColor(Color.YELLOW);
else if (RowName.equals(Color.YELLOW))
RowName.setBackgroundColor(Color.TRANSPARENT);
}
});
我知道这是错误的。希望你能看到我正在努力实现的目标。如果没有,我想做的就是让表格行开始透明。当有人单击表格行时,我希望它变为黄色。然后,如果他们再次单击它,我希望它变回透明。谢谢。
So I'm trying to find an easy way to get the background color or a table row to change when its clicked on. I've been trying to find a way to call what the background color is and check it but I haven't found a way to call the color. Here is what I have right now.
RowName = (TableRow) findViewById(R.id.RowName);
RowName.setBackgroundColor(Color.TRANSPARENT);
RowName.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (RowName.equals(Color.TRANSPARENT))
RowName.setBackgroundColor(Color.YELLOW);
else if (RowName.equals(Color.YELLOW))
RowName.setBackgroundColor(Color.TRANSPARENT);
}
});
I know that its wrong. Hopefully you can see what I'm trying to accomplish. If not, what I want to do is have the table row start of transparent. When someone clicks on the table row I want it to change to yellow. Then, if they click it again, I want it to change back to transparent. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将行的背景颜色设置为可绘制的状态列表(处理选择、按下、活动、非活动)。
http://developer.android.com/guide/topics/resources /drawable-resource.html#StateList
XML 应如下所示:
You need to set the background color of your row to a state list drawable (that handles select, pressed, active, non-active).
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
The XML should look something like this:
这就是最终的工作结果。确保您已命名 TableRows。在我创建之前我有
我也有
。然后我添加代码
要使其正常工作,请进入 xml 并在 OnClick 属性中添加 RowName 或您正在使用的 public void 的名称。
享受。
So here is what wound up working. Make sure you have your TableRows named. Before my on create I have
I also have
. I then add the code
To get it to work, go into your xml and in the OnClick property add RowName or the name of the public void that you are working with.
Enjoy.