注册界面+获取验证码倒计时设置
layout文件代码:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 <LinearLayout 6 android:layout_width="match_parent" 7 android:layout_height="wrap_content" 8 android:orientation="horizontal"> 9 <TextView 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:text="@string/usercode"/> 13 <EditText 14 android:id="@+id/logon_et_usercode" 15 android:layout_width="0dp" 16 android:layout_height="wrap_content" 17 android:layout_weight="1" 18 android:hint="@string/usercodhint"/> 19 </LinearLayout> 20 <LinearLayout 21 android:layout_width="match_parent" 22 android:layout_height="wrap_content" 23 android:orientation="horizontal"> 24 <TextView 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 android:text="@string/logon_password"/> 28 <EditText 29 android:id="@+id/logon_et_password" 30 android:layout_width="0dp" 31 android:layout_height="wrap_content" 32 android:layout_weight="1" 33 android:hint="@string/logon_pwdhint"/> 34 </LinearLayout> 35 <LinearLayout 36 android:layout_width="match_parent" 37 android:layout_height="wrap_content" 38 android:orientation="horizontal"> 39 <TextView 40 android:layout_width="wrap_content" 41 android:layout_height="wrap_content" 42 android:text="@string/logon_secondpwd"/> 43 <EditText 44 android:id="@+id/logon_et_secondpwd" 45 android:layout_width="0dp" 46 android:layout_height="wrap_content" 47 android:layout_weight="1" 48 android:hint="@string/logon_secondpwdhint"/> 49 </LinearLayout> 50 <LinearLayout 51 android:layout_width="match_parent" 52 android:layout_height="wrap_content" 53 android:orientation="horizontal"> 54 <TextView 55 android:layout_width="wrap_content" 56 android:layout_height="wrap_content" 57 android:text="@string/sex"/> 58 <RadioGroup 59 android:layout_width="wrap_content" 60 android:layout_height="wrap_content" 61 android:orientation="horizontal"> 62 <RadioButton 63 android:id="@+id/logon_rb_man" 64 android:layout_width="wrap_content" 65 android:layout_height="wrap_content" 66 android:text="男"/> 67 <RadioButton 68 android:id="@+id/logon_rb_woman" 69 android:layout_width="wrap_content" 70 android:layout_height="wrap_content" 71 android:text="女"/> 72 </RadioGroup> 73 </LinearLayout> 74 <LinearLayout 75 android:layout_width="match_parent" 76 android:layout_height="wrap_content" 77 android:orientation="horizontal"> 78 <TextView 79 android:layout_width="wrap_content" 80 android:layout_height="wrap_content" 81 android:text="@string/verificationcode"/> 82 <EditText 83 android:id="@+id/logon_et_verificationcodehint" 84 android:layout_width="0dp" 85 android:layout_height="wrap_content" 86 android:layout_weight="0.6" 87 android:hint="@string/verificationcodehint"/> 88 <Button 89 android:id="@+id/logon_bt_getverificationcode" 90 android:layout_width="0dp" 91 android:layout_height="wrap_content" 92 android:layout_weight="0.4" 93 android:text="@string/getverificationcode"/> 94 95 </LinearLayout> 96 <LinearLayout 97 android:layout_width="match_parent" 98 android:layout_height="wrap_content" 99 android:orientation="vertical"> 100 <Button 101 android:layout_width="match_parent" 102 android:layout_height="wrap_content" 103 android:text="@string/commit" 104 android:id="@+id/logon_bt_commit"/> 105 </LinearLayout> 106 107 </LinearLayout>
java文件:
1 package com.example.administrator.myapplication; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.os.CountDownTimer; 6 import android.view.View; 7 import android.widget.Button; 8 import android.widget.EditText; 9 import android.widget.RadioButton; 10 11 import java.util.Timer; 12 import java.util.TimerTask; 13 14 /** 15 * Created by Administrator on 2016/8/10. 16 */ 17 public class RegisterActivity extends Activity implements View.OnClickListener { 18 EditText mEt_Usercode,mRegisterEt_Pwd,mRegisterEt_SecondPwd,mEt_Code; 19 RadioButton mRb_Man,mRb_Woman; 20 Button mBt_Commit,mBt_Code; 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.register); 25 mEt_Usercode=(EditText)findViewById(R.id.logon_et_usercode); 26 mRegisterEt_Pwd=(EditText)findViewById(R.id.logon_et_password); 27 mRegisterEt_SecondPwd=(EditText)findViewById(R.id.logon_et_secondpwd); 28 mEt_Code=(EditText)findViewById(R.id.logon_et_verificationcodehint); 29 mRb_Man=(RadioButton)findViewById(R.id.logon_rb_man); 30 mRb_Woman=(RadioButton)findViewById(R.id.logon_rb_woman); 31 mBt_Code=(Button)findViewById(R.id.logon_bt_getverificationcode); 32 mBt_Commit=(Button)findViewById(R.id.logon_bt_commit); 33 34 mBt_Commit.setOnClickListener(this); 35 mBt_Code.setOnClickListener(this); 36 } 37 private int count=60; 38 39 @Override 40 public void onClick(View view) { 41 switch (view.getId()) 42 { 43 case R.id.logon_bt_commit: 44 break; 45 case R.id.logon_bt_getverificationcode: 46 mBt_Code.setEnabled(false); 47 timer.start(); 48 break; 49 } 50 51 } 52 CountDownTimer timer=new CountDownTimer(60000,1000) { 53 @Override 54 public void onTick(long millisUntilFinished) { 55 mBt_Code.setText(millisUntilFinished/1000+"秒"); 56 } 57 58 @Override 59 public void onFinish() { 60 mBt_Code.setEnabled(true); 61 mBt_Code.setText("发送验证码"); 62 } 63 }; 64 65 @Override 66 protected void onDestroy() { 67 super.onDestroy(); 68 timer.cancel(); 69 } 70 }
参考网址:http://www.jianshu.com/p/a953ae1c76c3