如何根据行状态(选中,正常,...)更改listview行中textview的文本颜色?

时间:2011-11-06 13:38:52

标签: java android listview textview

我的listview行在其自己的xml文件中定义。它包含一些textview。

如何根据行的状态(选定,聚焦,正常......)设置这些文本视图的文本颜色?

2 个答案:

答案 0 :(得分:1)

你必须为每一行使用setOnFocusChangeListener()。并访问您的文本视图,并更改文本颜色。

row.setOnFocusChangedListener(focuschangedListener);

private onFocusChangedListener focustchangedListener = new onFocusChangedListener(

            @Override
            public void onFocusChange(View row, boolean arg1) {

                //get access to textviews using row.findViewById()

                if (arg1) {
                     // view is on focus, change the textcolor

                } else {
                     // view lost focus, change the text colors to normal.
                }
            }

);

答案 1 :(得分:0)

如果您在代码中为listview设置适配器,则覆盖适配器的getView,如下所示:

adapter = new ArrayAdapter<String>(YourActivity.this , R.layout.liststylelayout)
{

                @Override
                public View getView(int position, View convertView,ViewGroup parent) 
                {
                    View v = super.getView(position, convertView, parent);
                    //v is parent view which has your textview as child
                    TextView tv1 =(TextView)v.findViewById(R.id.movielistitemstrings);          
                    //get your textview like this             
                    tv1.setTextColor(Color.parseColor("#FF8000"));      
                    //do your operations on textview                
                }                   
};