2014-09-02 8 views
0

내가하고 싶은 : CheckBoxPreference가 선택되어 있지 않으면 CheckBoxPreference의 제목 텍스트 색상이 회색이되고 선택하면 제목의 텍스트 색상이 원래 색상으로 되돌아갑니다 (테마에 따라 다름).Android에서 CheckBoxPreference의 제목 텍스트 색상을 변경하는 방법은 무엇입니까?

지금까지 내가 한 것은 : CheckBoxPreference에 이르는 새로운 클래스를 만들었습니다.

public class CustomCheckBoxPreference extends CheckBoxPreference{ 

    TextView txtTitle; 
    int originalTextColor; 

    public CustomCheckBoxPreference(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    protected void onBindView(View view) { 

     txtTitle = (TextView) view.findViewById(android.R.id.title); 
     originalTextColor = txtTitle.getCurrentTextColor(); 

     setOnPreferenceClickListener(new OnPreferenceClickListener() { 
      @Override 
      public boolean onPreferenceClick(Preference preference) { 
       if (isChecked()) { 
        txtTitle.setTextColor(originalTextColor); //it doesn't work 
       } 
       else { 
        txtTitle.setTextColor(Color.GRAY); //it doesn't work 
       } 
       return true; 
      } 
     }); 

     super.onBindView(view); 
    } 
} 

내가 응용 프로그램을 실행

txtTitle.setTextColor(..) 분명히 텍스트 색은 전혀 변경하지 않은, 작동하지 않았다. 또한 디버거에서 onPreferenceClick 메서드가 호출되었음을 확인했습니다.

+0

isChecked() 체크를 사용하고 있지만 정의 된 위치는 무엇입니까? icChecked()를 사용하는 대신에 ((CheckBoxPreference) 환경 설정) .isChecked()를 사용해야합니다. –

+0

@RajenRaiyarela : CheckBoxPreference 클래스에서 확장되었습니다. – null

답변

0

나는 똑같이했고 나에게 효과적이지 못했다. 나도 그 이유를 모른다.

onPreferenceClickListener()을 제거하고 else 만 사용하는 경우 작동합니다.

protected void onBindView(View view) { 

    txtTitle = (TextView) view.findViewById(android.R.id.title); 
    originalTextColor = txtTitle.getCurrentTextColor(); 


      if (isChecked()) { 
       txtTitle.setTextColor(originalTextColor); 
      } 
      else { 
       txtTitle.setTextColor(Color.GRAY); 
      } 
      return true; 

    super.onBindView(view); 

}