EditText使用笔记

1、替换光标

 android:textCursorDrawable="@drawable/shape_cusor"
 xml文件:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <size android:width="1dp" />
    <solid android:color="@color/civ_designer"  />
</shape>

2、显示或隐藏EditText的hint文字说明

mEtDetail.setOnFocusChangeListener(new View.OnFocusChangeListener() {
          @Override
           public void onFocusChange(View view, boolean hasFocus) {
               //显示或隐藏EditText的hint文字说明
               changeHint((EditText) view, hasFocus);
           }
     });

    private void changeHint(EditText view, boolean hasFocus) {
        EditText textView = view;
        String hint;
        if (hasFocus) {  //隐藏输入提示文字
            hint = textView.getHint().toString();
            textView.setTag(hint);
            textView.setHint("");
            } else {   //显示输入提示文字
            hint = textView.getTag().toString();
            textView.setHint(hint);
        }
    }

3、限制输入框的输入字数

mEtDetail.addTextChangedListener(new TextWatcher() {
long time;
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void afterTextChanged(Editable editable) {
 //限制需求描述输入字数
 String trim = mEtDetail.getText().toString().trim();
 if(trim!=null&&trim.length()>600){
     String str=trim.substring(0,600);
     mEtDetail.setText(str);
     mEtDetail.setSelection(str.length());
     long l = System.currentTimeMillis();
     if(l-time>10000){  //超过十秒才能再次提示
         ToastUtil.show("输入字数不能超过600字");
         time=l;
     }
 }
}
});

4、判断字符串是否为纯数字

public  boolean isNum(String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        return isNum.matches();
    }

5、判断手机号码是否正确

/**
   * 大陆号码或香港号码均可
   */
  public static boolean isPhoneLegal(String str)throws PatternSyntaxException {
      return isChinaPhoneLegal(str) || isHKPhoneLegal(str);
  }

  /**
   * 大陆手机号码11位数,匹配格式:前三位固定格式+后8位任意数
   * 此方法中前三位格式有:
   * 13+任意数
   * 15+除4的任意数
   * 18+除1和4的任意数
   * 17+除9的任意数
   * 147
   */
  public static boolean isChinaPhoneLegal(String str) throws PatternSyntaxException {
      String regExp = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))\\d{8}$";
      Pattern p = Pattern.compile(regExp);
      Matcher m = p.matcher(str);
      return m.matches();
  }

  /**
   * 香港手机号码8位数,5|6|8|9开头+7位任意数
   */
  public static boolean isHKPhoneLegal(String str)throws PatternSyntaxException {
      String regExp = "^(5|6|8|9)\\d{7}$";
      Pattern p = Pattern.compile(regExp);
      Matcher m = p.matcher(str);
      return m.matches();
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值