【问题标题】:Android : Change background color of table rows when it is clickedAndroid:单击时更改表格行的背景颜色
【发布时间】:2013-11-28 12:14:31
【问题描述】:

在我的 android 应用程序中有一个在运行时加载的表格布局。我为表格行设置了一个 OnClickListener。

tr.setClickable(true);
tr.setOnClickListener(trOnClickListener);

单击时我需要更改表格行的背景颜色。这是我的 onClick 方法。

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

        TableRow tablerow = (TableRow)v;

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

    }
};

当我单击表格行时,它会更改背景颜色。当我单击另一行时,它也会更改其背景颜色。但我想要的是, 一次只能更改一行的背景颜色。

我该怎么做?任何建议表示赞赏。

提前致谢

【问题讨论】:

    标签: java android tablelayout


    【解决方案1】:

    在drawable文件夹中创建XML并将其设置为bg

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

     <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
    
        <item android:drawable="@drawable/cell_shape_in_white" android:state_focused="true" android:state_pressed="false"/>
        <item android:drawable="@drawable/cell_shape_in_white" android:state_focused="true"/>
        <item android:drawable="@drawable/cell_shape_in_grey" android:state_focused="false"/>
        <item android:drawable="@drawable/cell_shape_in_grey"/>
    
    </selector>
    

    参考链接How to change the background color of a TableRow when focused?

    【讨论】:

    • 我用过这个。然后,当我单击一行时,它会将其背景颜色更改为黄色(例如)。但在一秒钟内它会变回以前的颜色(白色)。我需要保持这种颜色(黄色),直到用户单击另一行。当用户单击另一行时,其颜色应更改为黄色,之前单击的行的颜色应为白色。实际上,只有一排可以是黄色的。我怎样才能做到这一点?你能解释一下吗?
    • 你在动态创建tablerow吗
    • 是的。我的表格布局正在动态加载。
    • 它根据数据库中的行数而变化。
    • 我试过了。然后所有行都是灰色的,点击时颜色不会改变。/
    【解决方案2】:

    此非XML FIX使先前选择的行在选择附加行并突出显示(黄色)时恢复到中性表背景颜色(LT.灰色)。这是通过引用选定(子)行的适当索引号来实现的。

    public class dataTable extends Fragment {
    TextView tvSelectedRow = null;
    ...
    row.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View v){
                        if(tvSelectedRow == null) {
                            iRowNumber = tableLayout.indexOfChild(v); //This returns the rowID of the selected row
                            tableLayout.setBackgroundResource(R.color.LtGrey); //sets background color to LtGray for whole table.
                            row.setBackgroundResource(R.color.yellow);//changes background color of selected row to yellow.                           
                        }else if(tvSelectedRow != null){
                            tableLayout.getChildAt(iRowNumber).setBackgroundResource(R.color.LtGrey);//causes previously selected row to revert to LtGray.
                            iRowNumber = tableLayout.indexOfChild(v); //Resets iRowNumber to new row selected by user....                      
                            row.setBackgroundResource(R.color.yellow);//changes background color of selected row to yellow.                           
                        }
                    }
                });
    

    祝你好运!

    【讨论】:

      【解决方案3】:

      试试这个

      private View pressedView = null; 
      
      private OnItemClickListener getStartedListItem = new OnItemClickListener() {
      
      @Override
      public void onItemClick(AdapterView<?> arg0, View arg1, int position,
              long arg3) {
          // TODO Auto-generated method stub
           Intent myintent = new  Intent(getApplicationContext(),GetStartedWebview.class);
           myintent.putExtra("SelectedItem", getStartedItems[position]);
           startActivity(myintent);
      
           if(pressedView != null) {
               pressedView.setBackgroundResource(..); // reset background of old item
               pressedView = arg1; // Point pressedView to new item
           }
      }
      };
      

      【讨论】:

        猜你喜欢
        • 2013-09-27
        • 1970-01-01
        • 1970-01-01
        • 2018-10-14
        • 2019-09-01
        • 2014-06-12
        • 2011-10-20
        • 2016-03-23
        • 2011-06-05
        相关资源
        最近更新 更多