开发过程中会遇见很多应用注册时,需要通过手机发送验证码验证,这是可以封装一个验证码按钮:
attrs.xml
& lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt; resources> & lt; declare-styleable name=" VerifyCodeButton比; & lt; !——默认背景——比; & lt; attr name=" android:背景”/比; & lt; !——点击后背景——比; & lt; attr name==安慰肌?癱lickedBackground”格式比; & lt; !——倒计时间——比; & lt; attr name==罢?癱ountdownTime”格式比; & lt; !——倒计时间后提示文字——比; & lt; attr name==白址?癱ountdownText”格式比; & lt;/declare-styleable> & lt;/resources>
自定义按钮
公开课VerifyCodeButton扩展按钮{ 私人上下文mContext; 私人int mClickedBackground;//点击后背景 私人int mBackground;//当前背景 私人字符串mCountdownownText; 私人int mCountdownTime=60; 私人TimeCount mTimeCount; 公共VerifyCodeButton(上下文语境){ 这(上下文,null); } 公共VerifyCodeButton(上下文语境,AttributeSet attrs) { 这(上下文、attrs android.R.attr.buttonStyle); } 公共VerifyCodeButton(上下文语境、AttributeSet attrs int defStyleAttr) { 超级(上下文、attrs defStyleAttr); mContext=上下文; initAttrs (attrs); init (); } 私人空间initAttrs (AttributeSet attrs) { TypedArray TypedArray=mContext。obtainStyledAttributes (attrs R.styleable.VerifyCodeButton); mBackground=typedArray.getResourceId (R.styleable。VerifyCodeButton_android_background mBackground); mClickedBackground=typedArray.getResourceId (R.styleable。VerifyCodeButton_clickedBackground mClickedBackground); mCountdownTime=typedArray.getInt (R.styleable。VerifyCodeButton_countdownTime mCountdownTime); mCountdownownText=typedArray.getString (R.styleable.VerifyCodeButton_countdownText); typedArray.recycle (); } 私人空间init () { setBackgroundResource (mBackground); mTimeCount=new TimeCount (mCountdownTime * 1000, 1000); }/* * *开始计时 */公共空间开始(){ mTimeCount.start (); }/* * *取消计时 */公共空间取消(){ mTimeCount.cancel (); setClickable(真正的); setText (mCountdownownText !=null & # 63;mCountdownownText: "); setBackgroundResource (mBackground); } 类TimeCount延伸CountDownTimer {/* * * @param millisInFuture总时间 * @param countDownInterval间隔时间 */公共TimeCount(长millisInFuture长countDownInterval) { 超级(millisInFuture countDownInterval); }/* * * @param millisUntilFinished当前时间 */@Override 公共空间> & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt;形状xmlns: android=" http://schemas.android.com/apk/res/android " android:形状=熬匦巍北? & lt;角落android:=" 5 dp/半径比; & lt;固体android:颜色=" # feacc3”/比; & lt;/shape>
& lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt;形状xmlns: android=" http://schemas.android.com/apk/res/android " android:形状=熬匦巍北? & lt;角落android:=" 5 dp/半径比; & lt;固体android:颜色=" # 999999 "/比; & lt;/shape>
layout.xml
& lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt; LinearLayout xmlns: android=" http://schemas.android.com/apk/res/android " xmlns:应用=" http://schemas.android.com/apk/res-auto " xmlns:工具=" http://schemas.android.com/tools " android: layout_width=" match_parent " android: layout_height=" match_parent " android:取向=按怪薄? 工具:上下文=癱om.sample.verify.MainActivity”比; & lt; com.sample.verify.widget.VerifyCodeButton android: id=癅 + id/btn_verify_code” android: layout_width=" wrap_content " android: layout_height=" wrap_content " android: layout_gravity="中心" android: layout_margin=" 10 dp” android:背景=" @drawable/bg_btn_default” android:重力="中心" android:文本="获取验证码” android:输入textColor=" # ffffff " android: textSize=" 14 sp " 应用:clickedBackground=" @drawable/bg_btn_clicked” 应用:countdownText="重新获取” 应用:countdownTime=" 10 "/比; & lt;按钮 android: id=癅 + id/btn_cancle” android: layout_width=" wrap_content " android: layout_height=" wrap_content " android: layout_gravity=" center_horizontal " android:文本="取消倒计时“/比; & lt;/LinearLayout>android实现验证码按钮