MonoDroid为EditText设置最大值

时间:2012-01-10 16:12:50

标签: android xamarin.android

有没有办法为活动中创建的edittext设置最大值?例如,如果我不希望用户能够输入高于8的数字等,则edittext已经设置为数字字段。

et = new EditText(this);
et.InputType = (int)Android.Text.InputTypes.ClassNumber;

3 个答案:

答案 0 :(得分:4)

您应该在同一个文件夹中创建一个新类“InputFilterMinMax.java”。

然后你应该使用过滤器:

textEdit = (TextView)findViewById(R.id.editText1);
textEdit.setFilters(new InputFilter[]{ new InputFilterMinMax("0", "180")});

在你的课堂活动中(我将min设为“0”,最大设为“180”)。

InputFilterMinMax.java代码:

import android.text.InputFilter;
import android.text.Spanned;

public class InputFilterMinMax implements InputFilter {

    private int min, max; //paramets that you send to class

    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());
            String startString = dest.toString().substring(0, dstart);
            String insert = source.toString();
            String endString = dest.toString().substring(dend);
            String parseThis = startString+insert+endString;
            int input = Integer.parseInt (parseThis);
            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;
    }
}

来源:Android set min max value an edittext accepts

答案 1 :(得分:3)

您可以使用digits attribute

为EditText指定允许的字符
<EditText
    android:digits="012345678"
    ... />

或来自代码:

editText.KeyListener = DigitsKeyListener.GetInstance("012345678");

修改

听起来你要做的就是从整数最大值到Android所需的字符串表示。你可以这样做:

int maxValue = 8;
string digits = string.Join(string.Empty, Enumerable.Range(0, maxValue + 1));
editText.KeyListener = DigitsKeyListener.GetInstance(digits);

答案 2 :(得分:1)

如果您想要一般使用min max range checked EditText,您应该执行以下操作(假设为数字输入设置了EditText):

// This handles the minimum value check, this can't be done until AFTER you have given
// the user time to give input (aka not at a character-by-character resolution)
// One easy way to do this is to wait for the user to change focus
final int minimum = 15;
edit_text.setOnFocusChangeListener(
                new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View _, boolean has_focus) {
                        if (!has_focus && Integer.parseInt(edit_text.getText()) < minimum)
                            edit_text.setText(minimum + "");
                    }
                }
        );
// This handles the maximum value check, because the number increases monotonically with
// the addition of a digit, you can see that the user is inputting a number that is larger
// than the maximum number before the user tells you they are done
final int maximum = 180;
edit_text.setFilters(new InputFilters[] {
    new InputFilter() {
        @Override
        public CharSequence filter(CharSequence source, int source_start, int source_end,
                                   Spanned destination, int destination_start, 
                                   int destination_end) {   
           try {
               String prefix = destination.toString().substring(0, destination_start);
               String insert = source.toString();
               String suffix = destination.toString().substring(destination_end);
               String input_string = prefix + insert + suffix;
               int input = Integer.parseInt(input_string);

               // Leaves input within the bound alone
               if (input <= max)
                   return null;
            } catch (NumberFormatException _) { }
            // Blocks insertions of "source" strings that make the string larger than the maximum     
            return "";
        }
    }
});

如果要多次重复使用,可以命名这些抽象类。