如何创建仅接受预定义范围值的EditTextPreference?

时间:2017-04-17 07:41:25

标签: android

我希望EditTextPreference只接受6000到65536之间的值,我该怎么办?谢谢!

顺便说一下,代码A不起作用,我无法在EditTextPreference的编辑框中输入任何数字,哪个键是“WebServerPort”

<EditTextPreference
    android:key="WebServerPort"
    android:defaultValue="6000"
    android:title="Port"
    android:summary="Set port for web server, it should be greater than 6000 and less than 65536"
    android:inputType="number"
    android:layout="@layout/layout_customize_preference_item"
/>

代码A

public class UIPreference extends PreferenceActivity {
    private AdView adView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.mypreference);
        setContentView(R.layout.layout_preference);


        EditTextPreference edit_Pref = (EditTextPreference) getPreferenceScreen().findPreference("WebServerPort");
        edit_Pref.getEditText().setFilters(new InputFilter[]{ new InputFilterMinMax("6000", "65536")});
    }
}


class InputFilterMinMax implements InputFilter {

    private int min, max;

    public InputFilterMinMax(int min, int max) {
        this.min = min;
        this.max = max;
    }

    public InputFilterMinMax(String min, String max) {
        this.min = Integer.parseInt(min);
        this.max = Integer.parseInt(max);
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        try {
            int input = Integer.parseInt(dest.toString() + source.toString());
            if (isInRange(min, max, input))
                return null;
        } catch (NumberFormatException nfe) { }
        return "";
    }

    private boolean isInRange(int a, int b, int c) {
        return b > a ? c >= a && c <= b : c >= b && c <= a;
    }
}

2 个答案:

答案 0 :(得分:1)

您可以将TextWatcher分配给EditText并在那里监听文本更改,例如:

editTextPreference.getEditText().addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            try {
                long val = Long.parseLong(s.toString());
                if(val > 65536) {
                     s.replace(0, s.length(), "65536", 0, 5);
                } else if(val < 6000) {
                     s.replace(0, s.length(), "6000", 0, 4);
                }
            } catch (NumberFormatException ex) {
                 // Do something
            }
      }
});

答案 1 :(得分:0)

尝试在editTextPreference.getEditText()上添加InputFilter

InputFilter filter = new InputFilter() {
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            int number = 0;

            try {
                number = Integer.parseInt(source.toString());
            } catch (Exception e) {
            }
            if(number > 6000 && number < 65536) {
                return source;
            }
            return null;
        }
    };
    editTextPreference.getEditText().setFilters(new InputFilter[] { filter });