遇到判断EditText中文本,是否为制定格式
EditText et;
Button btn; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); et = (EditText) this.findViewById(R.id.editText1); btn = (Button) this.findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) { Pattern pattern = Pattern
.compile("^[\u4e00-\u9fa5]*$|^[a-zA-Z]*$");
Matcher matcher = pattern.matcher(et.getText()); if (et.getText() == null || et.getText().length() == 0) {
et.setError("不能为空");
} else if (matcher.matches()) {
Toast.makeText(getApplicationContext(), "匹配",
Toast.LENGTH_SHORT).show();
} else {
et.setError("不匹配");
} }
}); }
-----------------判断文本是否为纯字母或纯汉字-------------------
是否包含数字用[0-9]+?
----------------------------------------------------------------