android 字元長度限制,中文2個字元 ,英文1個字元
阿新 • • 發佈:2019-01-08
比如最大10個字元,若超過10個,則顯示9或者8個字元加省略號.
啊啊啊啊啊
哎哎哎啊...
哎哎哎啊a...
aaaaaaaa...
public class CalTextLength {
public static String handleText(String str, int maxLen) { if (TextUtils.isEmpty(str)) { return str; } int count = 0; int endIndex=0; for (int i = 0; i < str.length(); i++) { charitem = str.charAt(i); if (item < 128) { count = count + 1; } else { count = count + 2; } if(maxLen==count || (item>=128 && maxLen+1==count)){ endIndex=i; } } if (count <= maxLen) { return str; } else { returnstr.substring(0, endIndex) + "..."; } }
}
private static final int maxLen=12; //名字最大字元數tring name=CalTextLength.handleText(simpleContact.getName(),maxLen);holder.tv_name.setText(name);