注册信息时,验证码的发送与验证

简单的注册功能,代码和图样如下,共享给大家,希望对你有所帮助

布局文件:

  1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2     xmlns:tools="http://schemas.android.com/tools"
  3     android:layout_width="match_parent"
  4     android:layout_height="match_parent"
  5     tools:context="${relativePackage}.${activityClass}" >
  6     
  7     <TextView
  8         android:id="@+id/phone_register"
  9         android:layout_width="wrap_content"
 10         android:layout_height="50dp"
 11         android:layout_marginLeft="30dp"
 12         android:gravity="center"
 13         android:text="手机注册"
 14         android:textSize="16sp" />
 15 
 16     <RelativeLayout
 17         android:id="@+id/register_message"
 18         android:layout_width="match_parent"
 19         android:layout_height="wrap_content"
 20         android:layout_below="@+id/phone_register"
 21         android:layout_marginLeft="20dp"
 22         android:layout_marginTop="20dp" >
 23 
 24         <ImageView
 25             android:id="@+id/phone_img"
 26             android:layout_width="40dp"
 27             android:layout_height="40dp"
 28             android:padding="9dp"
 29             android:src="@drawable/phone_small_gray" />
 30 
 31         <EditText
 32             android:id="@+id/phone_number"
 33             android:layout_width="200dp"
 34             android:layout_height="40dp"
 35             android:layout_toRightOf="@+id/phone_img"
 36             android:hint="请输如手机号码" />
 37 
 38         <ImageView
 39             android:id="@+id/yanzheng_img"
 40             android:layout_width="40dp"
 41             android:layout_height="40dp"
 42             android:layout_below="@+id/phone_img"
 43             android:padding="9dp"
 44             android:src="@drawable/reg_sms_code" />
 45 
 46         <EditText
 47             android:id="@+id/yanzheng_number"
 48             android:layout_width="130dp"
 49             android:layout_height="40dp"
 50             android:layout_below="@+id/phone_img"
 51             android:layout_toRightOf="@+id/yanzheng_img"
 52             android:maxLength="6"
 53             android:hint="请输入验证码" />
 54 
 55         <Button
 56             android:id="@+id/reSend"
 57             android:layout_width="wrap_content"
 58             android:layout_height="30dp"
 59             android:layout_alignBottom="@+id/yanzheng_number"
 60             android:layout_toRightOf="@+id/yanzheng_number"
 61             android:layout_marginBottom="5dp"
 62             android:background="@drawable/yuanjiaoborder"
 63             android:hint="获取验证码"
 64             android:padding="3dp" />
 65 
 66         <ImageView
 67             android:id="@+id/yanzheng_number_img"
 68             android:layout_width="wrap_content"
 69             android:layout_height="wrap_content"
 70             android:layout_alignBottom="@+id/yanzheng_number"
 71             android:layout_alignRight="@+id/yanzheng_number"
 72             android:layout_marginBottom="5dp"
 73             android:visibility="gone" />
 74 
 75         <ImageView
 76             android:id="@+id/yanzheng_number_ok_no"
 77             android:layout_width="25dp"
 78             android:layout_height="25dp"
 79             android:layout_alignBottom="@+id/yanzheng_number"
 80             android:layout_toRightOf="@+id/reSend"
 81             android:src="@drawable/icon_checkbox_on"
 82             android:visibility="gone" />
 83 
 84         <ImageView
 85             android:id="@+id/user_name_img"
 86             android:layout_width="40dp"
 87             android:layout_height="40dp"
 88             android:layout_below="@+id/yanzheng_img"
 89             android:padding="9dp"
 90             android:src="@drawable/login_user_avatar" />
 91 
 92         <EditText
 93             android:id="@+id/user_name"
 94             android:layout_width="200dp"
 95             android:layout_height="40dp"
 96             android:layout_below="@+id/yanzheng_img"
 97             android:layout_toRightOf="@+id/user_name_img"
 98             android:hint="请输入验昵称" />
 99 
