Android:有关 TableRow 选择的查询
在我的 XML 中,我有一个只有 1 个 TableRow(即标题)的 TableLayout。我添加的其他所有行动态设置 TableRow 和 TableRow 的背景颜色(LTGray)其中TextColor为TextViews。我还处理每一行的点击事件。
private void createView(TableRow tr, TextView tv, String data, int rowId) {
tv.setText(data);
//tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setTextColor(Color.BLACK);
tv.setPadding(20, 0, 0, 0);
tr.setPadding(0, 1, 0, 1);
tr.setBackgroundColor(Color.LTGRAY);
tr.setId(rowId);
tr.setClickable(true);
tr.setOnClickListener(this);
tr.addView(tv);
}
注册选择: 我想改变 TableRow 的背景颜色,比如说黄色。因此,如果选择第一行,则 bgColor 应为黄色。然后,如果选择第 3 行,则第 1 行的颜色应变为 LTGray。
如果单击行之外的任何位置,则也应取消选择所选行(如果有的话)。为此,我是否必须添加主布局 clickListener 或再次选择该行并将其取消选择?
选择器(状态列表可绘制)可以同时使用这两种方式吗?或者我必须以编程方式处理它。我应该使用哪种 Drawable 在 Java 代码中 setBackgroundDrawable 来设置状态列表可绘制对象?
我相信像 TableRow 的其他组件一样,onClick 也会处理 onTouch。如果有错请指正。因为想要通过触摸行来处理相同的功能。
实现目标的最佳方法是什么?非常感谢任何帮助。
In my XML, I have a TableLayout with only 1 TableRow i.e. the heading. Other all rows I add dynamically setting BackgroundColor (LTGray) for TableRow & TextColor for TextViews in it . I also handle click event on each row.
private void createView(TableRow tr, TextView tv, String data, int rowId) {
tv.setText(data);
//tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setTextColor(Color.BLACK);
tv.setPadding(20, 0, 0, 0);
tr.setPadding(0, 1, 0, 1);
tr.setBackgroundColor(Color.LTGRAY);
tr.setId(rowId);
tr.setClickable(true);
tr.setOnClickListener(this);
tr.addView(tv);
}
Reg selection :
I want to change the BackgroundColor of TableRow lets say Yellow. So if 1st row is selected it bgColor should be Yellow. Then if 3rd row is selected the 1st row's color should turn to LTGray.
And if anywhere out of the Rows is clicked, then the selected Row (if at all) should also be de-selected. For this do I have to add the main layout clickListener OR make the row select again and it turns deselected ?
Can selector (state list drawable) work for both the ways or I got to handle it programmatically. What sort of Drawable should I use to setBackgroundDrawable in my Java Code to se the statelist drawable ?
I believe like other components for TableRow also onClick will also take care of onTouch. Please correct me if am wrong. As want to handle the same feature with touching the row also.
What is the best way to achieve the goal ? Any help is highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要在代码中更改它!请改用选择器。
摘自这里:
不是那么彻底,但更全面有用的答案是此处
动态更改选择器此处
Do not change that in code! Use selector instead.
Taken from here:
Not so thorough, but more useful answer is here
changing the selector dynamically here
好的,然后使用
OnFocusChangeListener
。它捕获获得和失去焦点。当视图的焦点状态发生更改时调用。
Ok, than use
OnFocusChangeListener
. It catches obtaining and losing the focus.Called when the focus state of a view has changed.
谢谢朋友,
我在代码本身中成功处理了它。向每行添加点击侦听器,并相应地添加和处理选定行和未选定行的颜色。
Thanks Friends,
I managed it handling in the code itself. Added click listener to each row addd and handle the colors of selected and non-selected row accordingly.