国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 自定義EditText輸入框帶清空按鈕

2019-12-12 02:52:31
字體:
來源:轉載
供稿:網友

Android 自定義EditText輸入框帶清空按鈕

當用戶輸入字符后 EditText會自動在輸入框的內部右側出現刪除按鈕

重寫EditText達到簡化布局的效果

效果圖:

繼承EditText

package com.example.myedittexttest;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;public class MyEditText extends EditText {	private final String TAG = "MyEditText";	private Drawable dRight;	private Rect rBounds;	public MyEditText(Context paramContext) {		super(paramContext);		initEditText();	}	public MyEditText(Context paramContext, AttributeSet paramAttributeSet) {		super(paramContext, paramAttributeSet);		initEditText();	}	public MyEditText(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {		super(paramContext, paramAttributeSet, paramInt);		initEditText();	}	// 初始化edittext 控件	private void initEditText() {		setEditTextDrawable();		addTextChangedListener(new TextWatcher() { // 對文本內容改變進行監聽			@Override			public void afterTextChanged(Editable paramEditable) {			}			@Override			public void beforeTextChanged(CharSequence paramCharSequence, int paramInt1, int paramInt2, int paramInt3) {			}			@Override			public void onTextChanged(CharSequence paramCharSequence, int paramInt1, int paramInt2, int paramInt3) {				MyEditText.this.setEditTextDrawable();			}		});	}	// 控制圖片的顯示	public void setEditTextDrawable() {		if (getText().toString().length() == 0) {			setCompoundDrawables(null, null, null, null);		} else {			setCompoundDrawables(null, null, this.dRight, null);		}	}	@Override	protected void onDetachedFromWindow() {		super.onDetachedFromWindow();		this.dRight = null;		this.rBounds = null;	}	/**	 * 添加觸摸事件 點擊之后 出現 清空editText的效果	 */	@Override	public boolean onTouchEvent(MotionEvent paramMotionEvent) {		if ((this.dRight != null) && (paramMotionEvent.getAction() == 1)) {			this.rBounds = this.dRight.getBounds();			int i = (int) paramMotionEvent.getRawX();// 距離屏幕的距離			// int i = (int) paramMotionEvent.getX();//距離邊框的距離			if (i > getRight() - 3 * this.rBounds.width()) {				setText("");				paramMotionEvent.setAction(MotionEvent.ACTION_CANCEL);			}		}		return super.onTouchEvent(paramMotionEvent);	}	/**	 * 顯示右側X圖片的	 * 	 * 左上右下	 */	@Override	public void setCompoundDrawables(Drawable paramDrawable1, Drawable paramDrawable2, Drawable paramDrawable3, Drawable paramDrawable4) {		if (paramDrawable3 != null)			this.dRight = paramDrawable3;		super.setCompoundDrawables(paramDrawable1, paramDrawable2, paramDrawable3, paramDrawable4);	}}

XML布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:paddingBottom="@dimen/activity_vertical_margin"  android:paddingLeft="@dimen/activity_horizontal_margin"  android:paddingRight="@dimen/activity_horizontal_margin"  android:paddingTop="@dimen/activity_vertical_margin"  tools:context=".MainActivity" >  <com.example.myedittexttest.MyEditText    android:id="@+id/edit_text"    android:layout_width="fill_parent"    android:layout_height="50dp"    android:layout_marginTop="50dp"    android:background="#88aaff"    android:drawableRight="@drawable/edit_clear"    android:textCursorDrawable="@null" />  <Button    android:id="@+id/button1"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_below="@+id/edit_text"    android:layout_marginTop="84dp"    android:layout_toRightOf="@+id/textView1"    android:text="Button" /></RelativeLayout>

XML中的屬性簡介:

顯示右側的X 按鈕:

android:drawableRight="@drawable/edit_clear"

設置光標的顏色 設置@null 表示光標的顏色和輸入框的字體顏色相同

android:textCursorDrawable="@null"

顯示隱藏光標

android:cursorVisible="true"http://顯示android:cursorVisible="false"http://隱藏

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 类乌齐县| 岑溪市| 黎平县| 富平县| 叶城县| 永修县| 青铜峡市| 沙湾县| 鄂伦春自治旗| 黄龙县| 和田市| 紫金县| 南昌市| 江安县| 江门市| 兴安县| 义马市| 丹江口市| 赤壁市| 惠水县| 江华| 会泽县| 石屏县| 米林县| 金塔县| 重庆市| 尉犁县| 江津市| 阳西县| 漳平市| 崇仁县| 平和县| 关岭| 元谋县| 乐清市| 绿春县| 宁强县| 岳池县| 荣成市| 旌德县| 梁平县|