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. golang中的那些坑之迭代器中的指针使用

    今天在编写代码的时候,遇到了一个莫名其妙的错误,debug了半天,发现这是一个非常典型且易犯的错误.记之 示例代码: package main import "fmt" type ...

  2. [Parcel] Running TypeScript with parcel-bundler

    TO get started with TypeScirpt quickly in your local computer is using parcel-bunlder: npm i -g parc ...

  3. 使用pip安装tensorflow 0.80,python 使用tensorflow 0.80遇到的问题及处理方法

    http://blog.csdn.net/levy_cui/article/details/51251095 1.python 版本切换到2.7 推荐使用pythonbrew,http://blog. ...

  4. Android平台Camera实时滤镜实现方法探讨(十)--代码地址以及简单介绍(20160118更新)

    简单做了个相机和图片编辑模块,时间原因非常多功能还没有做.尚有BUG,见谅,将在以后抽时间改动 代码地址 PS:请点个Star^-^ --------------------------------- ...

  5. python abstractmethod 对象比较

    from functools import total_ordering from abc import ABCMeta,abstractmethod @total_ordering class Sh ...

  6. 【React Native开发】React Native控件之Image组件解说与美团首页顶部效果实例(10)

    ),React Native技术交流4群(458982758),欢迎各位大牛,React Native技术爱好者增加交流!同一时候博客左側欢迎微信扫描关注订阅号,移动技术干货,精彩文章技术推送! Im ...

  7. ROS示例----导航功能包Husky_exploration

    ROS导航功能包示例husky amcl gmapping slam exploration 此功能包包含如下文件: 结构如下: $ tree -L 2 . ├── CMakeLists.txt -& ...

  8. Activity具体解释(生命周期、启动方式、状态保存,全然退出等)

    一.什么是Activity? 简单的说:Activity就是布满整个窗体或者悬浮于其它窗体上的交互界面. 在一个应用程序中通常由多个Activity构成,都会在Manifest.xml中指定一个主的A ...

  9. C# NPOI操作Excel(下)

    根据自己项目需求编写,仅供参考 个人建议:使用Excel模板进行导出操作.尽量避免自己生成Excel(既繁琐又容易出BUG).大多情况下导出Excel都是固定格式,使用模板导出会方便很多. publi ...

  10. linux后台运行命令

    Ctrl+z/bg/nohup/setsid/& screen 区别待续