EditTextPreference - only numeric value inputType - isn't working

前端 未结 8 541
迷失自我
迷失自我 2021-01-18 10:16


        
8条回答
  •  心在旅途
    2021-01-18 10:18

    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

    
        
    
    

提交回复
热议问题