반응형
Android에서 EditText의 최대 문자 수를 어떻게 설정합니까?
Android EditText 입력에 대한 최대 문자 수를 어떻게 설정합니까? setMaxLines, setMaxEMS가 표시되지만 문자 수에는 영향이 없습니다.
XML EditText
View
에서 140을 최대 문자 수인 곳에이 행을 추가 할 수 있습니다 .
android:maxLength="140"
동적으로 :
editText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(MAX_NUM) });
xml을 통해 :
<EditText
android:maxLength="@integer/max_edittext_length"
InputFilter를 사용할 수 있습니다.
EditText myEditText = (EditText) findViewById(R.id.editText1);
InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter.LengthFilter(10); //Filter to 10 characters
myEditText .setFilters(filters);
도움이 될 수 있습니다.
- http://www.tutorialforandroid.com/2009/02/maxlength-in-edittext-using-codes.html
- http://www.anddev.org/edit_text_max_limit_charactars-t1925.html
나는 항상 다음과 같이 최대 값을 설정했습니다.
<EditText
android:id="@+id/edit_blaze_it
android:layout_width="0dp"
android:layout_height="@dimen/too_high"
<!-- This is the line you need to write to set the max-->
android:maxLength="420"
/>
나를 위해 일하고 있습니다.
etMsgDescription.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maximum_character)});
etMsgDescription.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
tvCharacterLimite.setText(" "+String.valueOf(maximum_character - etMsgDescription.getText().length()));
}
@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
}
});
그것은 이런 식으로 저에게 효과적이었습니다. 내가 찾은 최고입니다. 최대 길이는 200 자입니다.
editObservations.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (editObservations.getText().length() >= 201){
String str = editObservations.getText().toString().substring(0, 200);
editObservations.setText(str);
editObservations.setSelection(str.length());
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
<EditText
android:id="@+id/edtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter device name"
android:maxLength="10"
android:inputType="textFilter"
android:singleLine="true"/>
InputType은 "textFilter"를 설정해야합니다
android:inputType="textFilter"
이 기능을 어디서나 쉽게 사용할 수 있습니다 :
public static void setEditTextMaxLength(EditText editText, int length) {
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(length);
editText.setFilters(FilterArray);
}
반응형
'IT박스' 카테고리의 다른 글
URL에 대한 PHP 유효성 검사 / 정규식 (0) | 2020.07.16 |
---|---|
AngularJS로 배열을 필터링하고 필터링 된 객체의 속성을 ng-model 속성으로 사용하려면 어떻게합니까? (0) | 2020.07.16 |
jQuery는 키를 누른 후 입력 값을 얻습니다. (0) | 2020.07.16 |
안드로이드의 RecyclerView에 간단한 8dp 헤더 / 바닥 글을 추가하는 방법은 무엇입니까? (0) | 2020.07.16 |
해시 값을 변경하는 방법? (0) | 2020.07.16 |