android 验证码按钮点击,判断网络和匹配手机号并自动更新时间;

    开发过程中会遇见很多app注册时需要通过手机发送验证码验证,并获取验证码进行验证,在发送验证码时会有个手机号判断和网络链接及按钮上的数字自动递减;为后续用起来方便,特地做个记录;

在布局之前先要做的是在AndroidManifest.xml注册网络权限;

android:name="android.permission.ACCESS_NETWORK_STATE" />


在来看一下布局,布局简单直接拉了两个控件,可根据自己需求定义;

xml version="1.0" encoding="utf-8"?>
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:orientation="vertical"
    >
   
       android:id="@+id/edit_text_view"
       android:layout_width="368dp"
       android:layout_height="wrap_content"
       android:inputType="phone"
       tools:layout_editor_absoluteY="0dp"
       tools:layout_editor_absoluteX="8dp" />
   
       android:layout_width="match_parent"
       android:layout_height="wrap_content">
      
          android:id="@+id/button_code"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginLeft="30dp"
          android:background="#898989"
          android:paddingRight="3dp"
          android:paddingLeft="3dp"
          android:text="获取验证码"/>
   


下面进入主题,看代码实现,关键地方已经用注释表明清楚了,在这里就不在重复了;

public class MainActivity extends Activity {
    private EditText edit;
    private Button bt;
    private static final String telephoneRegex = "^1[358][0-9]{9}$";
    private String b = "#528B8B";
    private String a = "#898989";
    private final String PHONE_NUMBER = "手机号不正确";
    private String again = "重新发送";
    private String Obtain = "正在获取";
    private long mTime = 60000;
    private long Onesecond = 1000;
    public static final String NETWORK_LIANJIESHIBAI = "请确认网络连接";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //第一种在setContentViewz之前调用关闭顶部actionbar;
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main_activity);
        initView();
    }
    private void initView() {
        edit = (EditText) findViewById(R.id.edit_text_view);
        bt = (Button) findViewById(R.id.button_code);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int tostInt;
                //判断是否链接网络
                if (isNetwork(getApplicationContext())) {
                    //判断手机号是否正确
                    int flag = phonenNumber();
                    if (flag == 0) {
                        tostInt = 0;
                        toastText(tostInt);
                    } else if (flag == 1) {
                        /*请求网络上传手机号码获取验证码,这里根据自己需求需定义;*/
                        //...................
                        //设置Button自动延时60,可根据自己项目时间定义;
                        TimerCount time = new TimerCount(mTime, Onesecond);
                        time.start();
                    }
                } else {
                    tostInt = 1;
                    toastText(tostInt);
                }
            }
        });
    }
    private void toastText(int tostInt) {
        if (tostInt == 0) {
            Toast.makeText(getApplication(), PHONE_NUMBER, Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getApplication(), NETWORK_LIANJIESHIBAI, Toast.LENGTH_SHORT).show();
        }
    }
    private int phonenNumber() {
        //通过正则,获取正确的手机号,正确为1反之0;
        if (null == edit.getText() || !telePhoneNumber(edit.getText().toString().trim())) {
            return 0;
        }
        return 1;
    }
    //匹配手机号;
    private boolean telePhoneNumber(String number) {
        if (number == null) {
            return false;
        }
        if (!number.matches(telephoneRegex)) {
            return false;
        }
        return true;
    }
    //自动更新Button时间延时,并可设置背景颜色
    class TimerCount extends CountDownTimer {
        public TimerCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }
        @Override
        public void onFinish() {
            bt.setText(again);
            bt.setClickable(true);
            bt.setBackgroundColor(Color.parseColor(b));
        }
        @Override
        public void onTick(long millisUntilFinished) {
            bt.setBackgroundColor(Color.parseColor(a));
            bt.setClickable(false);
            bt.setText(millisUntilFinished / 1000 + Obtain);
        }
    }
    public boolean isNetwork(Context context) {
        //判断网络是否链接
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }
}


上传两张对比图片;


android 验证码按钮点击,判断网络和匹配手机号并自动更新时间;_第1张图片


android 验证码按钮点击,判断网络和匹配手机号并自动更新时间;_第2张图片


OK ! 完美结束;


你可能感兴趣的:(android,研发,android,开发人员,app,验证码,网络)