Java Android:单击表格行时更改其背景色

Java Android:单击表格行时更改其背景色,java,android,tablelayout,Java,Android,Tablelayout,在我的android应用程序中,有一个在运行时加载的表布局。我为表行设置了OnClickListener tr.setClickable(true); tr.setOnClickListener(trOnClickListener); 我需要在单击表格行时更改其背景色。这是我的onClick方法 private OnClickListener trOnClickListener = new OnClickListener() { public void onClick(View v)

在我的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));

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

我该怎么做?如有任何建议,我们将不胜感激

提前感谢

试试这个

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
     }
}
};
private View按View=null;
私有OnItemClickListener getStartedListItem=新建OnItemClickListener(){
@凌驾
公共单击(适配器视图arg0,视图arg1,内部位置,
长arg3){
//TODO自动生成的方法存根
Intent myintent=newintent(getApplicationContext(),GetStartedWebview.class);
myintent.putExtra(“SelectedItem”,getStartedItems[position]);
星触觉(myintent);
如果(按View!=空){
按EdView.setBackgroundResource(..);//重置旧项目的背景
pressedView=arg1;//将pressedView指向新项目
}
}
};

在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>



请参阅链接

当选择并突出显示其他行(黄色)时,此非xml修复程序会使以前选择的行恢复为中性表格背景色(浅灰色)。这是通过引用所选(子)行的适当索引号来实现的


祝你好运

我用过这个。然后,当我单击一行时,它会将其背景颜色更改为黄色(例如)。但在一秒钟内,它会变回原来的颜色(白色)。我需要保留此颜色(黄色),直到用户单击另一行。当用户单击另一行时,其颜色应更改为黄色,先前单击的行的颜色应为白色。实际上,只有一行可以是黄色的。我该怎么做?你能解释一下吗?你正在动态创建tablerow。我的表布局正在动态加载。它根据数据库中的行数而变化。我已经尝试过了。然后所有的行都是灰色的,当它点击时不会改变颜色/
 <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>
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.                           
                    }
                }
            });