CountDownTimer倒計時器,獲取驗證碼,的使用

首先  先把封裝好的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>
然後就可以用啦 相當的簡單  不用自己去寫了  系統自帶的東西 用着還舒服

    

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章