如何只从0-59限制在Android设备上编辑文本中号的文本?(How to limit the t

2019-08-04 17:09发布

我有一个Edit Text ,其中的数字就只能在0-59 ,没有其他号码将在该类型的Edit Text ,我想text-watcher却没有获得成功。 任何的想法?

12-26 14:59:39.715: E/AndroidRuntime(19494): FATAL EXCEPTION: main
12-26 14:59:39.715: E/AndroidRuntime(19494): java.lang.StackOverflowError
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.findViewTraversal(ViewGroup.java:2765)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.View.findViewById(View.java:10442)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.findViewTraversal(ViewGroup.java:2765)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.View.findViewById(View.java:10442)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.findViewTraversal(ViewGroup.java:2765)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.View.findViewById(View.java:10442)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.findViewTraversal(ViewGroup.java:2765)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.View.findViewById(View.java:10442)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.findViewTraversal(ViewGroup.java:2765)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.View.findViewById(View.java:10442)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.findViewTraversal(ViewGroup.java:2765)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.View.findViewById(View.java:10442)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.findViewTraversal(ViewGroup.java:2765)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.View.findViewById(View.java:10442)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.findViewTraversal(ViewGroup.java:2765)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.View.findViewById(View.java:10442)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.View.findViewShouldExist(View.java:4281)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.View.findUserSetNextFocus(View.java:4263)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.FocusFinder.findNextFocus(FocusFinder.java:66)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.focusSearch(ViewGroup.java:569)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.focusSearch(ViewGroup.java:571)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.focusSearch(ViewGroup.java:571)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.focusSearch(ViewGroup.java:571)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.focusSearch(ViewGroup.java:571)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.focusSearch(ViewGroup.java:571)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.focusSearch(ViewGroup.java:571)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.focusSearch(ViewGroup.java:571)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.focusSearch(ViewGroup.java:571)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.View.focusSearch(View.java:4221)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.onCreateInputConnection(TextView.java:5378)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.inputmethod.InputMethodManager.startInputInner(InputMethodManager.java:977)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.inputmethod.InputMethodManager.restartInput(InputMethodManager.java:932)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:2958)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:2907)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.EditText.setText(EditText.java:90)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:2882)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at com.fishing.MyLocation$check_lat_degree.onTextChanged(MyLocation.java:236)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.sendOnTextChanged(TextView.java:7226)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:3045)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:2907)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.EditText.setText(EditText.java:90)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:2882)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at com.fishing.MyLocation$check_lat_degree.onTextChanged(MyLocation.java:236)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.sendOnTextChanged(TextView.java:7226)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:3045)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:2907)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.EditText.setText(EditText.java:90)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:2882)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at com.fishing.MyLocation$check_lat_degree.onTextChanged(MyLocation.java:236)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.sendOnTextChanged(TextView.java:7226)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:3045)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:2907)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.EditText.setText(EditText.java:90)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:2882)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at com.fishing.MyLocation$check_lat_degree.onTextChanged(MyLocation.java:236)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.sendOnTextChanged(TextView.java:7226)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:3045)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:2907)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.EditText.setText(EditText.java:90)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:2882)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at com.fishing.MyLocation$check_lat_degree.onTextChanged(MyLocation.java:236)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.sendOnTextChanged(TextView.java:7226)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:3045)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:2907)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.EditText.setText(EditText.java:90)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.j

Answer 1:

使用这个代码在XML只允许在编辑文本的数字:

<EditText android:id="@+id/edit_text_id"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:iputType="numbers" />

要控制在EditText上输入的文本值:

edittext.addTextChangedListener(new TextWatcher(){
    public void afterTextChanged(Editable s) {}

    public void beforeTextChanged(CharSequence s, int start, int count, int after){}

    public void onTextChanged(CharSequence s, int start, int before, int count){
        String strEnteredVal = edittext.getText().toString();

        if(!strEnteredVal.equals("")){
        int num=Integer.parseInt(strEnteredVal);
        if(num<60){
         edittext.setText(""+num);
        }else{
         edittext.setText("");
        }
    }

}); 


Answer 2:

以下是我想做到这一点:

public class InputFilterMinMax implements InputFilter {
    private int min;
    private int max;

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

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        //noinspection EmptyCatchBlock
        try {
            int input = Integer.parseInt(dest.subSequence(0, dstart).toString() + source + dest.subSequence(dend, dest.length()));
            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;
    }
}

然后设置您的EditText过滤器:

myEditText.setFilters(new InputFilter[]{new InputFilterMinMax(0, 59)});


Answer 3:

首先添加输入类型的数量,限制了EDITTEXT输入的数量。

editText.setInputType( InputType.TYPE_CLASS_NUMBER );
    InputFilter[] FilterArray = new InputFilter[1];
    FilterArray[0] = new InputFilter.LengthFilter(2);
    editText.setFilters(FilterArray);

然后

添加TextWatcher检查什么用户键入如果该值超过59显示一个警告或数量将小于60的任何消息。

editText.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub

    String added_number = editText.getText().toString();
    if (added_number.length != 0) {
        int number  = Integer.parseInt(added_number);

        if (number > 59)
            Toast.makeText(getApplicationContext, "Not more than 59", Toast.LENGTH_SHORT).show();
    }

}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    // TODO Auto-generated method stub
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    // TODO Auto-generated method stub

});


Answer 4:

void addAddTextChangeListener(final EditText et) {
        et.addTextChangedListener(new TextWatcher() {
            boolean textModified = false;
            @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) {
                String strEnteredVal = et.getText().toString();
                if(!strEnteredVal.equals("") && !(strEnteredVal.length() < 2)) {
                    int num = Integer.parseInt(strEnteredVal);
                    if (!textModified) {
                        if (num > 60) {
                            et.setText("");
                        } else {
                            textModified = true;
                            et.setText("" + num);
                            et.setEnabled(false);
                        }
                    }
                }
            }

        });
    }

android:inputType="number"EditText在XML文件条目。 但是这个代码停止工作,一旦得到正确的输入,所以你应该禁用EditText之后。



文章来源: How to limit the text in numbers only from 0-59 in Edit Text in Android?