Change EditTextPreference dialog input text color

早过忘川 提交于 2020-12-12 11:19:06

问题


I am able to change the title and summary color of my edit text preference. When you click into it I have the background dark and the buttons white. I still need to figure out how to change the actual value text. Any ideas?

Would be great if I could change the color of that line too.

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Typeface;
import android.preference.EditTextPreference;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.kisnardonline.kisnardonline.GameActivity;
import com.kisnardonline.kisnardonline.R;

@SuppressLint("AppCompatCustomView")
public class FontEditTextPreference extends EditTextPreference {
    public FontEditTextPreference(Context context) {
        super(context);
    }

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

    public FontEditTextPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected View onCreateView(ViewGroup viewGroup) {
        Typeface customFont = FontCache.getTypeface("UnderwoodChampionRegular.ttf", GameActivity.commandReceiver.activity_settings);
        View v = super.onCreateView(viewGroup);
        try {
            TextView titleView = (TextView) v.findViewById(android.R.id.title);
            titleView.setTypeface(customFont);
            titleView.setTextColor(ContextCompat.getColor(GameActivity.commandReceiver.activity_settings, R.color.THEME_LIGHT_TEXT));
            TextView summaryView = (TextView) v.findViewById(android.R.id.summary);
            summaryView.setTypeface(customFont);
            summaryView.setTextColor(ContextCompat.getColor(GameActivity.commandReceiver.activity_settings, R.color.THEME_LIGHT_TEXT));
        } catch (Exception e) {
            Log.e("jay", "Exception in FontEditTextPreference", e);
        }

        return v;
    }

    @Override
    protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
        super.onPrepareDialogBuilder(builder);
        builder.getContext().setTheme(R.style.JayPreferenceThemeDialog);
    }
}

My style is here:

<style name="JayPreferenceThemeDialog"  >
    <item name="android:colorBackground">@color/THEME_LIGHTER_BACKGROUND</item>
    <item name="android:textColor">@android:color/background_light</item>
    <item name="android:textColorSecondary">@android:color/background_light</item>
</style>

回答1:


<android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/prompt_password"
                android:imeActionId="6"
                android:textColor="@color/white"
                android:textColorHint="@color/white"
                android:imeActionLabel="@string/action_sign_in_short"
                android:imeOptions="actionUnspecified"
                android:inputType="textPassword"
                android:maxLines="1"
                android:singleLine="true" />

        </android.support.design.widget.TextInputLayout>



回答2:


Try this:

<style name="JayPreferenceThemeDialog" parent="Theme.AppCompat.Light.Dialog">
        <item name="colorAccent">@color/colorAccent</item> //line color
        <item name="android:colorBackground">@color/THEME_LIGHTER_BACKGROUND</item>
        <item name="android:textColor">@android:color/background_light</item>
        <item name="android:textColorSecondary">@android:color/background_light</item>
 </style>



回答3:


From the link Zealous shared: Android Theming Preference Dialog

@Override
protected void showDialog(Bundle state) {
    super.showDialog(state);
    Typeface customFont = FontCache.getTypeface("UnderwoodChampionRegular.ttf", GameActivity.commandReceiver.activity_settings);

    final Resources res = getContext().getResources();
    final Window window = getDialog().getWindow();

    // Title
    final int titleId = res.getIdentifier("alertTitle", "id", "android");
    final View title = window.findViewById(titleId);
    if (title != null) {
        ((TextView) title).setTextColor(ContextCompat.getColor(MyCommandReceiver.context_settings, R.color.THEME_LIGHT_TEXT));
        ((TextView) title).setTypeface(customFont);
    }

    // Title divider
    final int titleDividerId = res.getIdentifier("titleDivider", "id", "android");
    final View titleDivider = window.findViewById(titleDividerId);
    if (titleDivider != null) {
        titleDivider.setBackgroundColor(ContextCompat.getColor(MyCommandReceiver.context_settings, R.color.THEME_LIGHT_TEXT));
    }

    // EditText
    final View editText = window.findViewById(android.R.id.edit);
    if (editText != null) {
        ((EditText) editText).setTextColor(ContextCompat.getColor(MyCommandReceiver.context_settings, R.color.THEME_LIGHT_TEXT));
        ((EditText) editText).setTypeface(customFont);
    }

    //OK button
    final View okButton = window.findViewById(android.R.id.button1);
    if (okButton != null) {
        ((Button) okButton).setTextColor(ContextCompat.getColor(MyCommandReceiver.context_settings, R.color.THEME_LIGHT_TEXT));
        ((Button) okButton).setTypeface(customFont);
    }

    //Cancel button
    final View cancelButton = window.findViewById(android.R.id.button2);
    if (cancelButton != null) {
        ((Button) cancelButton).setTextColor(ContextCompat.getColor(MyCommandReceiver.context_settings, R.color.THEME_LIGHT_TEXT));
        ((Button) cancelButton).setTypeface(customFont);
    }
}


来源:https://stackoverflow.com/questions/51779376/change-edittextpreference-dialog-input-text-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!