2010-11-02 33 views
16

正如我的項目要求,我必須突出onClick的表格行。有什麼辦法可以做到這一點?或者請給我提供替代方案?如何突出點擊表格行?

+1

有點晚了,但檢查該線程的答案:http://stackoverflow.com/questions/6274343/how-to-change-the-background-color-of-a-tablerow-when-focused – Arnaud 2011-07-21 23:49:30

+0

你可以參考[本鏈接](http://stackoverflow.com/questions/4410420/onclick-change-tablerow-background-color)是指由Josh Clemm給出的答案。 – prateek 2012-04-07 10:02:32

+0

請參見此處的編輯答案:HTTP://stackoverflow.com/a/7022137/2469134 – 2017-03-12 20:02:04

回答

24

如果你想用股票上點擊亮點像你這樣一個通用的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(TextViews,Buttons等)。

+0

你顯然沒有測試過這一點。所有這些都設置了表格行的背景顏色,並向該行添加了點擊偵聽器,**沒有任何**。 – 2012-02-10 06:49:57

+6

我已經測試過了 - 它在生產中被成功地使用了。你是對的,它確定背景。它設置的是關鍵 - 「@android:drawable/list_selector_background」是單擊ListView中的條目時手機主題的標準高亮部分。 奇怪的是,在發表評論之前,你測試過它嗎? – 2012-02-10 18:10:00

+0

我測試過它,它工作。這正是我正在尋找的:TableRow的按鈕行爲。 – Lekensteyn 2012-04-05 19:17:26

9

內onclicklistener地址:

tr1.setBackgroundResource(drawable.list_selector_background); 

TR1哪裏是你的tablerow的。 (你需要使表格最後才能正常工作)。

+0

謝謝,但是當我按下該行時,它會突出顯示,然後再次回到未加亮的狀態! – 2017-03-12 19:52:58

11

連我都面臨着同樣的問題塞利勒潘迪特答案的幫助下做了一點改變它,這對我的作品

這是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> 
+0

設法遵循,但記住在可繪製文件夾下創建選擇器時,將selector.xml創建爲可繪製資源。 – 2015-07-08 02:21:03

0
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) + ""; 
       } 
       } 
     }); 
1
private OnClickListener tablerowOnClickListener = new OnClickListener() 
{ 
    public void onClick(View v) 
    { 
     //Highlight selected row 
     //Highlight selected row 
     for (int i = 1; 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)); 
      } 
     } 
     //... 
    } 
}; 
+0

其工作。行onclick監聽器的表格佈局並將此代碼放入其中。 – bhruguni 2017-06-02 09:19:30

+0

非常感謝,它解決了我的問題.....你今天救了我 – 2017-11-01 11:47:04