Android uses regular expressions to control edittext can only enter numbers, English, Chinese characters

Judged by regular expressions. The following example only allows letters, numbers and Chinese characters to be displayed.

public static String stringFilter(String str)throws PatternSyntaxException{      
      // Only letters, numbers and Chinese characters are allowed       
      String    regEx   =   "[^a-zA-Z0-9\u4E00-\u9FA5]";                      
      Pattern    p    =    Pattern.compile(regEx) ;      
      Matcher    m    =    p. matcher(str);      
      return    m. replaceAll(""). trim();      
  }

//Click event calls the above method

tv_other.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    nicheng = ed_xiugainicheng.getText().toString();
        String str = stringFilter(nicheng.toString());
            if(!nicheng.equals(str)){
           Toast.makeText(WoXiuGaiNiChengActivity.this, "不能输入非法字符!" , Toast.LENGTH_SHORT).show();
    }

}

Guess you like

Origin blog.csdn.net/as425017946/article/details/49204989