CountDownTimer倒计时器,获取验证码,的使用

来源:互联网 发布:数据展现工具 编辑:程序博客网 时间:2024/06/05 09:17

首先  先把封装好的timercount贴出来


public class TimerCount extends CountDownTimer {
private TextView bnt;
private OnStop onStop;
public TimerCount(long millisInFuture, long countDownInterval, TextView bnt) {
super(millisInFuture, countDownInterval);
this.bnt = bnt;
}

public TimerCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
}

@Override
public void onFinish() {
// TODO Auto-generated method stub
bnt.setClickable(true);
// bnt.setBackgroundResource(R.drawable.bt_bg);//设置为正常色
bnt.setText("获取验证码");
if (onStop != null) {
onStop.onStop(false);
}
}

@Override
public void onTick(long arg0) {
// TODO Auto-generated method stub
bnt.setClickable(false);
// bnt.setBackgroundResource(R.drawable.shape_forgetpwd_bg);//设置为灰�?
bnt.setText(arg0 / 1000 + "S后重发送");
// bnt.setText("还剩"+arg0 / 1000 + "�?");
if (onStop != null) {
onStop.onStop(true);
}
}

public void onTimerStop(OnStop onStop){
this.onStop = onStop;
}
public interface OnStop{
void onStop(boolean flag);
}

}


然后 在mianactivity中写相关代码


public class MainActivity extends ActionBarActivity {

private TimerCount timerCount;
private TextView actRgSendCard;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actRgSendCard = (TextView)findViewById(R.id.actRgSendCard);
actRgSendCard.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
getCode();
}
});

}
//获取验证码
private void getCode(){
timerCount = new TimerCount(60000, 1000, actRgSendCard);

timerCount.start();
}


}

xml布局文件 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.timercount.MainActivity" >

<RelativeLayout
android:id="@+id/iphone"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:orientation="horizontal" >

<TextView
android:id="@+id/tv_phone"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:text="手机号"
android:textColor="#000000"
android:textSize="16sp" />

<EditText
android:id="@+id/find_iphone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/tv_phone"
android:background="@null"
android:gravity="center_vertical"
android:hint="请输入手机号"
android:inputType="number"
android:singleLine="true"
android:textColor="#444444"
android:textColorHint="#cccccc"
android:textCursorDrawable="@null"
android:textSize="16sp" />

<View
android:id="@+id/iphone_line"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_alignParentBottom="true"
android:background="#000000" />
</RelativeLayout>

<RelativeLayout
android:id="@+id/auth_code"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center_vertical"
android:orientation="horizontal" >

<TextView
android:id="@+id/tv_phone"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:text="验证码"

android:textSize="16sp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/tv_phone"
android:gravity="center_vertical"
android:orientation="horizontal" >

<EditText
android:id="@+id/find_authcode"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:background="@null"
android:gravity="center_vertical"
android:hint="请输入验证码"
android:inputType="number"
android:singleLine="true"
android:textColor="#444444"
android:textColorHint="#cccccc"
android:textCursorDrawable="@null"
android:textSize="16sp" />

<TextView
android:id="@+id/actRgSendCard"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:gravity="center"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="获取验证码"
android:textColor="#028ae6"
android:textSize="16sp" />
</LinearLayout>

<View
android:id="@+id/iphone_line"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_alignParentBottom="true"
android:background="#000000" />
</RelativeLayout>

</LinearLayout>
然后就可以用啦 相当的简单  不用自己去写了  系统自带的东西 用着还舒服

    

0 0
原创粉丝点击