android 验证码按钮点击,判断网络和匹配手机号并自动更新时间;
来源:互联网 发布:python如何调用dll 编辑:程序博客网 时间:2024/05/23 18:17
开发过程中会遇见很多app注册时需要通过手机发送验证码验证,并获取验证码进行验证,在发送验证码时会有个手机号判断和网络链接及按钮上的数字自动递减;为后续用起来方便,特地做个记录;
在布局之前先要做的是在AndroidManifest.xml注册网络权限;
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
在来看一下布局,布局简单直接拉了两个控件,可根据自己需求定义;
<?xml version="1.0" encoding="utf-8"?><LinearLayout 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" > <EditText 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" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button 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="获取验证码"/> </LinearLayout></LinearLayout>
下面进入主题,看代码实现,关键地方已经用注释表明清楚了,在这里就不在重复了;
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; }}
上传两张对比图片;
OK ! 完美结束;
阅读全文
1 0
- android 验证码按钮点击,判断网络和匹配手机号并自动更新时间;
- android 按钮拖动并点击判断
- iOS 输入手机号和验证码的TextField,使获取验证码,提交订单 按钮可用
- 输入框进行判断验证(弹出对话框,点击按钮查询并隐藏)
- android按钮点击时间倒数
- 验证手机号,邮箱号,网络状态,日期时间等函数
- android 验证手机号和邮箱格式
- 手机号匹配并打星号
- 《新手养成记》--第一篇 iOS手机号正则判断和获取验证码
- Android手机号、密码、验证码正则表达式
- Android 手机号,邮箱验证
- Android验证手机号
- 判断按钮的点击和抬起
- 校验手机号和获取验证码
- 手机号和用户名验证
- 判断手机号和邮箱
- 绑定手机号,并发送段信验证码
- android 动态加载按钮并为按钮设置点击事件
- netty 之 ServerBootstrap的启动流程
- Nginx配置多虚拟主机(即配置域名)
- SourceTree使用SSH克隆码云项目
- max
- 9.非线性支持向量机与核函数
- android 验证码按钮点击,判断网络和匹配手机号并自动更新时间;
- 彩虹表使用
- 观察者模式
- 考试10.30
- json in java
- Redis集合
- js引用
- Idea奇怪问题解决方案
- rmmod: can't change directory to '/lib/modules': No such file or directory