2

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

    }
};

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

我怎样才能做到这一点 ?任何建议表示赞赏。

提前致谢

4

3 回答 3

1

在可绘制文件夹中创建 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>

或者

请参阅链接如何在聚焦时更改 TableRow 的背景颜色?

于 2013-11-15T07:15:03.843 回答
1

This non-xml fix causes the previously selected row to revert to the neutral table background color (Lt. grey) when an additional row is selected and highlighted (yellow). 这是通过引用选定(子)行的适当索引号来实现的。

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.                           
                    }
                }
            });

祝你好运!

于 2015-03-20T16:18:28.833 回答
0

尝试这个

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-11-15T07:13:24.167 回答