2016-04-07 16 views
0

Görünümün durumu checked olduğunda, CheckedTextView renk tonunu dinamik olarak değiştirmek istiyorum. CheckedTextView numaralı telefondan setCheckMarkTintList numaralı telefonu arayarak bunu başarabileceğime eminim. Bunu yapmak için, ColorStateList'a ihtiyacım var, ancak sorundurumu hariç, CheckedTextView'un her durumu için tüm renkleri korumak istiyorum.CheckedTextView öğesinin Onaltılık Değerli Checked Tint Renk Nasıl Değiştirilir

Yani, CheckedTextView ait ColorStateList elde edebilirsiniz ama checked devlet için sadece rengini değiştirmek için bir yol bilmiyorum. Yeni bir ColorStateList oluşturabildiğimi biliyorum, ancak orijinaldeki tüm değerleri koruduğundan nasıl emin olabilirim?

int[][] states = new int[][] { 
    new int[]{android.R.attr.state_pressed}, 
    new int[]{-android.R.attr.state_pressed}, 
    new int[]{android.R.attr.state_focused}, 
    new int[]{-android.R.attr.state_focused}, 
    new int[]{android.R.attr.state_selected}, 
    new int[]{-android.R.attr.state_selected}, 
    new int[]{android.R.attr.state_checkable}, 
    new int[]{-android.R.attr.state_checkable}, 
    new int[]{android.R.attr.state_checked}, 
    new int[]{-android.R.attr.state_checked}, 
    new int[]{android.R.attr.state_enabled}, 
    new int[]{-android.R.attr.state_enabled}, 
    new int[]{android.R.attr.state_window_focused}, 
    new int[]{-android.R.attr.state_window_focused}, 
    new int[]{} // default state 
} 

Ve orijinal ColorStateList gelen renklerden bir renk listesi oluşturmak:

Böyle bir durum listesi oluşturabilir

int[] colors = new int[] { 
    stateList.getColorForState(new int[]{android.R.attr.state_pressed}, stateList.getDefaultColor()), 
    stateList.getColorForState(new int[]{-android.R.attr.state_pressed}, stateList.getDefaultColor()), 
    stateList.getColorForState(new int[]{android.R.attr.state_focused}, stateList.getDefaultColor()), 
    stateList.getColorForState(new int[]{-android.R.attr.state_focused}, stateList.getDefaultColor()), 
    stateList.getColorForState(new int[]{android.R.attr.state_selected}, stateList.getDefaultColor()), 
    stateList.getColorForState(new int[]{-android.R.attr.state_selected}, stateList.getDefaultColor()), 
    stateList.getColorForState(new int[]{android.R.attr.state_checkable}, stateList.getDefaultColor()), 
    stateList.getColorForState(new int[]{-android.R.attr.state_checkable}, stateList.getDefaultColor()), 
    Color.parseColor(colorHexValue), 
    stateList.getColorForState(new int[]{-android.R.attr.state_checked}, stateList.getDefaultColor()), 
    stateList.getColorForState(new int[]{android.R.attr.state_enabled}, stateList.getDefaultColor()), 
    stateList.getColorForState(new int[]{-android.R.attr.state_enabled}, stateList.getDefaultColor()), 
    stateList.getColorForState(new int[]{android.R.attr.state_window_focused}, stateList.getDefaultColor()), 
    stateList.getColorForState(new int[]{-android.R.attr.state_window_focused}, stateList.getDefaultColor()), 
    stateList.getDefaultColor() 
} 

Ama bu sadece yalnız devletleri kapsayacak .. new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed, -android.R.attr.state_checked} gibi durumları da birleştirebilirsiniz. Her olası durumu hesaba katmak saçma olurdu, bu yüzden orijinal ColorStateList'un hangi durumları belirlediğini nasıl bilebilirim? Bunu yapmanın daha kolay bir yolu var mı? Bunu muhtaç mıyım?

cevap

0

CheckedTextView da renklendirme oldukça güzeldir. Sonunda ben onClickListener yılında takas renklerle münhal:

checkedTextView.setOnClickListener { 
    if (checkedTextView.isChecked) { 
     checkedTextView.checkMarkTintList = ColorStateList.valueOf(color1) 
    } else { 
     checkedTextView.checkMarkTintList = ColorStateList.valueOf(color2) 
    } 
} 

(örnek KOTLIN içinde Java benzerdir)

İlgili konular