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当中的常见控件
文本框,按钮 菜单按钮(需复写两个方法) 后续需完成联系代码.
随机推荐
- 导出resource文件的的资源
写个小工具,方便一次性将resource文件中的资源导出,不然反编译后一个个找,真是太麻烦了. using System;using System.Collections.Generic;using ...
- bzoj3135: [Baltic2013]pipesd
Description 有n个水库,m条管道.Jester会在某些管道中间凿开一个洞,让水流出来或者用水泵把水打进去.保证这个流速是偶数.对于一条管道(u, v),如果在中间凿开了一个洞让水流出来,流 ...
- bzoj2152 聪聪可可
Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一般情况下石头剪刀布就好 ...
- excel的常用公式
1,合并单元格 例子 B1="delete from table where id='"&A1&"';" 注意最好单元格为文本格式 1,去重复列 ...
- android学习笔记十——TabHost
TabHost——标签页 ==> TabHost,可以在窗口放置多个标签页,每个标签页相当于获得了一个与外部容器相同大小的组件摆放区域. 通过此种方式可以实现在一个容器放置更多组件(EG:通话记 ...
- linux 串口阻塞与非阻塞参数设置
在串口设置中,有以下两个参数可以决定是否阻塞. 在打开串口时不加O_NODELAY,可用下面的第二种方法,来进行阻塞/非阻塞的设定 c_cc[VTIME] 非规范模式读取时的超时时间(单位:百毫秒), ...
- jQuery formValidator手册
什么是jQuery formValidator? jQuery formValidator表单验证插件是客户端表单验证插件. 在做B/S开发的时候,我们经常涉及到很多表单验证,例如新用户注册,填写个人 ...
- Javascript金额转化
//"123,456.78"----> 123456.78(float格式) function rmoney(s) { return parseFloat(s.replace ...
- cf380D Sereja and Cinema 组合数学
time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...
- React Native 开发。
1.react-native run-android 安装 2.react-native start 开启调试端口