实现的效果:
(1)创建一个CustomButton类继承Button:
package com.example.administrator.customview;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.widget.Button;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by Administrator on 2016/10/12.
* 提示:因为此类继承了Button,所以Button的属性和方法都可以在此类中直接调用
*/
public class CustomButton extends Button {
private Timer timer; //轮询器
private int mMaxnum; //倒计时的时间
private String mSend; //button上显示的内容
/**
* 通过Handler来获取轮询器(子线程)中的内容,然后在主线程中更新UI
*/
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int what = msg.what;
if (what == 0) { //倒计时进行中
//直接调用自己的方法
setText("" + mMaxnum);
}
if (what == 1) { //倒计时结束,重置mMaxnum
mMaxnum = 10;
setEnabled(true);
setText(mSend);
}
}
};
/**
* 在代码中初始化控件时走此构造方法
* @param context
*/
public CustomButton(Context context) {
super(context);
}
/**
* 有自定义属性(res-values-attrs.xml)时走此构造方法
* @param context
* @param attrs 属性 从xml布局中传递过来的属性
*/
public CustomButton(Context context, AttributeSet attrs) {
super(context, attrs);
//获取在values-attrs.xml中声明的属性
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomButton);
mMaxnum = typedArray.getInteger(R.styleable.CustomButton_maxnum,60);
mSend = typedArray.getString(R.styleable.CustomButton_send);
//把自定义的属性值设置给button
setSend(mSend);
setMaxNum(mMaxnum);
}
//设置按钮初始文字
public void setSend(String send) {
this.mSend = send;
}
//设置最大时间
public void setMaxNum(int maxNum) {
this.mMaxnum = maxNum;
}
//开始倒计时的方法
public void startCountDown() {
//开始倒计时的时候要禁止再次点击
setEnabled(false);
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if (mMaxnum <= 0) {
timer.cancel();
mHandler.sendEmptyMessage(1);
return;
}
mMaxnum --;
mHandler.sendEmptyMessage(0);
}
}, 0, 1000);
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--声明属性-->
<declare-styleable name="CustomButton"> <!--name:自定义view的名字-->
<attr name="maxnum" format="integer"/> <!--name:属性名。format: 属性的类型-->
<attr name="send" format="string|reference"/>
</declare-styleable>
</resources>
(3)自定义Button对应的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--引入自定义的view。 在这里面定义相应控件的属性值-->
<com.example.administrator.customview.CustomButton
android:id="@+id/btn_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取验证码"
android:textSize="18sp"
android:textColor="#f00"
app:maxnum="10"
app:send="再次获取验证码"/>
</RelativeLayout>
(4)在MainActivity(这里我改成里CustomActivity)中初始化自定义的Button,并对其设置监听事件:
package com.example.administrator.customview;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
/**
* Created by Administrator on 2016/10/12.
*/
public class CustomActivity extends AppCompatActivity {
private CustomButton mCustomBtn; //自定义的Button
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_button); //自定义的布局文件
initView();
}
private void initView() {
mCustomBtn = (CustomButton) findViewById(R.id.btn_custom);
//对button按钮设置监听事件,点击后开始倒计时
mCustomBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(CustomActivity.this, "验证码已发送", Toast.LENGTH_SHORT).show();
//调用自定义button的开始倒计时的方法
mCustomBtn.startCountDown();
}
});
}
}
经过上面四步便能实现一个简单的倒计时效果。