본문 바로가기

Android

setFilter를 사용한 EditText 길이제한 주기

In this tutorial, I am going to explain how to create a EditText programatically, and the different properties, that we can assign to it in Android.

If we want, our EditText to take only numbers. Then the code is,


edit.setInputType(InputType.TYPE_CLASS_NUMBER);


For password style implementation, for the editText, the code is,


edit.setTransformationMethod(new android.text.method.PasswordTransformationMethod()
.getInstance());


For putting, all letters in caps, the code


InputFilter[] filter = new InputFilter[1];
filter[0] = new InputFilter.AllCaps();
edit.setFilters(filter);


For restricting, only upto this much characters to enter in the editText, the code is,


InputFilter[] filter = new InputFilter[1];
filter[0] = new InputFilter.LengthFilter(15);
edit.setFilters(filter);


In this, I am restricting the characters upto 15 characters.

For moving the editText horizontally or vertically, the code is,


edit.post(new Runnablepublic void run() {
edit.offsetLeftAndRight(50);
edit.offsetTopAndBottom(100);
}
});

  • 감사합니다 2018.03.23 17:26

    setFilters와 InputFilter를 이용해서 editText를 제어하려고 하고 있었는데 어떻게 쓰는지를 몰라서 찾다가 들어왔습니다. 딱 저한테 필요한 포스트였어요. 잘 보고 갑니다!