2017-05-04 7 views
0

내 코드에서 편집 텍스트를 사용하고 있습니다.이 값을 160으로 제한해야합니다. edittext의 값을 android의 특정 값으로 제한

내가 "3"으로 maxLenght을 촬영 한
android:id="@+id/txtWeight" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginLeft="5dp" 
         android:inputType="number" 
         android:layout_weight="0" 
         android:background="@drawable/border" 
         android:padding="5dp" 
         android:maxLength="3" 
         android:textSize="15sp" 
         android:maxLines="1" 
         android:hint="" 

를 사용하고있어 XML이다. 따라서 사용자는 999 개까지 입력 할 수 있습니다. 나는 그것을 원치 않는 값

editText.addTextChangedListener(new TextWatcher() { 
    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 

     // TODO Auto-generated method stub 
    } 

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

     // TODO Auto-generated method stub 
    } 

    @Override 
    public void afterTextChanged(Editable s) { 

     // TODO Auto-generated method stub 
    } 
}); 

답변

1

을 허용하지 않도록 당신은 당신의 EditText에 TextWatcher를 추가하고 변경할 수 있습니다 (160)

+0

감사가 – Abhinay

+1

감사합니다 일 버디 사용자 정의 정답으로 plase 마크를이 일했다면! –

0

을 제한하기 위해 필요한이 시도 : 다음

public class InputFilterMinMax implements InputFilter { 

private int min, max; 

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()); 
     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 et = (EditText) findViewById(R.id.myEditText); 
et.setFilters(new InputFilter[]{ new InputFilterMinMax("1", "180")}); 
1

를 수행
InputFilter if = new InputFilter[]{ new InputFilterMinMax("0", "180")} 

또는! 그것은

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

      return null; 

     } 
    };