Android中获取验证码时的倒计时效果


项目结构:


核心代码:

activity_main.xml

<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" >


   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="32dp"
        android:layout_marginRight="32dp"
        android:gravity="center_horizontal"
        android:orientation="vertical" >


        <TextView
            android:id="@+id/text_fragment_login_logo"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="32dp"
            android:scaleType="centerInside"
            android:textSize="28sp"
            android:textColor="#fff"
            android:gravity="center"
            android:textStyle="bold"
            android:text="停车宝(代泊员)"/>


        <EditText
            android:id="@+id/login_ed_shoujihao"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="32dp"
            android:hint="手机号"
            android:maxLength="11"
            android:maxLines="1"
            android:paddingTop="15dp"
            android:paddingBottom="15dp"
            android:numeric="integer" >


            <requestFocus />
        </EditText>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:orientation="horizontal" >


            <EditText
                android:id="@+id/login_ed_yanzhengma"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="验证码"
                android:maxLength="6"
                android:maxLines="1"
                android:layout_marginRight="8dp"
                android:paddingTop="15dp"
                android:paddingBottom="15dp"/>


            <Button
                android:id="@+id/login_btn_huoquyanzhengma"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="login_btn_huoquyanzhengma_onclick"
                android:textColor="#fff"
                android:textSize="20sp" 
                android:text="获取验证码" />
        </LinearLayout>


        <Button
            android:id="@+id/login_btn_denglu"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:background="#e65400"
            android:onClick="login_denglu_onclick"
            android:text="登录"
            android:padding="15dp"
            android:textColor="#fff"
            android:textSize="20sp" />


        


    </LinearLayout>
</LinearLayout>



MainActivity

public class MainActivity extends Activity implements OnClickListener{
Thread thread=null;
private boolean             tag          = true;
   private int                 i            = 60;
   public boolean              isChange     = false;
   private Button btnCode=null;
 


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnCode = (Button) findViewById(R.id.login_btn_huoquyanzhengma);
btnCode.setOnClickListener(this);
}

 @Override
   public void onClick(View v) {
 if (v.getId() == R.id.login_btn_huoquyanzhengma) {
   btnCode.setText("获取验证码");
           btnCode.setClickable(true);
           isChange = true;
           changeBtnGetCode();
 

}
 }

 private void changeBtnGetCode() {
       thread = new Thread() {
           @Override
           public void run() {
               if (tag) {
                   while (i > 0) {
                       i--;
                       if (MainActivity.this== null) {
                           break;
                       }
                       //当文本框内容改变时,结束循环。
//                        if (isChange && !btnCode.isClickable()) {
//                            isChange = false;
//                            break;
//                        }
                      MainActivity.this.runOnUiThread(new Runnable() {
                           @Override
                           public void run() {
                               btnCode.setText("获取验证码(" + i + ")");
                               btnCode.setClickable(false);
                           }
                       });
                       try {
                           Thread.sleep(1000);
                       } catch (InterruptedException e) {
                           throw new RuntimeException(e);
                       }
                   }
                   tag = false;
               }
               i = 60;
               tag = true;
               if (MainActivity.this != null) {
               MainActivity.this.runOnUiThread(new Runnable() {
                       @Override
                       public void run() {
                           btnCode.setText("获取验证码");
                           btnCode.setClickable(true);
                       }
                   });
               }
           };
       };
       thread.start();
   }




}//class

没有明白下载我的例子:

http://download.csdn.net/detail/zhaihaohao1/8387081




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值