android自定义view之刷新验证码

今天大家和分享下登入时候经常遇到的问题,这边只是个模型:

首先自定义view来onMeasue 作为存放验证码的地方,重写的view需要重写onDrawn和onMeasue 2个方法。

onMeasue 这个方法是用来测量长宽:

  @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        EXACTLY:一般是设置了明确的值或者是MATCH_PARENT
//        AT_MOST:表示子布局限制在一个最大值内,一般为WARP_CONTENT
        int widthMode=MeasureSpec.getMode(widthMeasureSpec);
        int widthSize=MeasureSpec.getSize(widthMeasureSpec);
        int heightMode=MeasureSpec.getMode(heightMeasureSpec);
        int heigtSize=MeasureSpec.getSize(heightMeasureSpec);
        int width;
        int height;
        if(widthMode==MeasureSpec.EXACTLY){
            width=widthSize;
        }
        else{
            mPaint.setTextSize(mTittleSize);
            mPaint.getTextBounds(mTitleName,0,mTitleName.length(),mRect);
            float texWidth=mRect.width();
            int desired=(int)(getPaddingLeft()+texWidth+getPaddingRight());
            width=desired;
        }
        if(heightMode==MeasureSpec.EXACTLY){
            height=heigtSize;
        }
        else{
            mPaint.setTextSize(mTittleSize);
            mPaint.getTextBounds(mTitleName,0,mTitleName.length(),mRect);
            float texHeight=mRect.height();
            int desired=(int)(getPaddingLeft()+texHeight+getPaddingRight());
            height=desired;
        }
        setMeasuredDimension(width, height);
    }

至于onDraw是画图用的:

@Override
protected void onDraw(Canvas canvas) {
    mPaint.setColor(getResources().getColor(R.color.colorPrimary));
    canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(),mPaint);
    mPaint.setColor(mTitleColor);
    canvas.drawText(mTitleName, getWidth() / 2 - mRect.width() / 2, getHeight() / 2 + mRect.height() / 2, mPaint);
}

再次确定布局文件:

其中值得注意的是,

xmlns:custom="http://schemas.android.com/apk/res-auto"

这个是适应android studio的,在eclipse不是这样写的。   
 
 
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <EditText
        android:id="@+id/et_number"
        android:layout_weight="4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="请输入验证码"/>
    <com.example.john.customviewtitle.CustomTitleView
        android:layout_weight="1"
        android:id="@+id/tv_custom"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        custom:titleSize="16sp"
        custom:titleName="12345"
        android:padding="10dp"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="true"
        custom:titlleColor="#ff0000"/>
    </LinearLayout>
    <Button
        android:id="@+id/btn_submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击验证"/>
</LinearLayout>

attrs.xml中的文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="titleName" format="string"/>
    <attr name="titlleColor" format="color"/>
    <attr name="titleSize" format="dimension"/>
     <declare-styleable name="CustomTitleView">
         <attr name="titleName"/>
         <attr name="titlleColor"/>
         <attr name="titleSize"/>
     </declare-styleable>
</resources>

至于activity中的代码就相对简单多了:

mCustomView.setmBack(new CustomTitleView.CallBack() {
    @Override
    public void onCBack(String numbers) {
        number=numbers;
    }
});
mBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if(mEt.getText().toString().equals(number)){
            Toast.makeText(MainActivity.this,"验证通过!",Toast.LENGTH_LONG).show();
        }
        else {
            Toast.makeText(MainActivity.this,"验证失败!",Toast.LENGTH_LONG).show();

        }
    }
});

一个简单的回调而已。

各位抱歉,表达能力有缺陷,如有需要的请查看源码。

http://download.csdn.net/detail/u013651405/9464444

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值