目标效果:
点击头像,可打开手机图库进行图片选择,并将选中的图片显示为新头像,点击注册按钮,可进行非空个格式验证。
项目压缩包:http://pan.baidu.com/s/1i5cvLR3 ,项目包中有三个文件,Login为注册原项目,roundedimageview为生成圆角图片的框架,Android-Validator-master为输入校验的框架。另外,校验功能学习自http://www.cnblogs.com/huihui920823/p/4814643.html ,换头像功能学习自http://blog.csdn.net/feng88724/article/details/6170955 。
1.按照上一篇博文导入框架。
2.在RegisterActivity.java页面进行头像的点击事件处理和注册按钮的点击事件处理。
RegisterActivity.java页面:
package com.example.login; import java.io.FileNotFoundException; import com.makeramen.roundedimageview.RoundedImageView; import com.throrinstudio.android.common.libs.validator.Form; import com.throrinstudio.android.common.libs.validator.Validate; import com.throrinstudio.android.common.libs.validator.validator.EmailValidator; import com.throrinstudio.android.common.libs.validator.validator.NotEmptyValidator; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.ContentResolver; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.Window; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class RegisterActivity extends Activity { private EditText etName,etNickname,etPass,etMotto,etEmail,etCity; private Button btRegister; private Form form; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); //点击自定义控件换头像 RoundedImageView rivHead=(RoundedImageView) findViewById(R.id.rivHead); rivHead.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { Log.d("MainActivity","show2"); Intent intent = new Intent(); intent.setType("image/*"); //开启Pictures画面Type设定为image intent.setAction(Intent.ACTION_GET_CONTENT); //使用Intent.ACTION_GET_CONTENT这个Action startActivityForResult(intent, 1); //取得相片后返回本画面 } }); //表单验证 initView(); //获取控件 setListener(); //判断成功还是失败 validateForm(); //格式验证 } @Override //获取传递回来的头像 protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { Uri uri = data.getData(); ContentResolver cr = this.getContentResolver(); try { Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri)); RoundedImageView rivHead=(RoundedImageView) findViewById(R.id.rivHead); rivHead.setImageBitmap(bitmap); //将Bitmap设定到ImageView } catch (FileNotFoundException e) { Log.e("Exception", e.getMessage(),e); } } super.onActivityResult(requestCode, resultCode, data); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.register, menu); return true; } private void initView(){ //实例表单验证需要的控件 etName=(EditText) findViewById(R.id.etName); etNickname=(EditText) findViewById(R.id.etNickname); etPass=(EditText) findViewById(R.id.etPass); etMotto=(EditText) findViewById(R.id.etMotto); etEmail=(EditText) findViewById(R.id.etEmail); etCity=(EditText) findViewById(R.id.etCity); btRegister=(Button) findViewById(R.id.btRegister); } private void setListener(){ //注册按钮点击事件 btRegister.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { boolean flag=form.validate(); if(flag){ Toast.makeText(RegisterActivity.this, "验证成功!", Toast.LENGTH_LONG).show(); }else{ Toast.makeText(RegisterActivity.this, "验证失败", Toast.LENGTH_LONG).show(); } } }); } private void validateForm(){ //格式验证与非空验证 form=new Form(); //创建Form用来装控件 //非空验证 Validate nameNotEmptyValidate=new Validate(etName); //创建Validate类,将被验证控件传入 Validate nickNameNotEmptyValidate=new Validate(etNickname); Validate passNotEmptyValidate=new Validate(etPass); Validate mottoNotEmptyValidate=new Validate(etMotto); Validate cityNotEmptyValidate=new Validate(etCity); NotEmptyValidator notEmpty=new NotEmptyValidator(this); //创建非空验证类 nameNotEmptyValidate.addValidator(notEmpty); //将这个Validate类addValidator加入非空验证类 nickNameNotEmptyValidate.addValidator(notEmpty); passNotEmptyValidate.addValidator(notEmpty); mottoNotEmptyValidate.addValidator(notEmpty); cityNotEmptyValidate.addValidator(notEmpty); //邮箱格式验证 Validate emailValidate=new Validate(etEmail); //创建Validate类,将被验证控件传入 EmailValidator emailValidator=new EmailValidator(this); //创建邮箱格式验证类 emailValidate.addValidator(emailValidator); //将这个Validate类addValidator加入邮箱格式验证类 form.addValidates(nameNotEmptyValidate); //Form表单addValidates所有的Validate类即可 form.addValidates(nickNameNotEmptyValidate); form.addValidates(passNotEmptyValidate); form.addValidates(mottoNotEmptyValidate); form.addValidates(cityNotEmptyValidate); form.addValidates(emailValidate); } }