2014-10-17 5 views
2

사용자가 소수점 이하 3 자리가있는 숫자를 입력하지 못하도록하는 EditText가 있습니다.안드로이드 : 사용자가 3 자리가 넘는 숫자를 입력하는 것을 방지하는 방법

numeric(15, 3) 

내가 어떻게 할 수있는 당신이 어떤 생각을 가지고 있습니까 :이 때문에 나는이 데이터 유형은 한 전화에서 보내는 데이터를 저장하는 것입니다 SQL 서버의 데이터베이스에? 이것은 내가 뭘하려

android:maxLength="15" 
android:lines="1" 
android:inputType="numberDecimal" 

편집

을 :

는 이미이 값을 설정했지만, 그들은 부분적으로 만 나에게 도움이 될

  mQuantityEditText.addTextChangedListener(new TextWatcher(){ 
     @Override 
     public void afterTextChanged(Editable s) { 
      String str = mQuantityEditText.getText().toString(); 
      DecimalFormat format=(DecimalFormat) DecimalFormat.getInstance(); 
      DecimalFormatSymbols symbols=format.getDecimalFormatSymbols(); 
      char sep=symbols.getDecimalSeparator(); 


      int indexOFdec = str.indexOf(sep);   

      if(indexOFdec >=0) { 
       if(str.substring(indexOFdec,str.length()-1).length() >3) 
       { 
        s.replace(0, s.length(),str.substring(0, str.length()-1));      
       } 
      } 
     } 
     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

     } 
     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) {      


     }  
    }); 

그것은 그것 때문에 일에만 소수점 이하 3 자리까지 허용하지만 숫자에 맞게 최대 자릿수를 제어하는 ​​방법을 아직 모르겠습니다.

미리 감사드립니다.

+1

소수점 이하 3 자리로 반올림 한 숫자. – biegleux

+0

여기 좀 보시라. http://stackoverflow.com/questions/5357455/limit-decimal-places-in-android-edittext –

+0

onTextChange 리스너를 구현하고 정규식을 수행하는 것이 어떨까요? –

답변

0

문자의 제한된 수의 경우, 당신은 안드로이드 API 필터를 추가 할 수 있습니다

DigitsKeyListener MyDigitKeyListener = new DigitsKeyListener(true, true); 
editEntryView.setKeyListener(MyDigitKeyListener); 
: 숫자 EDITTEXT를 들어

InputFilter[] FilterArray = new InputFilter[1]; 
FilterArray[0] = new InputFilter.LengthFilter(8); 
editEntryView.setFilters(FilterArray); 

을, 또한 obscur 솔루션이

+0

어떻게 그게 모호한가요? (또한 입력 크기를 제한하지 않음) – njzk2

0
public class RestrictDecimal implements InputFilter { 

Pattern mPattern; 

public RestrictDecimal(int beforePoint,int afterPoint) { 
    mPattern=Pattern.compile("[0-9]{0," + (beforePoint-1) + "}+((\\.[0-9]{0," + (afterPoint-1) + "})?)||(\\.)?"); 
} 

@Override 
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { 

     Matcher matcher=mPattern.mat`enter code here`cher(dest);  
     if(!matcher.matches()) 
      return ""; 
     return null; 
    } 

} 

// Set for edit Text 
editEntryView.setFilters(new InputFilter[] {new RestrictDecimal(15,3)}); 
+0

코드의 형식을 지정하고 솔루션이 질문에 대한 대답 인 이유를 설명하십시오 – njzk2

관련 문제