如何在 Android TextView 中以编程方式设置 maxLength?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2461824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to programmatically set maxLength in Android TextView?
提问by UMAR
I would like to programmatically set maxLength
property of TextView
as I don't want to hard code it in the layout. I can't see any set
method related to maxLength
.
我想以编程方式设置maxLength
属性,TextView
因为我不想在布局中对其进行硬编码。我看不到任何set
与maxLength
.
Can anyone guide me how to achieve this?
谁能指导我如何实现这一目标?
回答by Sephy
Should be something like that. but never used it for textview, only edittext :
应该是这样的。但从未将其用于 textview,仅用于 edittext :
TextView tv = new TextView(this);
int maxLength = 10;
InputFilter[] fArray = new InputFilter[1];
fArray[0] = new InputFilter.LengthFilter(maxLength);
tv.setFilters(fArray);
回答by IntelliJ Amiya
Try this
尝试这个
int maxLengthofEditText = 4;
editText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLengthofEditText)});
回答by Farid Ahmed
Easy way limit edit text character:
简单的方法限制编辑文本字符:
EditText ed=(EditText)findViewById(R.id.edittxt);
ed.setFilters(new InputFilter[]{new InputFilter.LengthFilter(15)});
回答by Kevin
For those of you using Kotlin
对于那些使用 Kotlin 的人
fun EditText.limitLength(maxLength: Int) {
filters = arrayOf(InputFilter.LengthFilter(maxLength))
}
Then you can just use a simple editText.limitLength(10)
然后你可以只使用一个简单的 editText.limitLength(10)
回答by santalu
For Kotlin and without resetting previous filters:
对于 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))
回答by CoolMind
As Jo?o Carlossaid, in Kotlin use:
正如Jo?o Carlos所说,在 Kotlin 中使用:
editText.filters += InputFilter.LengthFilter(10)
See also https://stackoverflow.com/a/58372842/2914140about some devices strange behaviour.
另请参阅https://stackoverflow.com/a/58372842/2914140关于某些设备的奇怪行为。
(Add android:inputType="textNoSuggestions"
to your EditText
.)
(添加android:inputType="textNoSuggestions"
到您的EditText
.)
回答by Kyriakos Georgiopoulos
I made a simple extension function for this one
我为此做了一个简单的扩展功能
/**
* 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)
回答by hanswim
To keep the original input filter, you can do it this way:
要保留原始输入过滤器,您可以这样做:
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);
回答by Sai Gopi N
best solution i found
我找到的最佳解决方案
textView.setText(text.substring(0,10));
回答by Null Pointer Exception
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);