简体   繁体   English

单击列表项时,如何在ListView中更改TextView文本颜色?

[英]How to change TextView text color inside a ListView when list item is clicked?

I am trying to change text color of a TextView inside a listview item. 我正在尝试更改列表视图项内TextView的文本颜色。

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                view.setSelected(true);

               ViewGroup selectedGroup = (ViewGroup)view;

                ((TextView)selectedGroup.getChildAt(4)).setTextColor(Color.parseColor("#336699"));
                String mID =String.valueOf(((TextView) selectedGroup.getChildAt(4)).getText());
            }
        });

But nothing happens, I can get text value of the textview, though. 但是什么也没发生,不过我可以得到textview的文本值。 What might be wrong in this code? 此代码可能有什么问题?

This is, by the way, my listView layout. 顺便说一下,这是我的listView布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="4dp">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:textColor="#336699"
        android:id="@+id/questionTitle"
        android:textSize="18sp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#444444"
        android:id="@+id/questionDate"
        android:textSize="12sp"/>
    <TextView android:layout_height="0dp" android:layout_width="0dp" android:visibility="invisible"
        android:id="@+id/questionContent" android:value=""/>
    <TextView android:layout_height="0dp" android:layout_width="0dp" android:visibility="invisible"
        android:id="@+id/questionSenderContact" android:value=""/>
    <TextView android:layout_height="0dp" android:layout_width="0dp" android:visibility="invisible"
              android:id="@+id/messageID" android:value=""/>

</LinearLayout>

EDIT: I was getting wrong textview. 编辑:我得到错误的textview。 Index of it was not 4, but 0. Sorry for a question caused by lack of attention. 它的索引不是4,而是0。抱歉,由于注意力不足引起的问题。

try this code: 试试这个代码:

public View row;

your_list.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> a, View v,
                    int position, long id) {

if (row != null) {
    row.setBackgroundResource(R.color.orange);
}
row = v;
v.setBackgroundResource(R.color.transparent_green);
)};

Create a new StateListDrawable like you did before but with black as default color and white when pressed. 像以前一样创建一个新的StateListDrawable,但是将黑色设置为默认颜色,并在按下时设置白色。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="@color/black" />
<item android:state_focused="true" android:color="@color/black" />
<item android:state_pressed="true" android:color="@color/black" />
<item android:color="@color/white" />
</selector>

Now for in the TextView change the text color to the new drawable: 现在在TextView中将文本颜色更改为新的drawable:

android:textColor="@color/list_item_text"

More on StateListDrawables: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList 有关StateListDrawables的更多信息: http : //developer.android.com/guide/topics/resources/drawable-resource.html#StateList

you will have to set textview as tag to the clickable button as: 您必须将textview设置为可点击按钮的标签,如下所示:

...
view.setOnClickListener(this);
view.setTag(textView);
...


public void onclick(View v){
  ...
  TextView textView = (TextView)v.getTag();
  textView.setTextColor(getResources().getColor(R.color.YOURCOLOR));
  ...
}

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

相关问题 单击该项目时如何更改项目列表视图的文本视图颜色? - How to change the textview color of item listview when that item is on click? 点击textview时如何改变背景颜色和文字颜色? - How to change the background color and text color when textview is clicked? 在列表视图中动态更改(列表项)TextView的颜色 - Dynamically change color of (List item)TextView in Listview 动态更改ListView中TextView的文本颜色? - Dynamically change text color of a TextView inside a ListView? 如何根据变量更改TextView(在listview中)的文本颜色? - How to change a TextView's (inside a listview) text color according to a variable? 单击时更改列表视图项的背景颜色 - change the background color of a listview item when clicked 当ArrayList中的值等于Android中的某个值时,如何更改ListView项目的文本颜色? - How do I change the text color of a ListView item when a value inside an ArrayList is equals to a certain value in Android? 选择ListView项时,更改TextView的字体颜色 - Change the font color of TextView when ListView Item gets selected 在列表视图中选择列表项时,如何更改其颜色? - how to change color of list item when it is selected in listview? 在android中单击按钮时更改textview文本颜色 - Change the textview text color when button is clicked in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM