android程序设计中输入手机号,验证格式,并倒计时几秒后再次发送

activity程序

public class MainActivity extends Activity implements OnClickListener {
    EditText et_phonenum;
    Button btn_send;

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

    @Override
    public void onClick(View v) {
        // 模拟发送短信
        String phone = et_phonenum.getText().toString();
        if (phone.matches("[1][3579][\\d]{9}")) {
            // 不能继续发
            btn_send.setEnabled(false);
            MyCountDownTimer timer = new MyCountDownTimer();
            // 开启
            timer.start();
            // 使用倒计时
            // new Thread() {
            // public void run() {
            // try {
            // sleep(2000);
            // } catch (InterruptedException e) {
            // // TODO Auto-generated catch block
            // e.printStackTrace();
            // }
            // // 1. 子线程不允许修改UI UI线程就是主线程
            // // 2. 主线程不允许使用网络请求
            // // 3. 在Activity中如果需要访问UI,可以使用
            // runOnUiThread(new Runnable() {
            //
            // @Override
            // public void run() {
            // btn_send.setEnabled(true);
            // }
            // });
            // };
            // }.start();
        }else {
            Toast.makeText(this, "手机号码格式错误", Toast.LENGTH_SHORT).show();
        }
    }

    class MyCountDownTimer extends CountDownTimer {
        // 必须显示的调用父类的构造方法
        public MyCountDownTimer() {
            // millisInFuture 倒计时的时间 毫秒
            // countDownInterval间隔多少毫秒执行一次事件
            super(10000, 1000);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            // 每countDownInterval触发一次onTick事件
            btn_send.setText("还剩" + millisUntilFinished / 1000 + "秒可以再次发送");
        }

        @Override
        public void onFinish() {
            btn_send.setEnabled(true);
            btn_send.setText("发送");
        }

    }
}

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"
    tools:context="com.example.lesson4_daojishi.MainActivity" >

    <EditText
        android:id="@+id/et_phonenum"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:digits="1234567890"
        android:hint="输入手机号"
        android:inputType="number" />

    <Button
        android:id="@+id/btn_send"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="发送" />

</LinearLayout>

注意:android中主线程和子线程的注意事项,倒计时的用法,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值