在Android中限制EditText文本长度的最佳方法是什么?
有没有通过xml实现这一点的方法?
在Android中限制EditText文本长度的最佳方法是什么?
有没有通过xml实现这一点的方法?
当前回答
对于已经使用自定义输入筛选器并希望限制最大长度的用户,请注意:
当您在代码中分配输入过滤器时,所有先前设置的输入过滤器都将被清除,包括一个使用android:maxLength设置的过滤器。我在尝试使用自定义输入筛选器以防止在密码字段中使用某些不允许的字符时发现了这一点。使用setFilters设置过滤器后,不再观察到maxLength。解决方案是以编程方式将maxLength和自定义过滤器设置在一起。类似于:
myEditText.setFilters(new InputFilter[] {
new PasswordCharFilter(), new InputFilter.LengthFilter(20)
});
其他回答
由于goto10的观察,我将以下代码组合在一起,通过设置最大长度来防止丢失其他过滤器:
/**
* This sets the maximum length in characters of an EditText view. Since the
* max length must be done with a filter, this method gets the current
* filters. If there is already a length filter in the view, it will replace
* it, otherwise, it will add the max length filter preserving the other
*
* @param view
* @param length
*/
public static void setMaxLength(EditText view, int length) {
InputFilter curFilters[];
InputFilter.LengthFilter lengthFilter;
int idx;
lengthFilter = new InputFilter.LengthFilter(length);
curFilters = view.getFilters();
if (curFilters != null) {
for (idx = 0; idx < curFilters.length; idx++) {
if (curFilters[idx] instanceof InputFilter.LengthFilter) {
curFilters[idx] = lengthFilter;
return;
}
}
// since the length filter was not part of the list, but
// there are filters, then add the length filter
InputFilter newFilters[] = new InputFilter[curFilters.length + 1];
System.arraycopy(curFilters, 0, newFilters, 0, curFilters.length);
newFilters[curFilters.length] = lengthFilter;
view.setFilters(newFilters);
} else {
view.setFilters(new InputFilter[] { lengthFilter });
}
}
实现此目的的另一种方法是将以下定义添加到XML文件中:
<EditText
android:id="@+id/input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="number"
android:maxLength="6"
android:hint="@string/hint_gov"
android:layout_weight="1"/>
这将限制EditText小部件的最大长度为6个字符。
您可以在EditText中使用android:maxLength=“10”。(此处限制为最多10个字符)
我看到了很多好的解决方案,但我想给出一个我认为更完整、更人性化的解决方案:
1、限制长度。2、如果输入更多,则发出回调以触发吐司。3、光标可以在中间或尾部。4、用户可以通过粘贴字符串进行输入。5、始终丢弃溢出输入并保留原点。
public class LimitTextWatcher implements TextWatcher {
public interface IF_callback{
void callback(int left);
}
public IF_callback if_callback;
EditText editText;
int maxLength;
int cursorPositionLast;
String textLast;
boolean bypass;
public LimitTextWatcher(EditText editText, int maxLength, IF_callback if_callback) {
this.editText = editText;
this.maxLength = maxLength;
this.if_callback = if_callback;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if (bypass) {
bypass = false;
} else {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(s);
textLast = stringBuilder.toString();
this.cursorPositionLast = editText.getSelectionStart();
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.toString().length() > maxLength) {
int left = maxLength - s.toString().length();
bypass = true;
s.clear();
bypass = true;
s.append(textLast);
editText.setSelection(this.cursorPositionLast);
if (if_callback != null) {
if_callback.callback(left);
}
}
}
}
edit_text.addTextChangedListener(new LimitTextWatcher(edit_text, MAX_LENGTH, new LimitTextWatcher.IF_callback() {
@Override
public void callback(int left) {
if(left <= 0) {
Toast.makeText(MainActivity.this, "input is full", Toast.LENGTH_SHORT).show();
}
}
}));
我没能做到的是,如果用户高亮显示当前输入的一部分并试图粘贴一个很长的字符串,我不知道如何恢复高亮显示。
例如,最大长度设置为10,用户输入“12345678”,并将“345”标记为突出显示,然后尝试粘贴一个超过限制的字符串“0000”。
当我尝试使用edit_text.setSelection(start=2,end=4)恢复原点状态时,结果是,它只插入2个空格作为“12 345 678”,而不是原点高亮显示。我希望有人解决这个问题。
对于已经使用自定义输入筛选器并希望限制最大长度的用户,请注意:
当您在代码中分配输入过滤器时,所有先前设置的输入过滤器都将被清除,包括一个使用android:maxLength设置的过滤器。我在尝试使用自定义输入筛选器以防止在密码字段中使用某些不允许的字符时发现了这一点。使用setFilters设置过滤器后,不再观察到maxLength。解决方案是以编程方式将maxLength和自定义过滤器设置在一起。类似于:
myEditText.setFilters(new InputFilter[] {
new PasswordCharFilter(), new InputFilter.LengthFilter(20)
});