100         <ImageView
101             android:id="@+id/user_pwd_img"
102             android:layout_width="40dp"
103             android:layout_height="40dp"
104             android:layout_below="@+id/user_name_img"
105             android:padding="9dp"
106             android:src="@drawable/login_password" />
107 
108         <EditText
109             android:id="@+id/user_pwd"
110             android:layout_width="200dp"
111             android:layout_height="40dp"
112             android:layout_below="@+id/user_name_img"
113             android:layout_toRightOf="@+id/user_pwd_img"
114             android:hint="请输入密码" />
115     </RelativeLayout>
116 
117     <LinearLayout
118         android:layout_width="match_parent"
119         android:layout_height="wrap_content"
120         android:layout_below="@+id/register_message"
121         android:orientation="vertical" >
122 
123         <Button
124             android:id="@+id/register_ok"
125             android:layout_width="200dp"
126             android:layout_height="40dp"
127             android:layout_marginTop="10dp"
128             android:layout_marginLeft="60dp"
129             android:gravity="center"
130             android:text="完成" />
131 
132         <TextView
133             android:id="@+id/back_login"
134             android:layout_width="match_parent"
135             android:layout_height="wrap_content"
136             android:layout_below="@+id/register_ok"
137             android:gravity="center_horizontal"
138             android:paddingTop="15dp"
139             android:text="已有果豆食谱账号,点击登录" />
140     </LinearLayout>
141 
142 </RelativeLayout>

注册处理的activity:

1:初始化布局文件

1     ImageView yangzheng_number_img;
2     EditText phone_number;
3     EditText user_name;
4     EditText user_pwd;
5     EditText yanzhengNumber;
6     Button reSend;
7     Button registerOk;
8     TextView back_login;
 1 phone_number=(EditText) findViewById(R.id.phone_number);
 2 phoneNumber=phone_number.getText().toString();
 3         
 4 yanzhengNumber=(EditText)findViewById(R.id.yanzheng_number);
 5 yangzheng_number_img=(ImageView)findViewById(R.id.yanzheng_number_img);
 6         
 7 user_name=(EditText) findViewById(R.id.user_name);
 8 user_pwd=(EditText) findViewById(R.id.user_pwd);
 9         
10                 
11 registerOk=(Button) findViewById(R.id.register_ok);
12 registerOk.setOnClickListener(this);
13 reSend = (Button) findViewById(R.id.reSend);
14 reSend.setOnClickListener(this);        
15 back_login=(TextView) findViewById(R.id.back_login);
16 back_login.setOnClickListener(this);

2:手机获取验证码

 1             phoneNumber=phone_number.getText().toString();
 2             String smsContent ="您的验证码为:"+Code.getInstance().getCode()+",请尽快输入页面";
 3             SmsManager smsManager = SmsManager.getDefault(); 
 4             if(phoneNumber.equals("")){
 5                 Toast.makeText(getApplicationContext(), "请输入手机号码", Toast.LENGTH_SHORT).show();
 6             }else{
 7                 //判断号码是否重复
 8                 if(smsContent.length()>70){
 9                     List<String> contents = smsManager.divideMessage(smsContent);  
10                     for(String c:contents){  
11                         smsManager.sendTextMessage(phoneNumber, null, c, null, null);  
12                     }  
13                 }else{  
14                     smsManager.sendTextMessage(phoneNumber, null, smsContent, null, null);  
15                 }  
16                 Toast.makeText(UserRegisterActivity.this, "短信发送成功", Toast.LENGTH_LONG);
17                 reSend.setText("重新发送");
18                 }

