日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

自定义EditText输入框

發(fā)布時間:2023/12/10 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自定义EditText输入框 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

轉(zhuǎn)載地址:http ://blog.csdn.net/lyfzxf/article/details/53513502

很喜歡簡書App的登陸框風(fēng)格 。

?

自定義ImgEditText 繼承與EditText。主要重寫部分方法即可。

?

?

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;public class ImgEditText extends android.support.v7.widget.AppCompatEditText 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) {//這里構(gòu)造方法也很重要,不加這個很多屬性不能再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);//這里只要改后面兩個參數(shù)就好了,一個寬一個是高,如果想知道為什么可以查找源碼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)選中時判斷@Overrideprotected 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)聽事件@Overridepublic 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設(shè)置點擊事件,所以我們用記住我們按下的位置來模擬點擊事件* 當(dāng)我們按下的位置 在 EditText的寬度 - 圖標(biāo)到控件右邊的間距 - 圖標(biāo)的寬度 和* EditText的寬度 - 圖標(biāo)到控件右邊的間距之間我們就算點擊了圖標(biāo),豎直方向就沒有考慮* (參考 http://blog.csdn.net/xiaanming/article/details/11066685/)*/@Overridepublic 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)用點擊事件(外部實現(xiàn))mightDrawableClick.rightDrawableClick();}}}return super.onTouchEvent(event);}//設(shè)置右側(cè)按鈕的點擊事件,外部調(diào)用的時候?qū)崿F(xiàn)該方法public void setDrawableClick( IMyRightDrawableClick myMightDrawableClick){this.mightDrawableClick = myMightDrawableClick;}//自定義接口(實現(xiàn)右邊圖片點擊事件)public interface IMyRightDrawableClick {void rightDrawableClick();}//允許外部修改右側(cè)顯示的圖片public void setRightDrawable(Drawable drawable){rightDrawable = drawable;setCompoundDrawablesWithIntrinsicBounds(leftDrawable, null, rightDrawable, null);}}

?

?

?

?

以上就是自定義類的主要代碼了,注釋比較清楚。

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

?

?

<TableRow><com.sdwfvc.exiaobang.view.ImgEditTextandroid:id="@+id/et_login_password"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginBottom="10dp"android:layout_marginTop="10dp"android:layout_weight="1"android:background="@null"android:drawableLeft="@mipmap/mm_image"android:drawablePadding="15dp"android:drawableRight="@mipmap/eye_normal"android:hint="密碼"android:inputType="textPassword"android:maxLength="16"android:paddingLeft="15dp"android:paddingRight="15dp"android:paddingTop="5dp"/></TableRow>

?

?

下面看代碼中的設(shè)置

?

?

?

?

?

mEt_login_password = (ImgEditText) findViewById(R.id.et_login_password); //登錄文本框drawableRight的點擊事件 mEt_login_password.setDrawableClick(this); //登錄文本框DrawableRight的點擊事件 @Override public void rightDrawableClick() {if (isHidden) {//設(shè)置EditText文本為可見的mEt_login_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());mEt_login_password.setRightDrawable(getResources().getDrawable(R.mipmap.eye_selected));} else {//設(shè)置EditText文本為隱藏的mEt_login_password.setTransformationMethod(PasswordTransformationMethod.getInstance());mEt_login_password.setRightDrawable(getResources().getDrawable(R.mipmap.eye_normal));}isHidden = !isHidden;mEt_login_password.postInvalidate();//切換后將EditText光標(biāo)置于末尾CharSequence charSequence = mEt_login_password.getText();if (charSequence instanceof Spannable) {Spannable spanText = (Spannable) charSequence;Selection.setSelection(spanText, charSequence.length());} }

?

?

?

?

這樣我們的例子就完成了。

不懂的可以下載看源碼,很簡單。

源碼下載

?

總結(jié)

以上是生活随笔為你收集整理的自定义EditText输入框的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。