android.support.v7.preference.EditTextPreference
doesn't contain getEditText()
method.
But we can extends android.support.v7.preference.EditTextPreferenceDialogFragmentCompat
to set inputType.
public class EditTextPreferenceDialogFragmentCompat extends android.support.v7.preference.EditTextPreferenceDialogFragmentCompat {
private EditText mEditText;
private int mInputType;
public static EditTextPreferenceDialogFragmentCompat newInstance(String key, int inputType) {
EditTextPreferenceDialogFragmentCompat fragment = new EditTextPreferenceDialogFragmentCompat();
Bundle b = new Bundle(2);
b.putString("key", key);
b.putInt("inputType", inputType);
fragment.setArguments(b);
return fragment;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mInputType = this.getArguments().getInt("inputType");
}
protected void onBindDialogView(View view) {
this.mEditText = view.findViewById(android.R.id.edit);
mEditText.setInputType(mInputType);
super.onBindDialogView(view);
}
}
Then make your activity implements PreferenceFragmentCompat.OnPreferenceDisplayDialogCallback
Use your EditTextPreferenceDialogFragmentCompat
instead of android.support.v7.preference.EditTextPreferenceDialogFragmentCompat
public boolean onPreferenceDisplayDialog(@NonNull PreferenceFragmentCompat preferenceFragmentCompat, Preference preference) {
String key = preference.getKey();
if (/**show your dialog*/) {
EditTextPreferenceDialogFragmentCompat f = EditTextPreferenceDialogFragmentCompat.newInstance(preference.getKey(), InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);
f.setTargetFragment(this, 0);
f.show(getFragmentManager(), "android.support.v14.preference.PreferenceFragment.DIALOG");
return true;
}
return false;
}
just use android.support.v7.preference.EditTextPreference
in xml
<android.support.v7.preference.PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.preference.EditTextPreference
.../>
</android.support.v7.preference.PreferenceScreen>