3:所需要的Code.java文件

  1 package com.ghp.tools;
  2 
  3 import java.util.Random;
  4 
  5 import android.graphics.Bitmap;
  6 import android.graphics.Bitmap.Config;
  7 import android.graphics.Canvas;
  8 import android.graphics.Color;
  9 import android.graphics.Paint;
 10 
 11 public class Code {
 12 
 13     private static final char[] CHARS = {  
 14         '2', '3', '4', '5', '6', '7', '8', '9',  
 15         'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', 'm',   
 16         'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',  
 17         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',   
 18         'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'  
 19     };  
 20       
 21     private static Code bmpCode;  
 22       
 23     public static Code getInstance() {  
 24         if(bmpCode == null)  
 25             bmpCode = new Code();  
 26         return bmpCode;  
 27     }  
 28       
 29     //default settings  
 30     private static final int DEFAULT_CODE_LENGTH = 3;  
 31     private static final int DEFAULT_FONT_SIZE = 25;  
 32     private static final int DEFAULT_LINE_NUMBER = 2;  
 33     private static final int BASE_PADDING_LEFT = 5, RANGE_PADDING_LEFT = 15, BASE_PADDING_TOP = 15, RANGE_PADDING_TOP = 20;  
 34     private static final int DEFAULT_WIDTH = 60, DEFAULT_HEIGHT = 40;  
 35       
 36     //settings decided by the layout xml  
 37     //canvas width and height  
 38     private int width = DEFAULT_WIDTH, height = DEFAULT_HEIGHT;   
 39       
 40     //random word space and pading_top  
 41     private int base_padding_left = BASE_PADDING_LEFT, range_padding_left = RANGE_PADDING_LEFT,   
 42             base_padding_top = BASE_PADDING_TOP, range_padding_top = RANGE_PADDING_TOP;  
 43       
 44     //number of chars, lines; font size  
 45     private int codeLength = DEFAULT_CODE_LENGTH, line_number = DEFAULT_LINE_NUMBER, font_size = DEFAULT_FONT_SIZE;  
 46       
 47     //variables  
 48     private String code;  
 49     private int padding_left, padding_top;  
 50     private Random random = new Random();  
 51     //验证码图片  
 52     public Bitmap createBitmap() {  
 53         padding_left = 0;  
 54           
 55         Bitmap bp = Bitmap.createBitmap(width, height, Config.ARGB_8888);   
 56         Canvas c = new Canvas(bp);  
 57   
 58         code = createCode();  
 59           
 60         c.drawColor(Color.WHITE);  
 61         Paint paint = new Paint();  
 62         paint.setTextSize(font_size);  
 63           
 64         for (int i = 0; i < code.length(); i++) {  
 65             randomTextStyle(paint);  
 66             randomPadding();  
 67             c.drawText(code.charAt(i) + "", padding_left, padding_top, paint);  
 68         }  
 69   
 70         for (int i = 0; i < line_number; i++) {  
 71             drawLine(c, paint);  
 72         }  
 73           
 74         c.save( Canvas.ALL_SAVE_FLAG );//保存    
 75         c.restore();//  
 76         return bp;  
 77     }  
 78       
 79     public String getCode() {  
 80         return code;  
 81     }  
 82       
 83     //验证码  
 84     private String createCode() {  
 85         StringBuilder buffer = new StringBuilder();  
 86         for (int i = 0; i < codeLength; i++) {  
 87             buffer.append(CHARS[random.nextInt(CHARS.length)]);  
 88         }  
 89         return buffer.toString();  
 90     }  
 91       
 92     private void drawLine(Canvas canvas, Paint paint) {  
 93         int color = randomColor();  
 94         int startX = random.nextInt(width);  
 95         int startY = random.nextInt(height);  
 96         int stopX = random.nextInt(width);  
 97         int stopY = random.nextInt(height);  
 98         paint.setStrokeWidth(1);  
 99         paint.setColor(color);  
100         canvas.drawLine(startX, startY, stopX, stopY, paint);  
101     }  
102       
103     private int randomColor() {  
104         return randomColor(1);  
105     }  
106   
107     private int randomColor(int rate) {  
108         int red = random.nextInt(256) / rate;  
109         int green = random.nextInt(256) / rate;  
110         int blue = random.nextInt(256) / rate;  
111         return Color.rgb(red, green, blue);  
112     }  
113       
114     private void randomTextStyle(Paint paint) {  
115         int color = randomColor();  
116         paint.setColor(color);  
117         paint.setFakeBoldText(random.nextBoolean());  //true为粗体,false为非粗体  
118         float skewX = random.nextInt(11) / 10;  
119         skewX = random.nextBoolean() ? skewX : -skewX;  
120         paint.setTextSkewX(skewX); //float类型参数,负数表示右斜,整数左斜  
121 //      paint.setUnderlineText(true); //true为下划线,false为非下划线  
122 //      paint.setStrikeThruText(true); //true为删除线,false为非删除线  
123     }  
124       
125     private void randomPadding() {  
126         padding_left += base_padding_left + random.nextInt(range_padding_left);  
127         padding_top = base_padding_top + random.nextInt(range_padding_top);  
128     }
129 }

