android短信按钮自定义控件

xiaoxiao2021-02-28  129

     写到自定义短信按钮想到的首先是我们注册的时候 ,加上我们的第三方短信验证,在学习是为此苦恼。写了一个自定义控件方便了许多。  废话不多说,直接贴代码:

public class TimeButton extends Button { //定义时间长度 private final int TIME_COUNT = 60; //获取时间长度 进行操作 private int mCount = TIME_COUNT; private String mText; //有参构造方法 public TimeButton(Context context, AttributeSet attrs) { super(context, attrs); } //开始的方法 public void starDown() { //获取当前text mText = (String) getText(); //把按钮设为false setEnabled(false); doDown(); } //进行操作 private void doDown() { //设置文本 setText(String.format("%s秒后重试",mCount)); //开启线程 postDelayed(new Runnable() { @Override public void run() { mCount--; //为0就停止 if(mCount==0){ stopDown(); }else{ //不为0就递归调用 doDown(); } } },1000); } //停止方法 private void stopDown() { //重新为变量赋值 mCount=TIME_COUNT; setText(mText); //按钮设为true setEnabled(true); } }

然后在布局文件中的使用 :

<com.example.a3559dell.customview.TimeButton android:layout_below="@id/button" android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送短信"/>

   最后在MainActivity中找到控件 调用方法就行了

转载请注明原文地址: https://www.6miu.com/read-39624.html

最新回复(0)