Android 自定义控件

用到的图片文件:

平时都是用简单的控件通过布局来组合一大堆控件,界面复杂起来的话,布局文件就很大,维护起来也很烦。就想把常用的控件组合写成自定义控件,这样维护起来也方便,代码也清爽,下面就以带消除按钮的EditText为例。平时,EditText删除输入的都是按好多下删除键的,看到很多应用的输入框,在输入内容后,会跳出一个清空按钮,点击一下,输入的都会清除,这个用户体验很好。自己尝试了一下,原先是用最简单的控件组合,发现代码量太大,重用性不高,所以想把这个封装成一个自定义控件,直接调用。直接上代码。

    EditTextWithClearButton.java

1import xidian.wwf.temperature.R; 2import xidian.wwf.temperature.util.StringUtil; 3import android.content.Context; 4import android.content.res.TypedArray; 5import android.text.Editable; 6import android.text.TextWatcher; 7import android.util.AttributeSet; 8import android.view.LayoutInflater; 9import android.view.View; 10import android.widget.EditText; 11import android.widget.ImageButton; 12import android.widget.LinearLayout; 13 14/** 15 * 带清除按钮的输入框 16 * @author WWF 17 */ 18public class EditTextWithClearButton extends LinearLayout{ 19 20 private EditText editText; 21 private ImageButton clearImageButton; 22 23 public EditTextWithClearButton(Context context) { 24 super(context); 25 } 26 27 public EditTextWithClearButton(Context context,AttributeSet attrs){ 28 super(context, attrs); 29 init(context,attrs); 30 } 31 32 /** 33  * 初始化 34  */ 35 public void init(Context context,AttributeSet attrs){ 36 View view = LayoutInflater.from(context).inflate(R.layout.weight_edit_with_clear, this, true); 37 editText = (EditText) view.findViewById(R.id.et); 38 clearImageButton = (ImageButton) view.findViewById(R.id.clear_ib); 39 clearImageButton.setOnClickListener(new OnClickListener() { 40 @Override 41 public void onClick(View v) { 42 editText.setText(""); 43 } 44 }); 45 editText.addTextChangedListener(new TextWatcher() { 46 47 @Override 48 public void onTextChanged(CharSequence s, int start, int before, int count) { 49 if (s.length() > 0) { 50 clearImageButton.setVisibility(View.VISIBLE); 51 } else { 52 clearImageButton.setVisibility(View.GONE); 53 } 54 } 55 56 @Override 57 public void beforeTextChanged(CharSequence s, int start, int count, 58 int after) { 59 60 } 61 @Override 62 public void afterTextChanged(Editable s) { 63 64 } 65 }); 66 //将属性值设置到控件中 67 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.EditWithClearText); 68 CharSequence hint = a.getText(R.styleable.EditWithClearText_hint); 69 CharSequence text = a.getText(R.styleable.EditWithClearText_text); 70 if (text!=null&&!StringUtil.isEmpty(text.toString().trim())) { 71 editText.setText(text); 72 //设置光标位置 73 editText.setSelection(text.length()); 74 this.clearImageButton.setVisibility(View.VISIBLE); 75 }else { 76 if (hint!=null&&!StringUtil.isEmpty(hint.toString().trim())) { 77 editText.setHint(hint); 78 } 79 } 80 a.recycle(); 81 } 82 83 /** 84  * 获得输入的值 85  * @return 86  */ 87 public String getText(){ 88 return this.editText.getText().toString(); 89 } 90 91 /** 92  * 设置值 93  * @param text 94  */ 95 public void setText(String text){ 96 this.editText.setText(text); 97 } 98 99 /** 100  * 设置默认值 101  * @param hint 102  */ 103 public void setHint(String hint){ 104 this.editText.setHint(hint); 105 } 106 107 /** 108  * 获得输入框控件 109  * @return 110  */ 111 public EditText getEditText(){ 112 return this.editText; 113 } 114 115 /** 116  * 获得消除按钮 117  * @return 118  */ 119 public ImageButton getClearImageButton(){ 120 return this.clearImageButton; 121 } 122}

weight_edit_with_clear.xml 自定义控件布局文件

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3    android:layout_width="fill_parent" 4    android:layout_height="wrap_content" 5    android:background="@null" 6    android:orientation="horizontal" > 7 8    <EditText 9        android:id="@+id/et" 10        style="@style/text_normal" 11        android:layout_width="0dp" 12        android:layout_height="wrap_content" 13        android:layout_weight="1" 14        android:background="@null" 15        android:gravity="center_vertical" 16        android:maxLength="20" 17        android:paddingLeft="5dp" 18        android:paddingRight="4dp" 19        android:singleLine="true" /> 20 21    <ImageButton 22        android:id="@+id/clear_ib" 23        android:layout_width="wrap_content" 24        android:layout_height="wrap_content" 25        android:background="@null" 26        android:contentDescription="@string/image_content" 27        android:src="@drawable/common_input_box_clear" 28        android:visibility="gone" /> 29 30</LinearLayout>

这个布局文件就是自定义控件组合的布局文件,我采用线性布局,然后将EditText 和ImageButton 组合起来。

属性配置文件 attrs.xml

1<?xml version="1.0" encoding="utf-8"?> 2<resources> 3    <declare-styleable name="EditTextWithClearBitton"> 4        <attr name="text"  format="string"/> 5        <attr name="hint"  format="string" /> 6    </declare-styleable> 7</resources>

Activity 布局文件

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3    xmlns:wwf="http://schemas.android.com/apk/res/xidian.wwf.temperature" 4    android:layout_width="fill_parent" 5    android:layout_height="fill_parent" 6    android:background="@color/white" 7    android:orientation="vertical" > 8 9 10    <LinearLayout 11        android:layout_width="fill_parent" 12        android:layout_height="wrap_content" 13        android:background="@drawable/table_above_nor" 14        android:gravity="center" 15        android:orientation="horizontal" 16        android:padding="10dp" 17        android:layout_margin="10dp" > 18 19        <TextView 20            style="@style/text_normal" 21            android:layout_width="wrap_content" 22            android:layout_height="wrap_content" 23            android:layout_gravity="center_vertical" 24            android:layout_marginLeft="10dp" 25            android:singleLine="true" 26            android:text="@string/account" 27            android:textStyle="bold" /> 28 29        <xidian.wwf.temperature.weight.EditTextWithClearButton 30            android:id="@+id/ssss" 31            android:layout_width="match_parent" 32            android:layout_height="wrap_content" 33            android:layout_gravity="center_vertical"  34            wwf:text="@string/system_error" 35            > 36        </xidian.wwf.temperature.weight.EditTextWithClearButton> 37    </LinearLayout> 38 39</LinearLayout>

引入我们自定义的属性,需在布局文件中加入上面的

xmlns:wwf="http://schemas.android.com/apk/res/项目包名"

在控件初始化中,获得属性值,赋给控件即可,赋值的代码

1//将属性值设置到控件中 2TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.EditTextWithClearBitton); 3CharSequence hint = a.getText(R.styleable.EditTextWithClearBitton_hint); 4CharSequence text = a.getText(R.styleable.EditTextWithClearBitton_text); 5if (text!=null&&!StringUtil.isEmpty(text.toString().trim())) { 6 editText.setText(text); 7 //设置光标位置 8 editText.setSelection(text.length()); 9 this.clearImageButton.setVisibility(View.VISIBLE); 10}else { 11 if (hint!=null&&!StringUtil.isEmpty(hint.toString().trim())) { 12 editText.setHint(hint); 13 } 14} 15a.recycle();

然后就可以使用这个自定义控件了

下面是我运行的结果

点赞
收藏

评论区

加载中...

相关推荐

Android开源控件ViewPager Indicator的使用方法

AndroidViewpagerIndicator是Android开发中最常用的控件之一,几乎所有的新闻类APP中都有使用,下面介绍其基本使用方法。!(http://static.oschina.net/uploads/img/201403/14220432_afb0.png)1\.ViewPagerIndicator的Lib

Android控件在xml中初始化

一、写在前面界面控件的初始化一般通过findViewByid来查找绑定再强制转换,这项工作只是个纯体力活没有任何营养,一般常用的是使用匿名内部类的方式:首先需要获取到layout中布局页面的Button控件中指定的Id:android:id"";之后为这样按钮绑定监听器,使用匿名内部类的方式,代码如下:

Android开发问题汇总

1.ClassCastException异常是类型匹配出现的错误,xml布局文件中的控件id在Activity中匹配错误2.eclipse中遇到logcat无任何信息输出解决办法:windowshowview选择android下的devices,打开devices,点击右边的截屏图片。

Android 自定义组合控件 简单导航栏

最近在做项目的过程中,发现项目中好多界面的导航栏都很类似或者一样,但是每次都要重复写同样的代码,觉得很不爽,所以就简单地自定义了一下导航栏控件.先上图:!导航栏(http://static.oschina.net/uploads/img/201510/09104048_sODx.png)(https://www.oschina.net/

Android开发之列表控件

一、基础知识:ListView是一个经常用到的控件,ListView里面的每个子项Item可以使一个字符串,也可以是一个组合控件。先说说ListView的实现:1.准备ListView要显示的数据;2.使用一维或多维动态数组保存数据;3.构建适配器,简单地来说,适配器就是Item数组,动态数组有多少元素就生成多少个Item;4.把适配器添

IOS开发

有没有这种需求,自定一个panel,里面放了好几个控件,在多个页面用到这个panel。解决这个问题有三条思路:1.自己继承UIView写一个类,在这里面以代码的形式添加需要的控件,完成布局。2.使用XIB布局文件完成布局3.使用storyboard完成布局在这三中方式中,1显得高端大气上档次,哗啦哗啦敲半天。虽然我是技术控,但是也很反感这

Android 自定义控件 - HelloWorld