方法比較簡單,監聽輸入的數值是否大於或小於指定的值即可,如果大於了,就給edit設置上最大的值,如果小於了就給edit設置最小的值
注意要在xml中指定inputType,不然字符串轉int或者double的時候會出現異常
- /** edittext只能輸入數值的時候做最大最小的限制 */
- public static void setRegion(final EditText edit, final double MIN_MARK, final double MAX_MARK) {
- edit.addTextChangedListener(new TextWatcher() {
- @Override
- public void onTextChanged(CharSequence s, int start, int before, int count) {
- if (start > 1) {
- if (MIN_MARK != -1 && MAX_MARK != -1) {
- double num = Double.parseDouble(s.toString());
- if (num > MAX_MARK) {
- s = String.valueOf(MAX_MARK);
- edit.setText(s);
- } else if (num < MIN_MARK) {
- s = String.valueOf(MIN_MARK);
- edit.setText(s);
- }
- edit.setSelection(s.length());
- }
- }
- }
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count, int after) {
- }
- @Override
- public void afterTextChanged(Editable s) {
- if (s != null && !s.equals("")) {
- if (MIN_MARK != -1 && MAX_MARK != -1) {
- double markVal = 0;
- try {
- markVal = Double.parseDouble(s.toString());
- } catch (NumberFormatException e) {
- markVal = 0;
- }
- if (markVal > MAX_MARK) {
- edit.setText(String.valueOf(MAX_MARK));
- edit.setSelection(String.valueOf(MAX_MARK).length());
- }
- return;
- }
- }
- }
- });
- }