/**
- editext 常用相关工具
*/
public class EditUtil {
// 设置只能输入汉字
public static void setInPutNameFilter(EditText et,int length){
InputFilter filter = new InputFilter() {
@Override
public CharSequence filter(CharSequence charSequence, int i, int i1, Spanned spanned, int i2, int i3) {
String speChat="[^\u4E00-\u9FA5]";
Pattern pattern = Pattern.compile(speChat);
Matcher matcher = pattern.matcher(charSequence.toString());
if(!matcher.find()){
return null;
}else {
return "";
}
}
};
et.setFilters(new InputFilter[]{new InputFilter.LengthFilter(length),filter});
}
/**
* 禁止EditText输入特殊字符和表情、空格、最大长度
* @param editText
* @param maxLength 输入框最大输入字符数
*/
public static void setEditTextInhibitInputSpeChat(EditText editText, int maxLength){
InputFilter filter=new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
String speChat="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]|[\\ud83c\\udc00-\\ud83c\\udfff]|[\\ud83d\\udc00-\\ud83d\\udfff]|[\\u2600-\\u27ff]";
Pattern pattern = Pattern.compile(speChat);
Matcher matcher = pattern.matcher(source.toString());
if(matcher.find() || source.equals(" ")){
return "";
}else {
return null;
}
}
};
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength), filter});
}
//原本输入的小写字母转喂大写字母
public static class TransInformation extends ReplacementTransformationMethod {
/**
* 原本输入的小写字母
*/
@Override
protected char[] getOriginal() {
return new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
}
/**
* 替代为大写字母
*/
@Override
protected char[] getReplacement() {
return new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
}
}
}
/**
- 限制输入框输入小数
- 默认第一位输入小数点时,转换为0.
- 如果起始位置为0,且第二位跟的不是".",则无法后续输入
*/
public class EditDoubleWather implements TextWatcher{
private EditText editText;
private int length = 0; // 小点长度
public EditDoubleWather(EditText et) {
editText = et;
}
public EditDoubleWather setLength(int d) {
length = d;
return this;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String trim = s.toString().trim();
if (!TextUtils.isEmpty(trim)) {
if (trim.contains(".")) {
String[] split = trim.split("\\.");
if (split.length > 1) {
if (StringUtil.StrCheck(split[1]) && split[1].length() > length) {
String d;
if (!StringUtil.StrCheck(split[0])) {
d = "0." + split[1].substring(0, length);
}else {
d = split[0] + "." + split[1].substring(0, length);
}
editText.setText(d);
editText.setSelection(d.length());
}
}
}
}
}
@Override
public void afterTextChanged(Editable editable) {
}
}
/**
* double 转换
* @param d 字符串
* @param lenght 保留小数长度
* @return
*/
public static double saveDouble(String d,int lenght){
if (d.substring(0,1).equals(".")) {
d = "0"+d;
}
BigDecimal bigDecimal = new BigDecimal(d);
return bigDecimal.setScale(lenght,BigDecimal.ROUND_HALF_UP).doubleValue();
}
网友评论