Android 開發(fā)仿簡(jiǎn)書登錄框可刪除內(nèi)容或顯示密碼框的內(nèi)容
簡(jiǎn)書App 是我很喜歡的一款軟件。今天就模仿了一下他的登錄框。先上圖:

好了下面上代碼,自定義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右邊帶圖片,可以設(shè)置點(diǎn)擊事件
*/
public class ImgEditText extends EditText implements TextWatcher {
//控件左邊的圖片
private Drawable leftDrawable = null;
//控件右邊的圖片
private Drawable rightDrawable = null;
// 控件是否有焦點(diǎn)
private boolean hasFoucs;
private IMyRightDrawableClick mightDrawableClick;
public ImgEditText(Context context) {
this(context, null);
}
public ImgEditText(Context context, AttributeSet attrs) {
//這里構(gòu)造方法也很重要,不加這個(gè)很多屬性不能再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);
//設(shè)置輸入框里面內(nèi)容發(fā)生改變的監(jiān)聽
addTextChangedListener(this);
}
//設(shè)置顯示圖片的大小
public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) {
super.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
//這里只要改后面兩個(gè)參數(shù)就好了,一個(gè)寬一個(gè)是高,如果想知道為什么可以查找源碼
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);
}
//光標(biāo)選中時(shí)判斷
@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);
}
}
//設(shè)置清除圖標(biāo)的顯示與隱藏,調(diào)用setCompoundDrawables為EditText繪制上去
protected void setImageVisible(boolean flag) {
//如果當(dāng)前右側(cè)有圖片則覆蓋右側(cè)的圖片,如果沒有還是顯示原來的圖片
if (getCompoundDrawables()[2] != null) {
rightDrawable = getCompoundDrawables()[2];
}
if (flag) {
setCompoundDrawables(getCompoundDrawables()[0], null, rightDrawable, null);
} else {
setCompoundDrawables(getCompoundDrawables()[0], null, null, null);
}
}
//文本框監(jiān)聽事件
@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) {
}
/**
* 因?yàn)槲覀儾荒苤苯咏oEditText設(shè)置點(diǎn)擊事件,所以我們用記住我們按下的位置來模擬點(diǎn)擊事件
* 當(dāng)我們按下的位置 在 EditText的寬度 - 圖標(biāo)到控件右邊的間距 - 圖標(biāo)的寬度 和
* EditText的寬度 - 圖標(biāo)到控件右邊的間距之間我們就算點(diǎn)擊了圖標(biāo),豎直方向就沒有考慮
* (參考 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) {
//調(diào)用點(diǎn)擊事件(外部實(shí)現(xiàn))
mightDrawableClick.rightDrawableClick();
}
}
}
return super.onTouchEvent(event);
}
//設(shè)置右側(cè)按鈕的點(diǎn)擊事件,外部調(diào)用的時(shí)候?qū)崿F(xiàn)該方法
public void setDrawableClick( IMyRightDrawableClick myMightDrawableClick){
this.mightDrawableClick = myMightDrawableClick;
}
//自定義接口(實(shí)現(xiàn)右邊圖片點(diǎn)擊事件)
public interface IMyRightDrawableClick {
void rightDrawableClick();
}
//允許外部修改右側(cè)顯示的圖片
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" />
下面看代碼中的設(shè)置
pwdIet = (ImgEditText) this.findViewById(R.id.pwdIet);
pwdIet.setDrawableClick(new ImgEditText.IMyRightDrawableClick() {
@Override
public void rightDrawableClick() {
if (isHidden) {
//設(shè)置EditText文本為可見的
pwdIet.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
pwdIet.setRightDrawable(getResources().getDrawable(R.mipmap.eye_selected));
} else {
//設(shè)置EditText文本為隱藏的
pwdIet.setTransformationMethod(PasswordTransformationMethod.getInstance());
pwdIet.setRightDrawable(getResources().getDrawable(R.mipmap.eye_normal));
}
isHidden = !isHidden;
pwdIet.postInvalidate();
//切換后將EditText光標(biāo)置于末尾
CharSequence charSequence = pwdIet.getText();
if (charSequence instanceof Spannable) {
Spannable spanText = (Spannable) charSequence;
Selection.setSelection(spanText, charSequence.length());
}
}
});
這樣我們的例子就完成了。
以上所述是小編給大家介紹的Android 開發(fā)仿簡(jiǎn)書登錄框可刪除內(nèi)容或顯示密碼框的內(nèi)容,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android設(shè)計(jì)登錄界面、找回密碼、注冊(cè)功能
- 在Nginx用htpasswd對(duì)網(wǎng)站進(jìn)行密碼保護(hù)的設(shè)置方法
- 頁面使用密碼保護(hù)代碼
- 設(shè)置密碼保護(hù)的SqlServer數(shù)據(jù)庫(kù)備份文件與恢復(fù)文件的方法
- Android開發(fā)之登錄驗(yàn)證實(shí)例教程
- Android集成新浪微博第三方登錄的方法
- Android開發(fā)之注冊(cè)登錄方法示例
- Android實(shí)現(xiàn)登錄功能demo示例
- Android調(diào)用第三方QQ登錄代碼分享
- Android登錄時(shí)密碼保護(hù)功能
相關(guān)文章
Android入門之TabHost與TabWidget實(shí)例解析
這篇文章主要介紹了Android入門之TabHost與TabWidget,對(duì)于Android初學(xué)者有一定的學(xué)習(xí)借鑒價(jià)值,需要的朋友可以參考下2014-08-08
Flutter 使用cached_image_network優(yōu)化圖片加載體驗(yàn)
在 Flutter 中,cached_image_network 即提供了緩存網(wǎng)絡(luò)圖片功能,同時(shí)還提供了豐富的加載過程指示。本文就來看下cached_image_network的具體使用2021-05-05
Android應(yīng)用實(shí)現(xiàn)點(diǎn)擊按鈕震動(dòng)
這篇文章主要為大家詳細(xì)介紹了Android應(yīng)用實(shí)現(xiàn)點(diǎn)擊按鈕震動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
Android?Flutter制作一個(gè)修改組件屬性的動(dòng)畫
flutter為我們提供了一個(gè)AnimationController來對(duì)動(dòng)畫進(jìn)行詳盡的控制,不過直接是用AnimationController是比較復(fù)雜的,如果只是對(duì)一個(gè)widget的屬性進(jìn)行修改,可以做成動(dòng)畫嗎,本文就來探討一下2023-05-05
Android筆記之:App調(diào)試的幾個(gè)命令的實(shí)踐與分析
本篇文章介紹了,在Android中:App調(diào)試的幾個(gè)命令的實(shí)踐與分析。需要的朋友參考下2013-04-04
淺談Android AsyncTask內(nèi)存安全的一種使用方式
這篇文章主要介紹了淺談Android AsyncTask內(nèi)存安全的一種使用方式,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08

