Android字段验证的实例代码
程序员文章站
2024-03-02 17:13:46
先给大家展示效果图:
package com.example.walkerlogin1;
import android.app.activity;
i...
先给大家展示效果图:
package com.example.walkerlogin1; import android.app.activity; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.toast; import com.throrinstudio.android.common.libs.validator.form; import com.throrinstudio.android.common.libs.validator.validate; import com.throrinstudio.android.common.libs.validator.validate.confirmvalidate; import com.throrinstudio.android.common.libs.validator.validate.ortworequiredvalidate; import com.throrinstudio.android.common.libs.validator.validator.emailvalidator; import com.throrinstudio.android.common.libs.validator.validator.notemptyvalidator; import com.throrinstudio.android.common.libs.validator.validator.phonevalidator; import com.throrinstudio.android.common.libs.validator.validator.urlvalidator; public class mainactivity extends activity { private edittext etaccount, etnick, etpassword, etmotto, etemail, etcity, etfoot,etheight,etweight,etexceptsteps; private button bt_ok; private form form; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initview(); setlistener(); validateform(); } //格式验证 private void validateform() { // 1. 先创建个表单form类用来装控件 form = new form(); // 非空验证 // 2. 然后创建validate类,将被验证控件传入 validate notemptyvalidate = new validate(etaccount); // 3. 将这个validate类addvalidator加入(如:非空类型notemptyverifior)类型验证类 notemptyvalidator notempty = new notemptyvalidator(this); notemptyvalidate.addvalidator(notempty); // 二选一 ortworequiredvalidate ortworequiredvalidate = new ortworequiredvalidate( etnick, etpassword); //密码验证 validate notpassword = new validate(etpassword); notemptyvalidator not2empty = new notemptyvalidator(this); notpassword.addvalidator(not2empty); //城市不能为空 validate etcity2 = new validate(etcity); notemptyvalidator etcity1 = new notemptyvalidator(this); etcity2.addvalidator(etcity1); //手机号不能为空 validate etmotto1 = new validate(etmotto); phonevalidator phonevalidator=new phonevalidator(this); etmotto1.addvalidator(phonevalidator); // 邮件验证 validate emailvalidate = new validate(etemail); emailvalidator emailvalidator = new emailvalidator(this); emailvalidator.setdomainname("qq\\.com");// 设置邮件规则:只能是qq邮箱 emailvalidate.addvalidator(emailvalidator); // 重复密码确认 // confirmvalidate confirmvalidate = new confirmvalidate(et_password1, // et_password2); // 网址 /*validate urlvalidate = new validate(et_url); urlvalidator urlvalidator = new urlvalidator(this); urlvalidate.addvalidator(urlvalidator);*/ // 4. form表单addvalidates这个validate类即可 form.addvalidates(notpassword); form.addvalidates(notemptyvalidate); form.addvalidates(ortworequiredvalidate); form.addvalidates(emailvalidate); form.addvalidates(etcity2); form.addvalidates(etmotto1); //form.addvalidates(confirmvalidate); // form.addvalidates(urlvalidate); } private void setlistener() { bt_ok.setonclicklistener(new onclicklistener() { public void onclick(view v) { // 5. 最后调用form.validate()验证即可,返回true表示验证通过。 boolean flag = form.validate(); if(flag){ toast.maketext(mainactivity.this, "验证成功!", toast.length_long).show(); }else{ toast.maketext(mainactivity.this, "验证失败", toast.length_long).show(); } } }); } private void initview() { etaccount = (edittext) findviewbyid(r.id.etaccount); etnick = (edittext) findviewbyid(r.id.etnick); etpassword = (edittext) findviewbyid(r.id.etpassword); etmotto = (edittext) findviewbyid(r.id.etmotto); etemail = (edittext) findviewbyid(r.id.etemail); etcity = (edittext) findviewbyid(r.id.etcity); etfoot = (edittext) findviewbyid(r.id.etfoot); etheight = (edittext) findviewbyid(r.id.etheight); etweight = (edittext) findviewbyid(r.id.etweight); etexceptsteps = (edittext) findviewbyid(r.id.etexceptsteps); bt_ok = (button) findviewbyid(r.id.btnclick); } } <scrollview xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/welcome_bg" android:orientation="vertical" > <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/welcome_bg" android:orientation="vertical" android:padding="10dp" tools:context=".registactivity" > <com.makeramen.roundedimageview.roundedimageview xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/roundimage_head" android:layout_width="80dp" android:layout_height="80dp" android:layout_gravity="center_horizontal" android:gravity="center_horizontal" android:onclick="changephoto" android:src="@drawable/test_photo" app:riv_border_color="#333333" app:riv_border_width="3dip" app:riv_corner_radius="10dip" app:riv_mutate_background="true" app:riv_oval="true" /> <edittext android:id="@+id/etaccount " android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="5dp" android:background="@android:drawable/edit_text" android:drawableleft="@drawable/etaccount" android:ems="10" android:hint="@string/etaccountrhint" > </edittext> <edittext android:id="@+id/etnick" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:drawable/edit_text" android:drawableleft="@drawable/etnick" android:ems="10" android:hint="@string/etnickhint" /> <edittext android:id="@+id/etpassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:drawable/edit_text" android:drawableleft="@drawable/etpassword" android:ems="10" android:inputtype="textpassword" android:hint="@string/etpassword" > </edittext> <edittext android:id="@+id/etmotto" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:drawable/edit_text" android:drawableleft="@drawable/etmotto" android:ems="10" android:hint="@string/etmotto" > </edittext> <edittext android:id="@+id/etemail" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:drawable/edit_text" android:drawableleft="@drawable/etemail" android:ems="10" android:hint="@string/etmail" android:inputtype="textemailaddress" > </edittext> <edittext android:id="@+id/etcity" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:drawable/edit_text" android:drawableleft="@drawable/etcity" android:ems="10" android:hint="@string/etcity" > </edittext> <view android:layout_width="match_parent" android:layout_height="2dp" android:background="@android:color/darker_gray" /> <edittext android:id="@+id/etfoot" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="5dp" android:background="@android:drawable/edit_text" android:drawableleft="@drawable/etfoot" android:ems="10" android:hint="@string/etstep" android:inputtype="number" > </edittext> <edittext android:id="@+id/etheight" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="5dp" android:background="@android:drawable/edit_text" android:drawableleft="@drawable/etheight" android:ems="10" android:hint="@string/etheight" android:inputtype="number" > </edittext> <edittext android:id="@+id/etweight" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="5dp" android:background="@android:drawable/edit_text" android:drawableleft="@drawable/etweight" android:ems="10" android:hint="@string/etweight" android:inputtype="number" > </edittext> <edittext android:id="@+id/etexceptsteps" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="5dp" android:background="@android:drawable/edit_text" android:drawableleft="@drawable/etexceptsteps" android:ems="10" android:hint="@string/etexceptsteps" android:inputtype="number" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="5dp" android:text="@string/etregistfinish" > </textview> <!-- <cn.edu.bztc.walkersimulate.util.reveallayout android:layout_width="match_parent" android:layout_height="wrap_content" > </cn.edu.bztc.walkersimulate.util.reveallayout> --> <button android:id="@+id/btnclick" android:layout_width="300dp" android:layout_height="55dp" android:layout_margintop="5dp" android:background="@drawable/btn_select" android:gravity="center" android:text="@string/etcity" > </button> </linearlayout> </scrollview>
另外还需导入一个类库android-validator-master
以上内容是小编给大家介绍的android字段验证的实例代码,希望对大家有所帮助,如果大家想了解更多资讯敬请关注网站!