2015-07-01 57 views
-1

我有一個ListView行安卓:更改竊聽的TextView的顏色爲綠色,其他黑色

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/text1"/> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/text2"/> 


</LinearLayout> 

我如何編寫代碼,這樣當點擊ListView項的文本1,該特定項目的選擇text1爲轉向文本顏色綠色.......並且所有其他項目重置爲文本顏色黑色。

到目前爲止,我已經在我的自定義這個代碼在我getView()的自定義適配器的

TextView text1 = (TextView) v.findViewById(R.id.txtDateUploaded); 

text1.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     boolean settogreen = (boolean) v.getTag(); 
     if (settogreen) { 
      ((TextView)v).setTextColor(getResources().getColor(R.color.green)); 
     } else { 
      ((TextView)v).setTextColor(getResources().getColor(R.color.black)); 
     } 
     v.setTag(!settogreen); 
    } 
}); 

而且問題是綠色的,個人的text1的黑色之間這隻切換,無論其他列表的狀態項目的文本1,不重置別人回黑色

回答

0

可以使用RadioGroup來實現這一目標:

把這樣的事情在你的佈局:

<RadioGroup 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <RadioButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New RadioButton" 
     android:textColor="@drawable/selector" 
     android:button="@null" /> 

    <RadioButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New RadioButton" 
     android:textColor="@drawable/selector" 
     android:button="@null" /> 
</RadioGroup> 

的selector.xml(在繪製文件夾添加):

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_checked="true" android:color="@android:color/holo_green_light" /> 
    <item android:color="@android:color/black" /> 
</selector> 

這自動切換文本的顏色。如果您單擊文本,文本顏色切換爲綠色,其他文本切換爲黑色。

enter image description here

相關問題