Android表单验证:AwesomeValidation
y37f
10年前
只要三个步骤就能够轻松实现复杂繁琐的表单验证。 示例代码
Steps
- Declare validation style;
- Add validations;
- Set a point when to trigger validation.
示例代码 // Step 1: designate a style AwesomeValidation mAwesomeValidation = new AwesomeValidation(BASIC); // or AwesomeValidation mAwesomeValidation = new AwesomeValidation(COLORATION); mAwesomeValidation.setColor(Color.YELLOW); // optional, default color is RED if not set // or AwesomeValidation mAwesomeValidation = new AwesomeValidation(UNDERLABEL); mAwesomeValidation.setContext(this); // mandatory for UNDERLABEL style // Step 2: add validations // support regex string or Guava#Range // you can pass resource or string mAwesomeValidation.addValidation(activity, R.id.edt_name, "[a-zA-Z\\s]+", R.string.err_name); mAwesomeValidation.addValidation(activity, R.id.edt_tel, RegexTemplate.TELEPHONE, R.string.err_tel); mAwesomeValidation.addValidation(activity, R.id.edt_year, Range.closed(1900, Calendar.getInstance().get(Calendar.YEAR)), R.string.err_year); mAwesomeValidation.addValidation(activity, R.id.edt_height, Range.closed(0.0f, 2.72f), R.string.err_height); // or mAwesomeValidation.addValidation(editText, "regex", "Error info"); // Step 3: set a trigger findViewById(R.id.btn_done).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mAwesomeValidation.validate(); } }); // Optional: remove validation failure information findViewById(R.id.btn_clr).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mAwesomeValidation.clear(); } });
截图
项目主页:http://www.open-open.com/lib/view/home/1426474150632