android EditText自定义开发实例

在学习android 开发的东西,对于自己开发的阶段性成果,进行记录下吧。EditText控件增加删除的功能。

主要的代码如下:

package com.cn;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
 
import android.widget.EditText;

public class  EditTextWithDel extends EditText implements OnFocusChangeListener,TextWatcher {
    /**
     * 删除按钮的图片
     */
    private Drawable delImg;
    //判断是否获取焦点
    private boolean hasFoucs;
   //构造函数
    public EditTextWithDel(Context context) {
        this(context, null);
        init();
    }
    //构造函数
    public EditTextWithDel(Context context, AttributeSet attrs) {
        // 这里构造方法也很重要,不加这个很多属性不能再XML里面定义
        this(context, attrs, android.R.attr.editTextStyle);
        init();
    }
    //构造函数
    public EditTextWithDel(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }
    private void init() {
        // 获取EditText的DrawableRight,假如没有设置我们就使用默认的图片,
    	//2是获得右边的图片  顺序是左上右下(0,1,2,3,)
        delImg = getCompoundDrawables()[2];
        if (delImg == null) {
            // throw new
            // 获取删除的图片资源,可以自己找一张图片放到drawable文件夹下 ;
            delImg = getResources().getDrawable(R.drawable.lastdelete);
        }

        delImg.setBounds(0, 0, delImg.getIntrinsicWidth(),delImg.getIntrinsicHeight());
        // 默认设置隐藏图标
        setClearIconVisible(false);
        // 设置焦点改变的监听   这个很重要
        setOnFocusChangeListener(this);
        // 设置输入框里面内容发生改变的监听 这个很重要
        addTextChangedListener(this);
    }

    /**
     * 因为我们不能直接给EditText设置点击事件,
     * 所以我们用记住我们按下的位置来模拟点击事件 
     * 当我们按下的位置 在 EditText的宽度 -
     * 图标到控件右边的间距 - 图标的宽度 和 EditText的宽度 - 图标到控件右边的间距之间我们就算点击了图标
     * 就是当我们按下的位置处于图片之内的时候,我们认为点击了图片,只考虑横向的位置
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (getCompoundDrawables()[2] != null) {
                boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight())
                      && 
                (event.getX() < ((getWidth() - getPaddingRight())));
                if (touchable) {
                    this.setText("");
                }
            }
        }
        return super.onTouchEvent(event);
    }

    /**
     * 当ClearEditText焦点发生变化的时候,判断里面字符串长度设置清除图标的显示与隐藏
     */
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        this.hasFoucs = hasFocus;
        if (hasFocus) {
            setClearIconVisible(getText().length() > 0);
        } else {
            setClearIconVisible(false);
        }
    }

    /**
     * 设置清除图标的显示与隐藏,调用setCompoundDrawables为EditText绘制上去
     * 
     * @param visible
     */
    protected void setClearIconVisible(boolean visible) {
        Drawable right = visible ? delImg : null;
        setCompoundDrawables(getCompoundDrawables()[0],getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
    }

    /**
     * 当输入框里面内容发生变化的时候回调的方法
     */
    @Override
    public void onTextChanged(CharSequence s, int start, int count, int after) {
        if (hasFoucs) {
            setClearIconVisible(s.length() > 0);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,int after) {

    }

    @Override
    public void afterTextChanged(Editable s) {

    }


}

配置main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
  >

<com.cn.EditTextWithDel
                android:id="@+id/etUserName"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
  >

                <requestFocus />
            </com.cn.EditTextWithDel>


 

</LinearLayout>
程序运行效果图:


本人的同一篇文章:http://jingyan.baidu.com/article/6d704a1316707b28db51ca29.html

链接:如果文章有所帮助,请点下


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值