EditTextPreference - only numeric value inputType - isn't working

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


        
相关标签:
8条回答
  • 2021-01-18 10:16

    It happens when using android.support.v14.preference.PreferenceFragment or android.support.v7.preference.PreferenceFragmentCompat even after choosing android:inputType="numberDecimal". This issue could be overcome by using android.preference.PreferenceFragment. Unfortunately, it's deprecated in API level 28.

    Even refactoring to AndroidX, using androidx.preference.PreferenceFragment or androidx.preference.PreferenceFragmentCompat didn't fix the issue.

    0 讨论(0)
  • 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

    <android.support.v7.preference.PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android">
        <android.support.v7.preference.EditTextPreference
            .../>
    </android.support.v7.preference.PreferenceScreen>
    
    0 讨论(0)
  • 2021-01-18 10:21

    EditTextPreference widgets should take the same attributes as a regular EditText, so use:

    android:inputType="number"
    
    0 讨论(0)
  • 2021-01-18 10:22
    android:digits="0123456789"
    

    use this in edittext as it accepts only defined numbers. if its not working then use Android-Support-Preference-V7-Fix library.

    Fixed EditTextPreference forwards the XML attributes (like inputType) to the EditText, just like the original preference did.

    0 讨论(0)
  • 2021-01-18 10:23

    I agree that original support preferences has some issues, but for resolving this issue we just need add custom layout and specify EditText with android:inputType="number"

    <android.support.v7.preference.EditTextPreference
                android:dialogLayout="@layout/preference_dialog_edittext_custom"
    

    So that you may copy original preference_dialog_layout.xml file and edit it.

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="48dp"
        android:layout_marginBottom="48dp"
        android:overScrollMode="ifContentScrolls">
    
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginStart="24dp"
          android:layout_marginEnd="24dp"
          android:orientation="vertical">
    
        <TextView android:id="@android:id/message"
            style="?android:attr/textAppearanceSmall"
            android:layout_marginBottom="48dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="?android:attr/textColorSecondary" />
    
        <EditText
            android:id="@android:id/edit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:layout_marginStart="-4dp"
            android:layout_marginEnd="-4dp" />
    
      </LinearLayout>
    
    </ScrollView>
    
    0 讨论(0)
  • 2021-01-18 10:27

    You can retrieve the EditText from the Preference and from there setInputTypes or use KeyListeners to inform the keyboard:

    EditText et = (EditText) editTextPref.getEditText();
    et.setKeyListener(DigitsKeyListener.getInstance());
    

    found the answer on older thread

    0 讨论(0)
提交回复
热议问题