반응형
Android TextView에서 maxLength를 프로그래밍 방식으로 설정하는 방법은 무엇입니까?
프로그래밍 방식으로 설정합니다.maxLength의 재산.TextView레이아웃에 하드 코딩을 하고 싶지 않기 때문입니다.하나도 안 보여요set관련된 방법maxLength.
이것을 달성하는 방법을 누가 안내해 줄 수 있습니까?
텍스트 보기에 사용하지 않고 텍스트만 편집합니다.
TextView tv = new TextView(this);
int maxLength = 10;
InputFilter[] fArray = new InputFilter[1];
fArray[0] = new InputFilter.LengthFilter(maxLength);
tv.setFilters(fArray);
사용해 보세요.
int maxLengthofEditText = 4;
editText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLengthofEditText)});
코틀린을 사용하시는 분들을 위해
fun EditText.limitLength(maxLength: Int) {
filters = arrayOf(InputFilter.LengthFilter(maxLength))
}
그런 다음 단순한 editText.limitLength(10)를 사용하면 됩니다.
쉬운 방법 제한 편집 텍스트 문자:
EditText ed=(EditText)findViewById(R.id.edittxt);
ed.setFilters(new InputFilter[]{new InputFilter.LengthFilter(15)});
주앙 카를로스가 말했듯이 코틀린어로 다음을 사용합니다.
editText.filters += InputFilter.LengthFilter(10)
일부 장치의 이상한 동작에 대해서는 https://stackoverflow.com/a/58372842/2914140 도 참조하십시오.
(추가)android:inputType="textNoSuggestions"당신에게EditText.)
Kotlin의 경우 및 이전 필터를 재설정하지 않은 경우:
fun TextView.addFilter(filter: InputFilter) {
filters = if (filters.isNullOrEmpty()) {
arrayOf(filter)
} else {
filters.toMutableList()
.apply {
removeAll { it.javaClass == filter.javaClass }
add(filter)
}
.toTypedArray()
}
}
textView.addFilter(InputFilter.LengthFilter(10))
나는 이것을 위해 간단한 확장 기능을 만들었습니다.
/**
* maxLength extension function makes a filter that
* will constrain edits not to make the length of the text
* greater than the specified length.
*
* @param max
*/
fun EditText.maxLength(max: Int){
this.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(max))
}
editText?.maxLength(10)
SWIFT 5를 위한 솔루션
editText.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(123))
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
//for Limit...
input.setFilters(new InputFilter[] {new InputFilter.LengthFilter(3)});
builder.setView(input);
내가 찾은 최고의 해결책
textView.setText(text.substring(0,10));
원래 입력 필터를 유지하려면 다음 방법을 사용합니다.
InputFilter.LengthFilter maxLengthFilter = new InputFilter.LengthFilter(100);
InputFilter[] origin = contentEt.getFilters();
InputFilter[] newFilters;
if (origin != null && origin.length > 0) {
newFilters = new InputFilter[origin.length + 1];
System.arraycopy(origin, 0, newFilters, 0, origin.length);
newFilters[origin.length] = maxLengthFilter;
} else {
newFilters = new InputFilter[]{maxLengthFilter};
}
contentEt.setFilters(newFilters);
언급URL : https://stackoverflow.com/questions/2461824/how-to-programmatically-set-maxlength-in-android-textview
반응형
'programing' 카테고리의 다른 글
| 어쨌든 VLA의 의미는 무엇입니까? (0) | 2023.08.15 |
|---|---|
| 도커에서 여러 터미널을 여는 방법은 무엇입니까? (0) | 2023.08.15 |
| 파일 수 x개 유지 및 다른 모든 파일 삭제 - Powershell (0) | 2023.08.15 |
| 다른 사용자가 설치한 앱을 제거하는 방법은 무엇입니까? (0) | 2023.08.15 |
| 배열을 코드로 인쇄 (0) | 2023.08.15 |