2012-12-26 33 views
6

我有一個Edit Text其中數字只能在0-59之間,其他號碼不會輸入那個Edit Text,我試過text-watcher但沒有得到成功。任何想法?如何在Android中的編輯文本中僅限於0-59的數字文本?

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 
+0

只有textwatcher不能處理您的情況,就像「Precondtion」「On Condition」和「Post Condition」 只需要製作自己的條件,然後用Textwatcher將其與任何3方法 –

回答

6

使用此代碼,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(""); 
     } 
    } 

}); 
+2

謝謝,但它會允許輸入不在0-59之間的任何數字 –

+0

請參閱我已編輯我的代碼: –

+0

但它給出的堆棧溢出錯誤?任何想法?在行「edittext.setText(」「+ num);」 –

0

首先添加輸入型作爲數量並限制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 

}); 
11

這裏是如何的任何消息我喜歡這樣做:

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)}); 
+1

不錯的答案.. + 1從我 –

+0

它不工作111〜999.如何改變它與11最小biger工作? – SDG69

0
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); 
         } 
        } 
       } 
      } 

     }); 
    } 

,並在XML文件EditText進入android:inputType="number"。 但是這段代碼一旦得到正確的輸入就停止工作,所以你應該在此之後停用EditText

相關問題