经过大神指导,上面封装的还不够全面,触摸事件应该也放进自定义中去,那么问题来了,怎么区分呢!,这就涉及到了自定义属性的介绍了

我通过设置属性来判断在onTouch事件中应该进行什么操作,接下来看看改良的代码

首先在attrs中设置样式

<declare-styleable name="drawrighteditview"> <attr name="edit_type" format="string" /> </declare-styleable>

然后在布局中设置样式,先导入xmlns:attrs="http://schemas.android.com/apk/res-auto"

  <com.asuka.android.asukaandroid.demo.views.DrawRightEditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/iv1"
android:background="@null"
android:hint="请输入账号"
android:textColor="#000"
//设置属性
attrs:edit_type="name"
android:drawableRight="@drawable/icon_delete"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textColorHint="#8d8d94"
android:textSize="16sp" />

再来看看自定义代码获取属性,实现在OnToch事件的操作

 public class DrawRightEditText extends EditText {
private Drawable mClearDrawable;
private String edit_type;
private boolean isSee = false;//密码是否可见
private static final int PASSWORD_MINGWEN = 0x90;
private static final int PASSWORD_MIWEN = 0x81;
String myNamespace = "http://schemas.android.com/apk/res-auto";
public DrawRightEditText(Context context) {
this(context, null);
} public DrawRightEditText(Context context, AttributeSet attrs) {
//这里构造方法也很重要,不加这个很多属性不能再XML里面定义
this(context, attrs, android.R.attr.editTextStyle);
} public DrawRightEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
//获取属性
20 edit_type = attrs.getAttributeValue(myNamespace,
21 "edit_type");
init();
}
private void init(){
//获取EditText的DrawableRight,getCompoundDrawables()获取Drawable的四个位置的数组
mClearDrawable = getCompoundDrawables()[2];
//设置图标的位置以及大小,getIntrinsicWidth()获取显示出来的大小而不是原图片的带小
mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth()+10, mClearDrawable.getIntrinsicHeight()+10);
//默认设置隐藏图标
setClearIconVisible(false);
//设置输入框里面内容发生改变的监听
addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
setClearIconVisible(s.length() > 0);
} @Override
public void afterTextChanged(Editable s) { }
});
} /**
* 设置清除图标的显示与隐藏,调用setCompoundDrawables为EditText绘制上去
* @param visible
*/
protected void setClearIconVisible(boolean visible) {
Drawable right = visible ? mClearDrawable : null;
setCompoundDrawables(getCompoundDrawables()[0],
getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
}
/**
* 设置晃动动画
*/
public void setShakeAnimation(){
this.startAnimation(shakeAnimation(3));
}
/**
* 晃动动画
* @param counts 1秒钟晃动多少下
* @return
*/
public static Animation shakeAnimation(int counts){
Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0);
translateAnimation.setInterpolator(new CycleInterpolator(counts));
translateAnimation.setDuration(1000);
return translateAnimation;
} @Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
if (getCompoundDrawables()[2] != null) {
//getTotalPaddingRight()图标左边缘至控件右边缘的距离
//getWidth() - getTotalPaddingRight()表示从最左边到图标左边缘的位置
//getWidth() - getPaddingRight()表示最左边到图标右边缘的位置
boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight())
&& (event.getX() < ((getWidth() - getPaddingRight()))); if (touchable) {
//根据属性判断操作
if (edit_type.equals("name")){
89 this.setText("");
90 }else if (edit_type.equals("password")){
91 if (isSee) {
92 //设置不可见
93 this.setInputType(PASSWORD_MIWEN);//密文
94 this.setSelection(this.length());//设置光标显示
95 } else {
96 //设置可见
97 this.setInputType(PASSWORD_MINGWEN);//明文
98 this.setSelection(this.length());//设置光标显示
99 }
100 isSee = !isSee;
101 }
}
}
}
return super.onTouchEvent(event);
}
}

最新文章

  1. Qt实现小功能之列表无限加载
  2. JsRender语法
  3. 第六课,T语言表达式(版本5.0)
  4. 我是面试官--&quot;自我介绍&quot;
  5. ASP.NET 查询客户端请求IP地址
  6. oracle 性能优化--索引总结
  7. zoj3747(递推dp)
  8. nodejs笔记之路由及util和url模块
  9. bzoj 3674: 可持久化并查集加强版 (启发式合并+主席树)
  10. Windows10环境下使用VisualSVN server搭建SVN服务器
  11. ArcPy批量计算Mean Center的两个实例
  12. Codeforces Round #207 (Div. 1) A. Knight Tournament (线段树离线)
  13. 为了应对异常情况,提供最原始的python第三方库的安装方法:手动安装。往往是Windows用户需要用到这种方法。
  14. 浅析&quot;WeixinJSBridge is not defined&quot;
  15. Repository HDU2846
  16. 项目导入时报错:The import javax.servlet.http.HttpServletRequest cannot be resolved 解决方法
  17. npm install &quot;Unexpected end of JSON input while parsing near&quot;问题
  18. Linux Shell nohup命令用法
  19. Linux更换jdk版本,java -version还是原来的版本问题
  20. 转载FPGA学习之内嵌乘法器调用

热门文章

  1. JSON-JSON 百科
  2. python核心编程学习记录之Web编程
  3. C++ 如何得到当前进程所占用的内存呢?【转】
  4. cbuffer padding
  5. 关于国内外CV领域牛人的博客链接 .
  6. nginx资源争夺问题
  7. 用Fiddler或Charles进行mock数据搭建测试环境
  8. UVA10312- Expression Bracketing(Catalan + 递推)
  9. SQL语句练习手册--第一篇
  10. ubuntu 备份清理tomcat日志,并且删除七天之前的日志