13

我有一个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
4

4 回答 4

19

这是我喜欢这样做的方式:

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)});
于 2014-08-30T01:10:46.423 回答
9

在 XML 中使用此代码以仅在Edit Text中允许数字:

<EditText android:id="@+id/edit_text_id"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:inputType="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("");
        }
    }

}); 
于 2012-12-26T06:22:44.943 回答
2

首先将输入类型添加为数字并限制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

});
于 2012-12-26T06:24:52.700 回答
2
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"EditTextxml 文件中的条目中。但是一旦得到正确的输入,这个代码就会停止工作,所以你应该EditText在那之后停用。

于 2015-05-22T19:48:49.477 回答