问题描述
我只需要更改为使用PreferenceFragmentCompat来实现权限检查,这意味着使用支持版本的EditTextPreference.不幸的是,似乎缺少getEditText.有什么解决方法吗?
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?
更新发现此问题已记录.似乎是故意将其删除了,但答案并没有说明如何实现我想做的事情.
UpdateFound this issue logged. Seems it's deliberately been removed but the answer doesn't explain how to implement what I want to do.
我使用的代码如下
final EditText editTextTL;
editTextTL = ((EditTextPreference) findPreference("my_preference")).getEditText();
editTextTL.setFilters(new InputFilter[]{new NumericRangeFilter()});
editTextTL.setOnFocusChangeListener(new AmountOnFocusChangeListener());
我相关的进口商品
import android.support.v7.preference.EditTextPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceFragmentCompat;
我的Gradle依赖项
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'
推荐答案
创建自定义EditTextPreference:
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);
}
}
在xml文件中声明其正确性:
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>
用以下几行更新您的PreferenceFragmentCompat:
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()});
}
}
}
这篇关于Android-具有首选项支持库的EditTextPreference中没有getEditText方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!