简体   繁体   English

如何基于行状态(已选择,正常,…)更改listview行内的textview的文本颜色?

[英]How to change text color of textviews inside listview row basd on row state (selected, normal, …)?

My row for listview is defined in its own xml file. 我的listview行在其自己的xml文件中定义。 It contains some textviews. 它包含一些文本视图。

How can i set the text color of these textviews based on the state of the row (selected , focused, normal ...)? 如何根据行的状态(选中,聚焦,普通...)设置这些文本视图的文本颜色?

you have to use setOnFocusChangedListener() for each row . 您必须为每行使用setOnFocusChangedListener()。 and get access to your textViews, and change the text color. 并访问您的textViews,并更改文本颜色。

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

);

If you are setting adapter for listview in your code then override getView of your adapter like this: 如果您在代码中为列表视图设置适配器,请像这样重写适配器的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                
                }                   
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM