EditText使用笔记

来源:互联网 发布:怎么更改淘宝店名 编辑:程序博客网 时间:2024/05/19 07:07

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;@Overridepublic void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}@Overridepublic void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}@Overridepublic 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
原创粉丝点击