java - Android:单击时更改表格行颜色,单击表格的另一行时删除颜色

标签 java android tablelayout

在我的 Android 应用程序中,有一个在运行时加载的表格布局。我已经实现了一个代码,用于在单击该行时更改该表的背景颜色。

private OnClickListener trOnClickListener = new OnClickListener() {
    public void onClick(View v) {
        TableRow tablerow = (TableRow)v;

        tablerow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.table_row_selector));


    }
};

现在我想在用户单击表格的另一行时删除此颜色,并且新单击的行应该更改其颜色。

这是我的绘图。

<?xml version="1.0" encoding="utf-8"?>

<item android:drawable="@drawable/ab_stacked_solid_whiteaction" android:state_pressed="true"/>
<item android:drawable="@drawable/table_shape" android:state_enabled="true"/>

任何建议都将受到高度赞赏。

提前致谢

最佳答案

将您的绘图更改为如下所示

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/background_selected" android:state_enabled="true"
          android:state_pressed="true"/>
    <item android:drawable="@drawable/background_selected" android:state_enabled="true"
          android:state_focused="true"/>
    <item android:drawable="@drawable/background_selected" android:state_enabled="true"
          android:state_selected="true"/>
    <item android:drawable="@drawable/background_selected" android:state_active="true"
          android:state_enabled="true"/>

    <item android:drawable="@drawable/background_selectable" android:state_pressed="false"/>
    <item android:drawable="@drawable/background_selectable" android:state_focused="false"/>
    <item android:drawable="@drawable/background_selectable" android:state_selected="false"/>
    <item android:drawable="@drawable/background_selectable" android:state_active="false"/>
</selector>

然后将行设置为tablerow.setSelected(true)。当选择另一行时,将先前选择的行设置为 tablerow.setSelected(false),将新选择的行设置为 true

我在我的一个 ListViews 中使用它并且它有效。

希望这有帮助

关于java - Android:单击时更改表格行颜色,单击表格的另一行时删除颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19998458/

相关文章:

java - 如何在 linux 和 unix 中将 "kill -3 <jvm pid>"标准输出单独重定向到一个文件?

android - Android 2即时运行

android - 如何将付费安卓应用从一个谷歌账户转移到另一个谷歌账户

java - Tomcat认证后如何更改Jsessionid?

java - 将 String 设置为不为 null 的值

android - 在不阻塞 UI 线程的情况下显示大表

Matlab - 绘图窗口排列

android - 将多个图像合并为一个图像

java - 如何减少这段代码的冗余?

android - 无法分配局部变量,因为它是在外部定义的