OnClick 更改表格行背景颜色

发布于 2024-10-07 04:43:39 字数 654 浏览 2 评论 0原文

因此,我试图找到一种简单的方法来在单击时更改背景颜色或表格行。我一直在尝试找到一种方法来调用背景颜色并检查它,但我还没有找到一种方法来调用颜色。这是我现在所拥有的。

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

翻了热茶 2024-10-14 04:43:39

您需要将行的背景颜色设置为可绘制的状态列表(处理选择、按下、活动、非活动)。

http://developer.android.com/guide/topics/resources /drawable-resource.html#StateList

XML 应如下所示:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--  Active state -->
    <item android:state_selected="true" android:state_focused="false"
        android:state_pressed="false" android:drawable="@android:color/transparent" />
    <!--  Inactive state-->
    <item android:state_selected="false" android:state_focused="false"
        android:state_pressed="false" android:drawable="@android:color/transparent" />
    <!--  Pressed state-->
    <item android:state_pressed="true" android:drawable="@android:color/yellow" />
    <!--  Selected state (using d-pad) -->
    <item android:state_focused="true" android:state_selected="true"
        android:state_pressed="false" android:drawable="@android:color/yellow" />
</selector>

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:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--  Active state -->
    <item android:state_selected="true" android:state_focused="false"
        android:state_pressed="false" android:drawable="@android:color/transparent" />
    <!--  Inactive state-->
    <item android:state_selected="false" android:state_focused="false"
        android:state_pressed="false" android:drawable="@android:color/transparent" />
    <!--  Pressed state-->
    <item android:state_pressed="true" android:drawable="@android:color/yellow" />
    <!--  Selected state (using d-pad) -->
    <item android:state_focused="true" android:state_selected="true"
        android:state_pressed="false" android:drawable="@android:color/yellow" />
</selector>
染柒℉ 2024-10-14 04:43:39

这就是最终的工作结果。确保您已命名 TableRows。在我创建之前我有

私有TableRow行名称;

我也有

int 状态 = 0;

。然后我添加代码

public void RowName(View view) {
  开关(状态){
  案例0:
      RowName.setBackgroundColor(Color.YELLOW);
      状态=1;
      休息;
  案例1:
      RowName.setBackgroundColor(Color.TRANSPARENT);
      状态=0;
      休息;
  }
}

要使其正常工作,请进入 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

private TableRow RowName;

I also have

int state = 0;

. I then add the code

public void RowName(View view) {
  switch (state) {
  case 0:
      RowName.setBackgroundColor(Color.YELLOW);
      state = 1;
      break;
  case 1:
      RowName.setBackgroundColor(Color.TRANSPARENT);
      state = 0;
      break;
  }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文