0

私はこれをしたい: CheckBoxPreference がチェックされていない場合、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メソッドが呼び出されたこともデバッガーで確認しました。

4

1 に答える 1

0

私も同じことをしてうまくいきませんでしたが、私も理由がわかりません。

ただし、それ以外の場合にのみ削除onPreferenceClickListener()して使用すると機能します。

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);

}
于 2014-10-08T10:31:16.893 に答える