Android - No getEditText method in EditTextPreference with Preference Support Library

[亡魂溺海] 提交于 2021-02-18 21:54:12

问题


I've just had to change to using PreferenceFragmentCompat for implementation of permissions checking which means using the support version of EditTextPreference. Unfortunately it seems getEditText is missing. Is there any work around?

Update Found this issue logged. Seems it's deliberately been removed but the answer doesn't explain how to implement what I want to do.

The code I was using is as follows

final EditText editTextTL;
editTextTL = ((EditTextPreference) findPreference("my_preference")).getEditText();
editTextTL.setFilters(new InputFilter[]{new NumericRangeFilter()});
editTextTL.setOnFocusChangeListener(new AmountOnFocusChangeListener());

My relevant imports

import android.support.v7.preference.EditTextPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceFragmentCompat;

My Gradle dependencies

compile 'com.android.support:support-v4:23.0.1'
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:preference-v14:23.0.1'
compile 'com.android.support:recyclerview-v7:23.0.1'

回答1:


Create custom EditTextPreference:

public class CustomEditTextPreference extends EditTextPreference {
    public CustomEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr,
        int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
    public CustomEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public CustomEditTextPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public CustomEditTextPreference(Context context) {
        super(context);
    }
}

Declare it in xml file for prefences:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <com.example.CustomEditTextPreference
        android:key="test_key"
        android:title="Title" />
</PreferenceScreen>

Update your PreferenceFragmentCompat with the next lines:

public class PrefFragment extends PreferenceFragmentCompat {

private static final String DIALOG_FRAGMENT_TAG =
    "android.support.v7.preference.PreferenceFragment.DIALOG";

@Override
public void onCreatePreferences(Bundle bundle, String s) {
    addPreferencesFromResource(R.xml.settings);
}

@Override
public void onDisplayPreferenceDialog(Preference preference) {
    // check if dialog is already showing
    if (getFragmentManager().findFragmentByTag(DIALOG_FRAGMENT_TAG) != null) {
        return;
    }

    DialogFragment f = null;
    if (preference instanceof CustomEditTextPreference) {
        f = EditTextPreferenceDialog.newInstance(preference.getKey());
    } else {
        super.onDisplayPreferenceDialog(preference);
    }
    if (f != null) {
        f.setTargetFragment(this, 0);
        f.show(getFragmentManager(), DIALOG_FRAGMENT_TAG);
    }
}

public static class EditTextPreferenceDialog extends EditTextPreferenceDialogFragmentCompat {

    public static EditTextPreferenceDialog newInstance(String key) {
        final EditTextPreferenceDialog
            fragment = new EditTextPreferenceDialog();
        final Bundle b = new Bundle(1);
        b.putString(ARG_KEY, key);
        fragment.setArguments(b);
        return fragment;
    }

    @Override
    protected void onBindDialogView(View view) {
        super.onBindDialogView(view);
        ((EditText)view.findViewById(android.R.id.edit)).setFilters(new InputFilter[]{new NumericRangeFilter()});
    }

}

}




回答2:


Maybe this will help, I was struggling with this today, I found the solution based in fragments. This code works after EditTextPreference click, assuming that preference dialog has been shown.

List<Fragment> fragments = getSupportFragmentManager().getFragments();
Fragment dialogFragment = null;
boolean dialogFragmentFound = false;
for (Fragment f : fragments) {
    if (f != null) {
        String tag = f.getTag();
        if (tag != null) {
            tag = tag.toLowerCase().trim();
            if (tag.indexOf("preference") >= 0 && tag.indexOf("dialog") >= 0) {
                dialogFragmentFound = true;
                dialogFragment = f;
            }
        }
    }
}
if (dialogFragmentFound) {
    Dialog dialog = ((DialogFragment) dialogFragment).getDialog();
    EditText ediText = dialog.findViewById(android.R.id.edit);
    if (ediText != null)
        ediText.setText("your text");
}

Best regards




回答3:


getEditText() is not missing in the support library

check the link https://github.com/consp1racy/android-support-preference/blob/master/library/src/main/java/net/xpece/android/support/preference/EditTextPreference.java

You can get EditText,

please check the version of your support library



来源:https://stackoverflow.com/questions/32477726/android-no-getedittext-method-in-edittextpreference-with-preference-support-li

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