Article directory
-
- 1. Remove the border at the bottom of EditText
- 2. EditText gets focus
- 3. Set the cursor at the end of the text
- 4. How to remove the scroll bar in EditText
- 5. EditText is nested under ScrollView, how to prevent the scrolling of EditText from being hijacked
- 6. EditText string length monitoring
- 7. EditText string length limit
1. Remove the border at the bottom of EditText
android:background="@null"
2. EditText gets focus
getBinding().editText1.setFocusable(true);
getBinding().editText1.setFocusableInTouchMode(true);
getBinding().editText1.requestFocus();
3. Set the cursor at the end of the text
In this case, the text is set directly through the code, and the cursor does not move to the end.
// 获取输入的文本内容
String content = getBinding().editText2.getText().toString();
// 获取输入文本字符串的长度
int length = content.length();
// 将光标移到最后
getBinding().editText2.setSelection(length);
4. How to remove the scroll bar in EditText
android:scrollbars="none"
5. EditText is nested under ScrollView, how to prevent the scrolling of EditText from being hijacked
First look at the layout (example), note that only one layout can be included in ScrollView
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/picture1"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/picture2"/>
</LinearLayout>
</ScrollView>
Then look at the sliding effect of EditText. Obviously, the sliding of EditText is hijacked by ScrollView, and only the sliding of ScrollView is executed.
Solution:
getBinding().editText2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
boolean isLastLineReached = !v.canScrollVertically(1); // 是否滑动到最后一行
boolean isTopLineReached = !v.canScrollVertically(-1); // 是否滑动到首行
if ((isLastLineReached && event.getAction() == MotionEvent.ACTION_DOWN)
|| (isTopLineReached && event.getAction() == MotionEvent.ACTION_UP)) {
v.getParent().requestDisallowInterceptTouchEvent(false); // 放弃事件
} else {
v.getParent().requestDisallowInterceptTouchEvent(true); // 禁止 ScrollView 处理事件
}
return false;
}
});
6. EditText string length monitoring
getBinding().editText2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
// 更新 TextView 显示的字符计数
getBinding().tvCount.setText(String.format("输入框内有:%d个字符", charSequence.length()));
}
@Override
public void afterTextChanged(Editable s) {
}
});
7. EditText string length limit
// 限制最大输入为10个字符
getBinding().editText2.setFilters(new InputFilter[]{
new MaxTextLengthFilter(10)});
public static class MaxTextLengthFilter implements InputFilter {
private final int mMaxLength;
// 构造方法中传入最多能输入的字数
public MaxTextLengthFilter(int max) {
mMaxLength = max;
}
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
int keep = mMaxLength - (dest.length() - (dend - dstart));
if (keep < (end - start)) {
ToastManager.showShort("不能超过" + mMaxLength + "个字符");
}
if (keep <= 0) {
return "";
} else if (keep >= end - start) {
return null;
} else {
return source.subSequence(start, start + keep);
}
}
}