【问题标题】:Android: Change table row color when it clicks and remove the color when click anothe row of the tableAndroid:单击时更改表格行颜色并在单击表格的另一行时删除颜色
【发布时间】:2017-02-16 19:29:54
【问题描述】:

在我的 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 资源文件,请显示您的可绘制对象的代码。
  • @Neil 我已经编辑了我的问题。请帮我解决这个问题

标签: java android tablelayout


【解决方案1】:

改变你的drawable看起来像这样

<?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 之一中使用它并且它有效。

希望对你有帮助

【讨论】:

  • 如何识别表格行。我用过 tr.setTag()。我可以用它来识别之前选择的行和新选择的行吗?
  • 您必须将所选视图保存在某种全局变量或类似的东西中。您在 onClick(View v) 中获得了视图,因此可以使用该视图。像 v.setSelected(true) 和 currentSelected.setSelected(false) 然后 currentSelected = v;
  • 是的,它是一个你必须设置的全局变量。只需在第一次将其设置为 false 时检查它是否为 null
  • 我已将其初始化为空。可以吗?私有视图 currentSelected = null;
  • 是的,只要您在 onClick 中进行空值检查
【解决方案2】:

如果你从 xml 使用选择器,并应用为背景或源..

【讨论】:

    【解决方案3】:

    创建一个字段:查看restoredView

    row.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (restoredView != null) {
                restoredView.setBackgroundColor(Color.parseColor("#EEEEEE"));
            }
            view.setBackgroundColor(Color.parseColor("#DDDDDD"));
            restoredView = view;
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多