获取验证码按钮点击后,一分钟内不可继续点击

 1 <input type="button" id="btn" value="免费获取验证码" />  
 2 <script type="text/javascript">  
 3     var wait=60;  
 4     function time(o) {  
 5         if (wait == 0) {  
 6             o.removeAttribute("disabled");            
 7             o.value="免费获取验证码";  
 8             wait = 60;  
 9         } else {  
10             o.setAttribute("disabled", true);  
11             o.value="重新发送(" + wait + ")";  
12             wait--;  
13             setTimeout(function() {  
14                 time(o)  
15             },  
16             1000)  
17         }  
18     }  
19     document.getElementById("btn").οnclick=function()
20     {time(this);}
21 </script>

 

转载于:https://www.cnblogs.com/grnBlogs/p/5394699.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取验证码的功能需要用到 Android 中的计时器(CountDownTimer)和短信验证。以下是一种简单的实现方式: 1. 在 XML 布局文件中添加一个按钮和一个 EditText,用于用户输入手机号码: ``` <EditText android:id="@+id/et_phone_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入手机号码"/> <Button android:id="@+id/btn_get_verification_code" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="获取验证码"/> ``` 2. 在 Activity 中找到这两个控件,并设置按钮的点事件: ```java public class MainActivity extends AppCompatActivity { private EditText etPhoneNumber; private Button btnGetVerificationCode; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etPhoneNumber = findViewById(R.id.et_phone_number); btnGetVerificationCode = findViewById(R.id.btn_get_verification_code); // 设置按钮的点事件 btnGetVerificationCode.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 获取用户输入的手机号码 String phoneNumber = etPhoneNumber.getText().toString(); // TODO: 调用短信验证 API,发送验证码 // 启动倒计时器 startCountDown(); } }); } // 启动倒计时器 private void startCountDown() { // 创建一个计时器,设置时间间隔为 1 秒,总时间为 60 秒 CountDownTimer countDownTimer = new CountDownTimer(60000, 1000) { // 时间间隔结束调用的方法 @Override public void onTick(long millisUntilFinished) { // 更新按钮上的文字 btnGetVerificationCode.setText(millisUntilFinished / 1000 + " 秒后重新发送"); // 禁用按钮 btnGetVerificationCode.setEnabled(false); } // 总时间结束调用的方法 @Override public void onFinish() { // 更新按钮上的文字 btnGetVerificationCode.setText("获取验证码"); // 启用按钮 btnGetVerificationCode.setEnabled(true); } }; // 启动计时器 countDownTimer.start(); } } ``` 注意:上述代码中关于短信验证的部分需要自己调用相应的 API 实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值