如何在单击时突出显示表格行?

发布于 2024-09-30 09:32:19 字数 62 浏览 5 评论 0原文

根据我的项目要求,我必须突出显示 onClick 上的表行。有什么办法可以做到这一点吗?或者请建议我替代方案?

As my project requirement i have to highlight the table row on onClick. There is any way to do this? Or please suggest me the alternative?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

不弃不离 2024-10-07 09:32:20

如果您想像使用通用 ListView 一样使用单击突出显示的库存,则需要将每行的背景设置为 android:background="@android:drawable/list_selector_background"

这里是一个例子:

<TableLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:stretchColumns="0">
  <TableRow
     android:id="@+id/first_row"
     android:background="@android:drawable/list_selector_background" >
    ... row content ...
  </TableRow>
</TableLayout>

然后在代码中,

TableRow firstRow = (TableRow) findViewById(R.id.first_row);
firstRow.setOnClickListener(new OnClickListener() {
       @Override
        public void onClick(View v) {
            // TODO: do your logic here

        }   
}

你应该得到一个可突出显示的行,就像在 ListView 中一样...

编辑:
上面将为您提供默认主题的列表背景选择器。如果您想要更通用的选择器(例如用户触摸行时的材料设计选择器),请使用此:

android:background="?android:attr/selectableItemBackground"

此外,这不仅适用于 TableRows。您应该能够在几乎任何带有 onClickListener 的通用小部件(文本视图、按钮等)上执行此操作。

If you want to use the stock on click highlight like you get with a generic ListView, you want to set the background of each row to be android:background="@android:drawable/list_selector_background"

Here is an example:

<TableLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:stretchColumns="0">
  <TableRow
     android:id="@+id/first_row"
     android:background="@android:drawable/list_selector_background" >
    ... row content ...
  </TableRow>
</TableLayout>

Then in code,

TableRow firstRow = (TableRow) findViewById(R.id.first_row);
firstRow.setOnClickListener(new OnClickListener() {
       @Override
        public void onClick(View v) {
            // TODO: do your logic here

        }   
}

And you should get a highlight-able row just like in a ListView...

EDIT:
Above will give you the default theme's list background selector. If you want the more generic selector (like the material design selector when the user touches a row) use this:

android:background="?android:attr/selectableItemBackground"

Also this applies to more than just TableRows. You should be able to do this on almost any generic widget with an onClickListener attached (TextViews, Buttons, etc).

眼趣 2024-10-07 09:32:20

即使我在 salil pandit 答案的帮助下也面临着同样的问题,但对其做了一些更改,这对我有用

这是 xml 中的 TableRow

<TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:padding="5dip" 
        android:background="@drawable/selector">

这是 selector.xmlres\drawable 文件夹中

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


     <item android:drawable="@android:drawable/list_selector_background"></item>

</selector>

Even I was facing the same problem with the help of salil pandit answer made a little change to it and that works for me

This is TableRow in xml:

<TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:padding="5dip" 
        android:background="@drawable/selector">

This is selector.xml in res\drawable folder

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


     <item android:drawable="@android:drawable/list_selector_background"></item>

</selector>
嗳卜坏 2024-10-07 09:32:20

在 onclicklistener 中添加:

 tr1.setBackgroundResource(drawable.list_selector_background);

其中 tr1 是您的表格行。 (您需要将表格行最终确定才能工作)。

Within the onclicklistener add:

 tr1.setBackgroundResource(drawable.list_selector_background);

Where tr1 is your tablerow. (you will need to make the tablerow final for it to work).

坏尐絯℡ 2024-10-07 09:32:20
private OnClickListener tablerowOnClickListener = new OnClickListener()
{
    public void onClick(View v)
    {
        //Highlight selected row
        //Highlight selected row
        //Start from 0 to make sure that the first item will also be looped 
        //through
        for (int i = 0; i < tblItemDetail.getChildCount(); i++)
        {
            View row = tblItemDetail.getChildAt(i); 
            if (row == v)
            {
                row.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));               
            }
            else
            {
                //Change this to your normal background color.
                row.setBackgroundColor(getResources().getColor(android.R.color.transparent));
            }
        }
        //...
    }
};
private OnClickListener tablerowOnClickListener = new OnClickListener()
{
    public void onClick(View v)
    {
        //Highlight selected row
        //Highlight selected row
        //Start from 0 to make sure that the first item will also be looped 
        //through
        for (int i = 0; i < tblItemDetail.getChildCount(); i++)
        {
            View row = tblItemDetail.getChildAt(i); 
            if (row == v)
            {
                row.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));               
            }
            else
            {
                //Change this to your normal background color.
                row.setBackgroundColor(getResources().getColor(android.R.color.transparent));
            }
        }
        //...
    }
};
誰認得朕 2024-10-07 09:32:20
String _row_selected = null;
boolean _is_selection_even = false;
private TableLayout TL;
TableRow row_data = new TableRow(this);

