본문 바로가기

구글과인터넷/안드로이드

안드로이드 EditText의 최대 입력 가능 길이 제한에 관한 코드

출처: http://www.androidpub.com/48864


EditText를 사용하며 글자수를 몇글자 이하만 저장될 수 있도록 하고 싶은데..

혹시 옵션으로 그런것을 설정 할 수 있는지 궁금합니다.. 

그게 아니면 글자 한글자씩 입력 될때마다 카운트를 해야하나요?;;

공지사항을 다 읽었음

댓글
2009.12.08 16:13:29
딱신
EditText는 TextView를 상속받은 클래스이고,
TextView에는 아래와 같은 
글자입력 길이 제한 속성을 제공하고 있네요...

 <EditText 
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:maxLength="5"
 />
댓글
2010.04.26 18:13:55
howisgeek
inputfilter 사용하세요.



////////////////////////////////////////////////////////////


출처; http://chaotic21c.egloos.com/10537099

ditText를 이용하여 텍스트를 입력 받을 때 특정한 길이만큼 제한을 줄 필요가 있다.

상위클래스인 TextView의 android:maxLength라는 xml attribute를 이용해도 되겠지만,
xml의 layout을 사용하지 않고 code상에서 구현을 할 수도 있다. 당연히.......ㅎㅎ

 editor = new EditText(this);
 editor.setSingleLine();
 int maxLength = 32;
 InputFilter[] filterArray = new InputFilter[1];
 filterArray[0] = new InputFilter.LengthFilter(maxLength);
 editor.setFilters(filterArray);
요렇게 하면 한줄로 된, 32글자까지 입력을 받을 수 있는 EditText를 만들 수 있다. ^^