Android 필드 에서 검 증 된 인 스 턴 스 코드

11522 단어 android검증 하 다.
먼저 효과 도 를 보 여 드 리 겠 습 니 다.

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 도 가 져 와 야 합 니 다.
이상 의 내용 은 소 편 이 소개 한 안 드 로 이 드 필드 에서 검 증 된 인 스 턴 스 코드 입 니 다.여러분 께 도움 이 되 기 를 바 랍 니 다.더 많은 정 보 를 알 고 싶 으 시 면 저희 사 이 트 를 주목 하 세 요!

좋은 웹페이지 즐겨찾기