当前位置:网站首页>Bug- solve the display length limitation of log distinguished character encoding (edittext+lengthfilter)
Bug- solve the display length limitation of log distinguished character encoding (edittext+lengthfilter)
2022-06-25 09:54:00 【TowerOs】
BUG Fix Log 001 EditText Distinguish the display length limit of character encoding
EditText Distinguish the display length limit of character encoding
This article uses : adopt EditText.setFilter() To put the expanded Filter( filter ) Customize related functions
First, let's take the native implementation as an example
// TextView(EditView)#setFilters
public void setFilters(InputFilter[] filters) {
if (filters == null) {
throw new IllegalArgumentException();
}
mFilters = filters;
if (mText instanceof Editable) {
setFilters((Editable) mText, filters);
}
}
private InputFilter[] mFilters = NO_FILTERS;
private static final InputFilter[] NO_FILTERS = new InputFilter[0];
You can see in the EditText in ,xml attribute maxLens The function resolves to In code LengthFilter Realized
if (maxlength >= 0) {
setFilters(new InputFilter[] {
new InputFilter.LengthFilter(maxlength) });
} else {
setFilters(NO_FILTERS);
}
// LengthFilter yes InputFilter The only implemented internal static class
public static class LengthFilter implements InputFilter {
@UnsupportedAppUsage
private final int mMax;
public LengthFilter(int max) {
mMax = max;
}
// The important filtering logic is here
public CharSequence filter(CharSequence source, int start, int end, Spanned dest,
int dstart, int dend) {
int keep = mMax - (dest.length() - (dend - dstart));
if (keep <= 0) {
return "";
} else if (keep >= end - start) {
return null; // keep original
} else {
keep += start;
if (Character.isHighSurrogate(source.charAt(keep - 1))) {
--keep;
if (keep == start) {
return "";
}
}
return source.subSequence(start, keep);
}
}
/** * @return the maximum length enforced by this input filter */
public int getMax() {
return mMax;
}
}
We can go straight to LengthFilter Transform the packaging to achieve its own functions
public static class DisEngAndChLengthFilter extents InputFilter.LengthFilter {
@override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest,
int dstart, int dend) {
CharSequence content = dest;
int parseLen = 0;
int ctnLen = content.length();
int size = content.length();
if(size > 0){
while(ctnLen > 0){
char tmp = content.charAt(size-ctnLen);
if(tmp >= 0x4E00 && tmp <= 0x9FA5){
parseLen+=2;
}else {
parseLen+=1;
}
ctnLen--;
}
}
//mMax = parseLen;
LogUtil.d(TAG+"sourceLen"+source.length()+"source"+source+"start:"+start+"end:"+end+"dest:"+dest+"dstart:"+dstart+"dend:"+dend);
int keep = mMax - (parseLen - (dend - dstart));
if (keep <= 0) {
return "";
} else if (keep >= end - start) {
return null; // keep original
} else {
keep += start;
if (Character.isHighSurrogate(source.charAt(keep - 1))) {
--keep;
if (keep == start) {
return "";
}
}
return source.subSequence(start, keep);
}
}
}
thus , Can achieve , Or customize your own functions
边栏推荐
- The gradle configuration supports the upgrade of 64 bit architecture of Xiaomi, oppo, vivo and other app stores
- What functions should smart agriculture applet system design have
- 字符串 最长公共前缀
- 富时A50开户什么地方安全
- Title B of the certification cup of the pistar cluster in the Ibagu catalog
- clang frontend command failed with exit code 250
- Minio基本使用与原理
- STM32 receives data by using idle interrupt of serial port
- Solution to the problem of repeated startup of esp8266
- MongoDB的原理、基本使用、集群和分片集群
猜你喜欢

Remove the mosaic, there's a way, attached with the running tutorial

Methodchannel of flutter

Jetpack compose layout (IV) - constraintlayout

瑞吉外卖项目(二)

x86电脑上下载debian的arm64的包

Rxjs TakeUntil 操作符的学习笔记

汇付国际为跨境电商赋能:做合规的跨境支付平台!

Online notes on Mathematics for postgraduate entrance examination (8): Kego equations, eigenvalues and eigenvectors, similarity matrix, quadratic series courses
![[zufe expense reimbursement] zhecai invoice reimbursement specification (taking Xinmiao reimbursement as an example), which can be passed in one trip at most](/img/28/c5c6b6d03b459745dc3735f8b39ea9.jpg)
[zufe expense reimbursement] zhecai invoice reimbursement specification (taking Xinmiao reimbursement as an example), which can be passed in one trip at most

Applet cloud development joint table data query and application in cloud function
随机推荐
What should be paid attention to in PMP examination?
Arduino bootloader burning summary
Armbian version name comparison
CYCA少儿形体礼仪 乐清市培训成果考核圆满落幕
The way that flutter makes the keyboard disappear (forwarding from the dependent window)
[matlab] image binarization (imbinarize function)
Grabcut image segmentation in opencv
[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate
Study on correlation of pumpkin price and design of price prediction model based on BP neural network
纳米数据世界杯数据接口,中超数据,体育数据比分,世界杯赛程api,足球比赛实时数据接口
Jetpack compose layout (IV) - constraintlayout
Get started quickly with jetpack compose Technology
Opencv中的GrabCut图像分割
Creating a binary tree (binary linked list) from a generalized table
[Ruby on rails full stack course] course directory
JS tool function, self encapsulating a throttling function
vscode试图过程写入管道不存在
[IOU] intersection over union
Applet cloud development joint table data query and application in cloud function
Ruiji takeout project (II)