4:确定注册时的代码

 1 String yzNum=yanzhengNumber.getText().toString();
 2             String bit=Code.getInstance().getCode();
 3             if(yzNum.equals("")){
 4                 Toast.makeText(getApplicationContext(), "请输入验证码", Toast.LENGTH_SHORT).show();
 5             }else if(!yzNum.equals(bit)){
 6                 Toast.makeText(getApplicationContext(), "验证码输入错误", Toast.LENGTH_SHORT).show();
 7                 yanzhengNumber.setText("");
 8                 getYanzhengNumber();
 9             }else if(yzNum.equals(bit)){
10                 Toast.makeText(getApplicationContext(), "验证码正确", Toast.LENGTH_SHORT).show();
11                 
12                 ImageView yanzheng_number_ok_no=(ImageView) findViewById(R.id.yanzheng_number_ok_no);
13                 yanzheng_number_ok_no.setVisibility(0);
14                 
15                 userPwd=user_pwd.getText().toString();
16                 userName=user_name.getText().toString();
17                 if(userName.equals("")||userPwd.equals("")){
18                     Toast.makeText(getApplicationContext(), "昵称和密码不能为空!", Toast.LENGTH_SHORT).show();
19                 }else if(userPwd.length()<6){
20                     Toast.makeText(getApplicationContext(), "密码长度不能小于6!", Toast.LENGTH_SHORT).show();
21                 }else{                    
22                     
23                     String url="http://172.27.211.1/guodouRecipe/servlet/insertIntoUser";
24                     CustomRequest request=new CustomRequest(Method.POST, url, new Listener<String>() {
25 
26                         @Override
27                         public void onResponse(String result) {
28                             // TODO Auto-generated method stub
29                             if(result.equals("true")){
30                                 Toast.makeText(getApplicationContext(), "注册成功!", Toast.LENGTH_SHORT).show();
31                                 new Handler().postDelayed(new Runnable(){//匿名类
32                                     //创建一个延时的调用
33                                     public void run(){
34                                         //写入本地数据库
35                                         Users u=new Users();
36                                         u.setUserPhoneNumber(phoneNumber);
37                                         u.setUserName(userName);
38                                         u.setUserPwd(userPwd);
39                                         
40                                         
41                                         Intent intent= new Intent(UserRegisterActivity.this,UserLoginActivity.class);
42                                         //intent.putExtra("userName", userName);
43                                         //intent.putExtra("userPwd", userPwd);
44                                         startActivityForResult(intent,4);                                        
45                                     }                                            
46                                     },1000);
47                             }
48                             if(result.equals("false")){
49                                 Toast.makeText(getApplicationContext(), "注册失败!result=false", Toast.LENGTH_SHORT).show();
50                             }
51                         }
52                     }, new ErrorListener() {
53 
54                         @Override
55                         public void onErrorResponse(VolleyError errReslut) {
56                             // TODO Auto-generated method stub
57                             Toast.makeText(getApplicationContext(), "err注册失败!", Toast.LENGTH_SHORT).show();
58                         }
59                     });
60                     request.setParam("phoneNumber", phoneNumber);
61                     request.setParam("userName", userName);
62                     request.setParam("userPwd", userPwd);
63                     
64                     GuodouRecipeApplication app=(GuodouRecipeApplication)UserRegisterActivity.this.getApplication();
65                     app.getmRequestQueue().add(request);                    
66                 }
67                                 
68             }

 

posted @ 2014-10-20 11:27  九卿  阅读(1292)  评论(0编辑  收藏  举报