android 开发进阶 自定义控件-仿ios自动清除控件
先上图:
开发中经常需要自定义view控件或者组合控件,某些控件可能需要一些额外的配置。比如自定义一个标题栏,你可能需要根据不同尺寸的手机定制不同长度的标题栏,或者更常见的你需要配置标题栏的背景,这时候,你就会考虑到你写的view的扩展性问题,通常情况下,我们可以为这个自定义的标题栏加上一些setXXX方法,供外界调用,设置其颜色、长度等属性。但是我们都知道,在使用系统控件时,我们大多数情况下并不需要在代码中配置控件,而仅仅只需在布局文件中对控件宽、高、颜色等进行配置,这样做的好处就将UI与业务逻辑解耦,使代码更加清晰。
1.编写attrs属性文件。android在默认情况下并没有attrs.xml,我们需要手动在values目录下新建一个这样的文件。文件根结点是resources,子节点叫declare-styleable,比如下面就是一个attrs文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ClearEditTextView">
<attr name="icon_width" format="integer" />
<attr name="icon_height" format="integer" />
</declare-styleable>
</resources>
此处用来设置,EditText 右边的 清除按钮大小 宽,高
这个想必大家都不陌生,我这里需要继承EditText;
package com.example.clearbuttonedit; import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.animation.Animation;
import android.view.animation.CycleInterpolator;
import android.view.animation.TranslateAnimation;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.widget.EditText;
/**
* 继承的EditText.
*
* 对于自定义的View有EditText的属性,可以直接在xml文件中使用就可以了
* 如果含有自己独特的属性,那么就需要在构造函数中获取属性文件attrs.xml中自定义属性的名称
* 并根据需要设定默认值,放在在xml文件中没有定义。
* 如果使用自定义属性,那么在应用xml文件中需要加上新的schemas,
* 比如这里是xmlns:my="http://schemas.android.com/apk/res/com.example.clearbuttonedit"
* 其中xmlns后的“my”是自定义的属性的前缀,res后的是我们应用所在的包
* */
public class ClearEditText extends EditText implements
TextWatcher { private Drawable mClearIcon; private int clearIconWidth;
private int clearIconHeight; public ClearEditText(Context context) {
this(context, null); } public ClearEditText(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.editTextStyle); } public ClearEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr); mClearIcon = getCompoundDrawables()[2];
if (mClearIcon == null) {
mClearIcon =getResources().getDrawable(R.drawable.clear_btn);
}
getConfig(context, attrs);
init(); } /**
* 从xml中获取配置信息
*/
private void getConfig(Context context, AttributeSet attrs) {
// TODO Auto-generated method stub
//TypedArray是一个数组容器用于存放属性值
TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.ClearEditTextView); clearIconWidth = ta.getInt(R.styleable.ClearEditTextView_icon_width, mClearIcon.getIntrinsicWidth());
clearIconHeight = ta.getColor(R.styleable.ClearEditTextView_icon_height, mClearIcon.getIntrinsicHeight());
//用完务必回收容器
ta.recycle();
} private void init() {
mClearIcon.setBounds(0, 0, clearIconWidth,
clearIconHeight);
//设置控件,top bottom left right 图片(类似android:drawableLeft)
setCompoundDrawables(null, null, mClearIcon, null);
//先隐藏清除按钮
setClearIconVisible(false);
//监听EditText 内容改变
addTextChangedListener(this);
} protected void setClearIconVisible(boolean visible) {
Drawable right = visible ? mClearIcon : null;
setCompoundDrawables(getCompoundDrawables()[0],
getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
} @Override
public boolean onTouchEvent(MotionEvent event) {
if (getCompoundDrawables()[2] != null) {
if (event.getAction() == MotionEvent.ACTION_UP) {
boolean touchable = event.getX() > (getWidth()
- getPaddingRight() - clearIconWidth)
&& (event.getX() < ((getWidth() - getPaddingRight())));
if (touchable) {
this.setText("");
}
}
}
// TODO Auto-generated method stub
return super.onTouchEvent(event);
} //如果editText 控件文字大于 0 显示清除按钮
@Override
public void onTextChanged(CharSequence s, int start, int count, int after) {
setClearIconVisible(s.length() > 0);
} @Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) { } @Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub }
}
(2)使用 TextWatcher 监听 EditText内容变化,如果有文字就让清除按钮显示,没有就不显示;
3.编写布局文件,使用上面的自定义的控件。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:clearIcon="http://schemas.android.com/apk/res/com.example.clearbuttonedit"
android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainActivity" > <TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="登 录" /> <com.example.clearbuttonedit.ClearEditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:hint="请输入用户名"
android:layout_marginTop="20dp"
android:layout_below="@id/title"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
clearIcon:icon_width="30"
clearIcon:icon_height="30"
android:background="@drawable/login_edit_selector"
android:layout_centerHorizontal="true"
>
</com.example.clearbuttonedit.ClearEditText> <com.example.clearbuttonedit.ClearEditText
android:id="@+id/pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginTop="10dp"
android:layout_below="@id/username"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="请输入密码"
android:background="@drawable/login_edit_selector"
android:layout_centerHorizontal="true"
>
</com.example.clearbuttonedit.ClearEditText> </RelativeLayout>
需要注意的是,要想使用自定义属性,必须先加上命名空间,否则android不认识这些自定义属性。命名控件一般规则是 xmlns:[attrs中declare-styleable的name] =
"http://schemas.android.com/apk/res/包名",
4.在activity中通过 setContentView 加载布局
。
demo下载:
http://download.csdn.net/detail/q610098308/9299081
android 开发进阶 自定义控件-仿ios自动清除控件的更多相关文章
- Android开发技巧——定制仿微信图片裁剪控件
拍照--裁剪,或者是选择图片--裁剪,是我们设置头像或上传图片时经常需要的一组操作.上篇讲了Camera的使用,这篇讲一下我对图片裁剪的实现. 背景 下面的需求都来自产品. 裁剪图片要像微信那样,拖动 ...
- android 开发进阶自定义控件 类似 TextView
开发自定义控件的步骤: 1. 继承View: 2.重写构造函数并构造方法中获得我们自定义的属性. 3. 重写onDraw, 4.重写onMeasure 等函数 一.自定义View的属性,首先在res/ ...
- eclipse android开发,文本编辑xml文件,给控件添加ID后,R.java,不自动的问题。
直接编辑xml文件给控件添加id,不自动更新.原来的id写法:@id/et_tel 然后改写成这样:@+id/et_tel 然后就好了!操`1
- Android-PickerView【仿iOS的PickerView控件,并封装了时间选择和选项选择这两种选择器】使用
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 本文主要演示Android-PickerView的选项选择器.时间选择器的简单运用.由于每一个版本略有不用,所以实际使用方式以git ...
- Android 开发最牛的图片轮播控件,基本什么都包含了。
Android图片轮播控件 源码下载地址: Android 图片轮播 现在的绝大数app都有banner界面,实现循环播放多个广告图片和手动滑动循环等功能.因为ViewPager并不支持循环翻页, ...
- C# 自定义控件及引用自动义控件
1.http://www.cnblogs.com/hjxzjp/p/7823292.html 优先考虑从现有的控件中进行派生,并添加所需要的功能. 在解决方案资源管理器窗口中设置:引用----&g ...
- android开发 软键盘出现后 防止EditText控件遮挡 总体平移UI
在EditText控件接近底部的情况下 软键盘弹出后会把获得焦点的EditText控件遮挡 无法看到输入信息 防止这种情况发生 就须要设置AndroidManifest.xml的属性 前面的xml信 ...
- Android开发:在布局里移动ImageView控件
在做一个app时碰到需要移动一个图案的位置,查了一上午资料都没找到demo,自己写一个吧 RelativeLayout.LayoutParams lp = new RelativeLayout.Lay ...
- 学习笔记001之[Android开发视频教学].01_06_Android当中的常见控件
文本框,按钮 菜单按钮(需复写两个方法) 后续需完成联系代码.
随机推荐
- Java ArrayList操作
import java.util.ArrayList; import java.util.List; import java.util.Iterator; public class Study { p ...
- C\C++拾遗------C#程序员重温C\C++之要点
1.开发工具:建议采用VS2012及2013 1).因为VS2012对C++编码实现了自动编排格式(Ctrl E D) 2).提供智能感知(联想输入)功能 相比VS2010及以前版本对于习惯了智能感知 ...
- visual studio 编译时 出现 Files 的值 乱码
参考了:http://blog.163.com/jiang_tao_2010/blog/static/121126890201031031337332/ 最近在做程序时,在生成解决方法过程中,电脑出现 ...
- nginx的https配置
测试自签名的ssl证书 首先执行如下命令生成一个key openssl genrsa -des3 - 然后他会要求你输入这个key文件的密码.不推荐输入.因为以后要给nginx使用.每次reload ...
- golang自动导入postgresql脚本
直接代码 package main import ( "fmt" "golang-objective-go/dataFoundation/dataConvert" ...
- EventHandler委托与自定义委托
http://blog.csdn.net/uuxyz/article/details/7175248 EventHandler委托与自定义委托 自定义委托: //1. public delegate ...
- C和C++混合编程
extern "C"表示编译生成的内部符号名使用C约定.C++支持函数重载,而C不支持,两者的编译规则也不一样.函数被C++编译后在符号库中的名字与C语言的不 同.例如,假设某个函 ...
- POJ 3368 Frequent values RMQ 训练指南 好题
#include<cstdio> #include<cstring> ; const int inf=0x3f3f3f3f; inline int max(int x,int ...
- MYSQL 安装更新,使用,管理,备份和安全等
如何安装更新,使用,管理,备份和安全,维护优化一个MYSQL系统. 一.MYSQL发展历史,特点.对SQL语法进行介绍 二.如何安装一个MYSQL系统 三四.如何利用SQL语言以及其他的客户工具对MY ...
- [实变函数]2.5 Cantor 三分集
1 Cantor 三分集的构造: $$\bex P=\cap_{n=1}^\infty F_n. \eex$$ 2 Cantor 三分 ...