Instance code for Android field validation _android

Source: Internet
Author: User
Tags xmlns

First to show you the effect of the picture:

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 (); 
}//Format verify private void Validateform () {//1. First create form form class to load control form = new Form (); Non-null authentication//2. 
The Validate class is then created, and the validated control is passed into the Validate notemptyvalidate = new Validate (etaccount); 3. 
Add this validate class Addvalidator (for example: Non-null type Notemptyverifior) type validation class Notemptyvalidator Notempty = new Notemptyvalidator (this); 
Notemptyvalidate.addvalidator (Notempty); 
Two selected one ortworequiredvalidate ortworequiredvalidate = new Ortworequiredvalidate (Etnick, Etpassword); 
Password verification Validate Notpassword = new Validate (Etpassword); 
Notemptyvalidator not2empty = new Notemptyvalidator (this); 
Notpassword.addvalidator (Not2empty); 
The city cannot be empty Validate EtCity2 = new Validate (etcity); 
Notemptyvalidator etCity1 = new Notemptyvalidator (this); etcity2.aDdvalidator (etCity1); 
Cell phone number cannot be empty Validate etMotto1 = new Validate (Etmotto); 
Phonevalidator phonevalidator=new Phonevalidator (this); 
Etmotto1.addvalidator (Phonevalidator); 
Message verification Validate emailvalidate = new Validate (etemail); 
Emailvalidator emailvalidator = new Emailvalidator (this); 
Emailvalidator.setdomainname ("qq\\.com"),//Set up the mail rule: only QQ mailbox Emailvalidate.addvalidator (Emailvalidator); 
Duplicate password confirmation//confirmvalidate confirmvalidate = new Confirmvalidate (et_password1,//ET_PASSWORD2); 
URL/*validate urlvalidate = new Validate (Et_url); 
Urlvalidator urlvalidator = new Urlvalidator (this); Urlvalidate.addvalidator (urlvalidator); *//4. 
Form form Addvalidates This validate class can be 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. The Last Call to Form.validate () validation, returns True 
Represents a validation pass. 
Boolean flag = Form.validate (); if (flag) {Toast.maketext (mainactivity.this, verify successful!) 
", Toast.length_long). Show (); 
}else{Toast.maketext (Mainactivity.this, "validation failed", 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" @ 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:d rawable/edit_text" android:drawableleft= "@drawable/etaccount" android:ems= "" android:hint= "@string Etaccountrhint "> </EditText> <edittext android:id=" @+id/etnick "android:layout_width=" Match_parent "and roid:layout_height= "Wrap_content" android:background= "@android:d rawable/edit_text" android:drawableleft= "@ 
Drawable/etnick "android:ems=" android:hint= "@string/etnickhint"/> <edittext android:id= "@+id/etPassword" Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:background= "@android:d rawable/ Edit_text "android:drawableleft=" @drawable/etpassword "android:ems=" android:inputtype= "Textpassword" Android: hint= "@string/etpassword" > </EditText> <edittext android:id= "@+id/etmotto" match _parent "android:layout_height=" Wrap_content "android:background="@android:d rawable/edit_text "android:drawableleft=" @drawable/etmotto "android:ems=" "android:hint=" @string Etmotto "> </EditText> <edittext android:id=" @+id/etemail "android:layout_width=" Match_parent "Android:l" ayout_height= "Wrap_content" android:background= "@android:d rawable/edit_text" android:drawableleft= "@drawable 
Etemail "android:ems=" 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:d rawable/edit_text" android:drawableleft= "@drawable/etcity" android:ems= "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:d rawable/edit_text" android:drawableleft= "@drawable/etfoot" Android:ems= "Ten" android:hint= "@string/etstep" android:inputtype= "number" > </EditText> <edittext Androi D:id= "@+id/etheight" android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android:layout_ 
margintop= "5DP" android:background= "@android:d rawable/edit_text" android:drawableleft= "@drawable/etheight" Android:ems= "Ten" android:hint= "@string/etheight" android:inputtype= "number" > </EditText> <edittext Andr Oid:id= "@+id/etweight" android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android:layout_ 
margintop= "5DP" android:background= "@android:d rawable/edit_text" android:drawableleft= "@drawable/etweight" Android:ems= "Ten" android:hint= "@string/etweight" android:inputtype= "number" > </EditText> <edittext Andr Oid:id= "@+id/etexceptsteps" android:layout_width= "Match_parent" Android:layout_height= "Wrap_content" android:layout_margintop= "5DP" android:background= "@android:d rawable/edit_text" Android: drawableleft= "@drawable/etexceptsteps" android:ems= "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& 
Gt  </ScrollView>

In addition, you need to import a class library Android-validator-master

The above content is small to introduce the Android field Verification example code, I hope to help you, if you want to learn more information please pay attention to cloud Habitat community website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.