package com.egojit.android.sops.views.EditText;

 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.animation.Animation;
import android.view.animation.CycleInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.EditText; import com.asuka.android.asukaandroid.R; /**
* 备注:
* 作者:王莹
* 时间:2017/4/25.
*/ public class EditTextView 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;
private int mDrawablePadding = 16;
String myNamespace = "http://schemas.android.com/apk/res-auto"; public EditTextView(Context context) {
this(context, null);
} public EditTextView(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.editTextStyle);
} public EditTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle); edit_type = attrs.getAttributeValue(myNamespace,
"edit_type");
init();
} private void init() {
//获取EditText的DrawableRight,getCompoundDrawables()获取Drawable的四个位置的数组
mClearDrawable = getCompoundDrawables()[2];
if (mClearDrawable == null) {
if (edit_type.equals("name")) {
// 默认显示的是删除按钮
mClearDrawable = getResources().getDrawable(R.drawable.delete);
} else if (edit_type.equals("password")) {
// 默认显示的是明文密文按钮
mClearDrawable = getResources().getDrawable(R.drawable.icon_eye);
}
}
//设置图标的位置以及大小,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 != null) {
if (edit_type.equals("name")) {
this.setText("");
} else if (edit_type.equals("password")) {
if (isSee) {
//设置不可见
this.setInputType(PASSWORD_MIWEN);//密文
this.setSelection(this.length());//设置光标显示
mClearDrawable = getResources().getDrawable(R.drawable.loginmiwen);
setIcon(mClearDrawable);
} else {
//设置可见
this.setInputType(PASSWORD_MINGWEN);//明文
this.setSelection(this.length());//设置光标显示
mClearDrawable = getResources().getDrawable(R.drawable.loginmingwen);
setIcon(mClearDrawable);
}
isSee = !isSee;
}
} else {
//默认为进行删除操作
this.setText("");
}
}
}
}
return super.onTouchEvent(event);
} private void setIcon(Drawable mDeleIcon) {
//设置图标的位置以及大小,getIntrinsicWidth()获取显示出来的大小而不是原图片的大小
mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth() + 10, mClearDrawable.getIntrinsicHeight() + 10);
setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[0], mDeleIcon, getCompoundDrawables()[0]);
}
}

EditTextView的更多相关文章

  1. android TextView EditTextView一些技巧使用 (视图代码布局)

    android TextView 是最常用的控件 可以用作普通的显示,还可以用作有显示文字的按钮,用作有显示图片的图文组合 1. 图文组合 xml 中: <TextView android:id ...

  2. 查找后去掉EditTextView的焦点

    //在按钮点击事件里处理 bt_search.setOnClickListener(new OnClickListener() { public void onClick(View v) {      ...

  3. iOS 之UITextFiled/UITextView小结

    一:编辑被键盘遮挡的问题 参考自:http://blog.csdn.net/windkisshao/article/details/21398521 1.自定方法 ,用于移动视图 -(void)mov ...

  4. Android App的设计架构:MVC,MVP,MVVM与架构经验谈

    相关:http://www.cnblogs.com/wytiger/p/5996876.html 和MVC框架模式一样,Model模型处理数据代码不变在Android的App开发中,很多人经常会头疼于 ...

  5. Android 中常见控件的介绍和使用

    1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.la ...

  6. 一个简单的Android富文本TextView实现

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px Helvetica; color: #555555 } p.p2 { margin: 0.0p ...

  7. [转]框架模式 MVC 在Android中的使用

    算来学习Android开发已有2年的历史了,在这2年的学习当中,基本掌握了Android的基础知识.越到后面的学习越感觉困难,一来是自认为android没啥可学的了(自认为的,其实还有很多知识科学), ...

  8. 框架模式 MVC 在Android中的使用

    算来学习Android开发已有2年的历史了,在这2年的学习当中,基本掌握了Android的基础知识.越到后面的学习越感觉困难,一来是自认为android没啥可学的了(自认为的,其实还有很多知识科学), ...

  9. Android 里面的小坑

    1.listview加了个blockdescen,竟然导致editTextView不能获取焦点

随机推荐

  1. [Typescript] Make TypeScript Class Usage Safer with Strict Property Initialization

    By setting the strictPropertyInitialization flag in the .tsconfig file, TypeScript will start throwi ...

  2. hdu5289 Assignment (区间查询最大值最小值,st算法...)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5289 题意:给定长度为n的序列a和一个整数K,找出最大值和最小值的差值小于K的区间.输出满足条件的区间的个 ...

  3. 爪哇国新游记之二十九----访问URL获取输入流

    代码: import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import ...

  4. require.js - 详解

    测试结构如下 index.html <!DOCTYPE html> <html lang="en"> <head> <meta chars ...

  5. Android自己定义(三)实现圆盘的百分比设置

    近期一直在学习自己定义控件,昨天看到群里有人问怎样怎样实现圆盘样式的显示,学有所用,于是乎就有了这篇博客 先上图,一目了然 这里的显示颜色以及颜色块的大小你都能够自己设置 这里设置了三种颜色,相应三种 ...

  6. Vue 前端路由 vue-router

    1.前端路由 后端路由:多页面,服务器端渲染好返回给浏览器. 前端路由:改变url不向服务器发送请求:前端可以监听url变化:前端可以解析url并执行相应操作. 前后端分离:后端只提供API来返回数据 ...

  7. VirtualBox实现宿主机和虚拟机之间网络的通讯

    摘要:实现宿主机和虚拟机之间网络的通讯 环境: 宿主机操作系统            WindowsXP 虚拟机软件                    VirtualBox 虚拟机操作系统     ...

  8. Spring使用经验之Listener综述

    Note:Spring使用版本是4.1.6.RELEASE 1. ContextLoaderListener最基本的SpringListener,加载Spring配置文件 配置名为contextCon ...

  9. C++ Primer Plus的若干收获--(二)

    哎,真是不想吐槽考驾照的艰辛历程了.跑到大西郊,顶着大太阳,一天就能摸上个十几分钟二十分钟的车,简直不要太坑爹,这两天真是做的我屁股疼的不行. .. 今天果断不去了.仅仅可惜我的大阿根廷啊,坚持到最后 ...

  10. JanusGraph的schema及数据建模

    每个JanusGraph都有一个schema,该schema由edge labels, property keys和vertex labels组成.JanusGraph的schema可以显式或隐式创建 ...