edt_zit.setSelection(edt_content.length());
/**
* 现在是记录上一次显示的字符串,设置为当前不符合法输入的字符串内容
* */
import_name.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
checkEdittextContent();
}
@Override
public void afterTextChanged(Editable s) {
String str=import_name.getText().toString();
Log.e("tag", "onEditorAction: "+str );
if (!TextStyleUtils.isLegal(str)){
showToast("节点名称不能为特殊符号");
if (str.length()>1) {
// str=str.substring(0, str.length() - 1);
import_name.setText(edtNameLast);
import_name.setSelection(edtNameLast.length());
}else {
import_name.setText("");
edtNameLast=import_name.getText().toString();
}
}
if (str.length()>6){
showToast("节点名称为1-6位");
import_name.setText(edtNameLast);
import_name.setSelection(str.length());
}
edtNameLast=import_name.getText().toString();
}
});
//true为含有表情
private static boolean noContainsEmoji(String str) {
int len = str.length();
for (int i = 0; i < len; i++) {
if (isEmojiCharacter(str.charAt(i))) {
return true;
}
}
return false;
}
private static boolean isEmojiCharacter(char codePoint) {
return !((codePoint == 0x0) ||
(codePoint == 0x9) ||
(codePoint == 0xA) ||
(codePoint == 0xD) ||
((codePoint >= 0x20) && (codePoint <= 0xD7FF)) ||
((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) ||
((codePoint >= 0x10000) && (codePoint <= 0x10FFFF)));
}
/**
* 判断是否有符号、空格
* 合法则true,不合法false
* */
public static boolean isLegal(String name){
if (TextStyleUtils.noContainsEmoji(name)){
return false;
}
String str="~ !@#$%^&*()_+,./;'[],。、;‘《》?:“|{}<>?:|";
for (int i=0;i<name.length();i++){
if (str.contains(name.substring(i,i+1))){
return false;
}
}
return true;
}
/**
* true为含表情
* */
private static boolean noContainsEmoji(String str) {//真为含有表情
int len = str.length();
for (int i = 0; i < len; i++) {
if (isEmojiCharacter(str.charAt(i))) {
return true;
}
}
return false;
}
private static boolean isEmojiCharacter(char codePoint) {
return !((codePoint == 0x0) ||
(codePoint == 0x9) ||
(codePoint == 0xA) ||
(codePoint == 0xD) ||
((codePoint >= 0x20) && (codePoint <= 0xD7FF)) ||
((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) ||
((codePoint >= 0x10000) && (codePoint <= 0x10FFFF)));
}
Android edittext字符串提醒处理,及是否包含表情包及标点符号,设置edit光标位置
最新推荐文章于 2022-06-24 11:42:27 发布