欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 开发仿简书登录框可删除内容或显示密码框的内容

程序员文章站 2024-02-26 14:48:46
简书app 是我很喜欢的一款软件。今天就模仿了一下他的登录框。先上图: 好了下面上代码,自定义imgedittext 继承与edittext。重写一些方法。...

简书app 是我很喜欢的一款软件。今天就模仿了一下他的登录框。先上图:

Android 开发仿简书登录框可删除内容或显示密码框的内容

好了下面上代码,自定义imgedittext 继承与edittext。重写一些方法。

package lyf.myimgedittextdemo;
import android.content.context;
import android.graphics.rect;
import android.graphics.drawable.drawable;
import android.text.editable;
import android.text.textwatcher;
import android.util.attributeset;
import android.view.motionevent;
import android.widget.edittext;
/**
* lyf on 2016/12/6.
* 自定义的edittext右边带图片,可以设置点击事件
*/
public class imgedittext extends edittext implements textwatcher {
//控件左边的图片
private drawable leftdrawable = null;
//控件右边的图片
private drawable rightdrawable = null;
// 控件是否有焦点
private boolean hasfoucs;
private imyrightdrawableclick mightdrawableclick;
public imgedittext(context context) {
this(context, null);
}
public imgedittext(context context, attributeset attrs) {
//这里构造方法也很重要,不加这个很多属性不能再xml里面定义
this(context, attrs, android.r.attr.edittextstyle);
}
public imgedittext(context context, attributeset attrs, int defstyleattr) {
super(context, attrs, defstyleattr);
init();
}
//初始化基本图片
private void init() {
//获取radiobutton的图片集合
drawable[] drawables = getcompounddrawables();
leftdrawable = drawables[0];
rightdrawable = drawables[2];
setcompounddrawableswithintrinsicbounds(leftdrawable, null, null, null);
//设置输入框里面内容发生改变的监听
addtextchangedlistener(this);
}
//设置显示图片的大小
public void setcompounddrawableswithintrinsicbounds(drawable left, drawable top, drawable right, drawable bottom) {
super.setcompounddrawableswithintrinsicbounds(left, top, right, bottom);
//这里只要改后面两个参数就好了,一个宽一个是高,如果想知道为什么可以查找源码
if (left != null) {
left.setbounds(0, 0, 50, 50);
}
if (right != null) {
right.setbounds(0, 0, 50, 50);
}
if (top != null) {
top.setbounds(0, 0, 100, 100);
}
if (bottom != null) {
bottom.setbounds(0, 0, 100, 100);
}
setcompounddrawables(left, top, right, bottom);
}
//光标选中时判断
@override
protected void onfocuschanged(boolean focused, int direction, rect previouslyfocusedrect) {
super.onfocuschanged(focused, direction, previouslyfocusedrect);
this.hasfoucs = focused;
if (focused) {
setimagevisible(gettext().length() > 0);
} else {
setimagevisible(false);
}
}
//设置清除图标的显示与隐藏,调用setcompounddrawables为edittext绘制上去
protected void setimagevisible(boolean flag) {
//如果当前右侧有图片则覆盖右侧的图片,如果没有还是显示原来的图片
if (getcompounddrawables()[2] != null) {
rightdrawable = getcompounddrawables()[2];
}
if (flag) {
setcompounddrawables(getcompounddrawables()[0], null, rightdrawable, null);
} else {
setcompounddrawables(getcompounddrawables()[0], null, null, null);
}
}
//文本框监听事件
@override
public void ontextchanged(charsequence text, int start, int lengthbefore, int lengthafter) {
if (hasfoucs) {
if (text.length() > 0) {
setimagevisible(true);
} else {
setimagevisible(false);
}
}
}
public void beforetextchanged(charsequence s, int start, int count, int after) {
}
public void aftertextchanged(editable s) {
}
/**
* 因为我们不能直接给edittext设置点击事件,所以我们用记住我们按下的位置来模拟点击事件
* 当我们按下的位置 在 edittext的宽度 - 图标到控件右边的间距 - 图标的宽度 和
* edittext的宽度 - 图标到控件右边的间距之间我们就算点击了图标,竖直方向就没有考虑
* (参考 http://blog.csdn.net/xiaanming/article/details/11066685/)
*/
@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) {
//调用点击事件(外部实现)
mightdrawableclick.rightdrawableclick();
}
}
}
return super.ontouchevent(event);
}
//设置右侧按钮的点击事件,外部调用的时候实现该方法
public void setdrawableclick( imyrightdrawableclick mymightdrawableclick){
this.mightdrawableclick = mymightdrawableclick;
}
//自定义接口(实现右边图片点击事件)
public interface imyrightdrawableclick {
void rightdrawableclick();
}
//允许外部修改右侧显示的图片
public void setrightdrawable(drawable drawable){
rightdrawable = drawable;
setcompounddrawableswithintrinsicbounds(leftdrawable, null, rightdrawable, null);
}
}

以上就是自定义类的主要代码了,注释比较清楚。

布局布局文件里直接引用就好。

<lyf.myimgedittextdemo.imgedittext
android:id="@+id/pwdiet"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:drawableleft="@mipmap/mm_image"
android:drawableright="@mipmap/eye_normal"
android:paddingleft="15dp"
android:paddingright="15dp"
android:paddingtop="5dp"
android:drawablepadding="15dp"
android:layout_margintop="10dp"
android:layout_marginbottom="10dp"
android:hint="密码"
android:inputtype="numberpassword" />

下面看代码中的设置

pwdiet = (imgedittext) this.findviewbyid(r.id.pwdiet);
pwdiet.setdrawableclick(new imgedittext.imyrightdrawableclick() {
@override
public void rightdrawableclick() {
if (ishidden) {
//设置edittext文本为可见的
pwdiet.settransformationmethod(hidereturnstransformationmethod.getinstance());
pwdiet.setrightdrawable(getresources().getdrawable(r.mipmap.eye_selected));
} else {
//设置edittext文本为隐藏的
pwdiet.settransformationmethod(passwordtransformationmethod.getinstance());
pwdiet.setrightdrawable(getresources().getdrawable(r.mipmap.eye_normal));
}
ishidden = !ishidden;
pwdiet.postinvalidate();
//切换后将edittext光标置于末尾
charsequence charsequence = pwdiet.gettext();
if (charsequence instanceof spannable) {
spannable spantext = (spannable) charsequence;
selection.setselection(spantext, charsequence.length());
}
}
});

这样我们的例子就完成了。

以上所述是小编给大家介绍的android 开发仿简书登录框可删除内容或显示密码框的内容,希望对大家有所帮助