android自定义view之刷新验证码
2016-03-17 15:23
645 查看
今天大家和分享下登入时候经常遇到的问题,这边只是个模型:首先自定义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不是这样写的。
<LinearLayoutxmlns: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"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><EditTextandroid:id="@+id/et_number"android:layout_weight="4"android:layout_width="0dp"android:layout_height="wrap_content"android:hint="请输入验证码"/><com.example.john.customviewtitle.CustomTitleViewandroid: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><Buttonandroid: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() {@Overridepublic void onCBack(String numbers) {number=numbers;}});mBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic 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
相关文章推荐
- 阅读《Android 从入门到精通》(32)——Bundle
- Android的Activity屏幕切换动画(一)-左右滑动切换
- Android 无需root实现apk的静默安装
- Android工程编译的时候报libmaliinstr.so not found的错误是为什么呢?
- Android中对Handle机制的理解
- Android如何使用API
- android的输入框EditText限制条件setFilters
- Android Java与C++的调用
- Android--Handler的使用方法:在子线程中更新界面
- android自定义控件之Dialog详解
- [置顶] android开发之当设置textview多少字后以省略号显示。限制TextView的字数
- android 二维码 helloworld (二维码的生成与扫描)
- [Android] ListView中getView的原理+如何在ListView中放置多个item
- Android开发_控制硬加速hardwareAccelerated
- 基于Android的ELF PLT/GOT符号重定向过程
- 在Android项目中,遇到过的坑
- android 图片缓存LruCache
- android五种布局模式
- 分享几个实用的Android开源项目(这是我摘抄的。。。o(╯□╰)o)
- Android Studio如何集成Genymotion