row_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (_row_selected != null) {

                    if (Integer.parseInt(_row_selected) == TL.indexOfChild(v)) {

                        if (_is_selection_even) {
                            TL.getChildAt(Integer.parseInt(_row_selected)).setBackgroundColor(0xFF00FF00);
                            _is_selection_even = false;
                        } else {
                            TL.getChildAt(Integer.parseInt(_row_selected)).setBackgroundColor(Color.WHITE);
                            _is_selection_even = true;
                        }


                    } else {
                        TL.getChildAt(Integer.parseInt(_row_selected)).setBackgroundColor(Color.WHITE);
                        v.setBackgroundColor(0xFF00FF00);
                        _row_selected = null;
                        _row_selected = TL.indexOfChild(v) + "";
                    }

                } else {
                    v.setBackgroundColor(0xFF00FF00);
                    _row_selected = null;
                    _row_selected = summaryTL.indexOfChild(v) + "";
                }
              }
        });
String _row_selected = null;
boolean _is_selection_even = false;
private TableLayout TL;
TableRow row_data = new TableRow(this);

row_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (_row_selected != null) {

                    if (Integer.parseInt(_row_selected) == TL.indexOfChild(v)) {

                        if (_is_selection_even) {
                            TL.getChildAt(Integer.parseInt(_row_selected)).setBackgroundColor(0xFF00FF00);
                            _is_selection_even = false;
                        } else {
                            TL.getChildAt(Integer.parseInt(_row_selected)).setBackgroundColor(Color.WHITE);
                            _is_selection_even = true;
                        }


                    } else {
                        TL.getChildAt(Integer.parseInt(_row_selected)).setBackgroundColor(Color.WHITE);
                        v.setBackgroundColor(0xFF00FF00);
                        _row_selected = null;
                        _row_selected = TL.indexOfChild(v) + "";
                    }

                } else {
                    v.setBackgroundColor(0xFF00FF00);
                    _row_selected = null;
                    _row_selected = summaryTL.indexOfChild(v) + "";
                }
              }
        });
若言繁花未落 2024-10-07 09:32:20

@SalilPandit 的带有行选择器的编程版本:

final TableRow row = new TableRow(this);

row.setBackgroundResource(android.R.drawable.list_selector_background);

row.setOnClickListener(new View.OnClickListener(){            

     @Override
     public void onClick(View view){
         row.setFocusable(true);
         // TODO
     }
});

对于通用选择器(材料设计):

TypedValue typeValue = new TypedValue();
this.getTheme().resolveAttribute(android.R.attr.selectableItemBackground,
                                 typeValue, true);

final TableRow row = new TableRow(this);
row.setBackgroundResource(typeValue.resourceId);

row.setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(View view){
          row.setFocusable(true);
          // TODO
    }
});

@SalilPandit 's programmitical version with row selector:

final TableRow row = new TableRow(this);

row.setBackgroundResource(android.R.drawable.list_selector_background);

row.setOnClickListener(new View.OnClickListener(){            

     @Override
     public void onClick(View view){
         row.setFocusable(true);
         // TODO
     }
});

For generic selector ( material design ):

TypedValue typeValue = new TypedValue();
this.getTheme().resolveAttribute(android.R.attr.selectableItemBackground,
                                 typeValue, true);

final TableRow row = new TableRow(this);
row.setBackgroundResource(typeValue.resourceId);

row.setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(View view){
          row.setFocusable(true);
          // TODO
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文