最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2017年05月18日 11:00:17   投稿:lqh  
這篇文章主要介紹了Android 自定義EditText輸入框帶清空按鈕的相關(guān)資料,需要的朋友可以參考下

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

當(dāng)用戶輸入字符后 EditText會(huì)自動(dòng)在輸入框的內(nèi)部右側(cè)出現(xiàn)刪除按鈕

重寫(xiě)EditText達(dá)到簡(jiǎn)化布局的效果

效果圖:

繼承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() { // 對(duì)文本內(nèi)容改變進(jìn)行監(jiān)聽(tīng)
			@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;

	}

	/**
	 * 添加觸摸事件 點(diǎn)擊之后 出現(xiàn) 清空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);
	}

	/**
	 * 顯示右側(cè)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中的屬性簡(jiǎn)介:

顯示右側(cè)的X 按鈕:

android:drawableRight="@drawable/edit_clear"

設(shè)置光標(biāo)的顏色 設(shè)置@null 表示光標(biāo)的顏色和輸入框的字體顏色相同

android:textCursorDrawable="@null"

顯示隱藏光標(biāo)

android:cursorVisible="true"http://顯示

android:cursorVisible="false"http://隱藏

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

相關(guān)文章

  • Android 曲線圖的繪制示例代碼

    Android 曲線圖的繪制示例代碼

    本篇文章主要介紹了Android 曲線圖的繪制示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-02-02
  • Android仿新浪微博個(gè)人信息界面及其他效果

    Android仿新浪微博個(gè)人信息界面及其他效果

    這篇文章主要為大家詳細(xì)介紹了Android仿新浪微博個(gè)人信息界面及其他效果設(shè)計(jì),如正則表達(dá)式如何匹配相應(yīng)表情字段,處理微博發(fā)出時(shí)間距現(xiàn)在時(shí)刻的時(shí)間,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • 去除arraylist容器中的相同的對(duì)象元素的方法

    去除arraylist容器中的相同的對(duì)象元素的方法

    下面小編就為大家?guī)?lái)一篇去除arraylist容器中的相同的對(duì)象元素的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-09-09
  • Android內(nèi)置的OkHttp用法介紹

    Android內(nèi)置的OkHttp用法介紹

    okhttp是一個(gè)第三方類庫(kù),用于android中請(qǐng)求網(wǎng)絡(luò)。這是一個(gè)開(kāi)源項(xiàng)目,是安卓端最火熱的輕量級(jí)框架,由移動(dòng)支付Square公司貢獻(xiàn)(該公司還貢獻(xiàn)了Picasso和LeakCanary) 。用于替代HttpUrlConnection和Apache HttpClient
    2022-08-08
  • Android自定義View 實(shí)現(xiàn)水波紋動(dòng)畫(huà)引導(dǎo)效果

    Android自定義View 實(shí)現(xiàn)水波紋動(dòng)畫(huà)引導(dǎo)效果

    在android程序開(kāi)發(fā)中,我們經(jīng)常簡(jiǎn)單通過(guò)自定義view實(shí)現(xiàn)水波紋動(dòng)畫(huà)引導(dǎo)功能,下面通過(guò)本文給大家分享實(shí)現(xiàn)代碼,需要的朋友參考下
    2017-01-01
  • Android實(shí)現(xiàn)在ServiceManager中加入自定義服務(wù)的方法詳解

    Android實(shí)現(xiàn)在ServiceManager中加入自定義服務(wù)的方法詳解

    這篇文章主要介紹了Android實(shí)現(xiàn)在ServiceManager中加入自定義服務(wù)的方法,結(jié)合實(shí)例形式分析了Android開(kāi)發(fā)中ServiceManager自定義服務(wù)的相關(guān)創(chuàng)建與使用方法,需要的朋友可以參考下
    2017-08-08
  • Android開(kāi)發(fā)入門(mén)之Notification用法分析

    Android開(kāi)發(fā)入門(mén)之Notification用法分析

    這篇文章主要介紹了Android中Notification用法,較為詳細(xì)的分析了Notification的功能、使用步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2016-07-07
  • Android App數(shù)據(jù)格式Json解析方法和常見(jiàn)問(wèn)題

    Android App數(shù)據(jù)格式Json解析方法和常見(jiàn)問(wèn)題

    JSON數(shù)據(jù)格式,在Android中被廣泛運(yùn)用于客戶端和網(wǎng)絡(luò)(或者說(shuō)服務(wù)器)通信,非常有必要系統(tǒng)的了解學(xué)習(xí)。恰逢本人最近對(duì)json做了一個(gè)簡(jiǎn)單的學(xué)習(xí),特此總結(jié)一下,以饗各位
    2014-03-03
  • Android ListView彈性效果的實(shí)現(xiàn)方法

    Android ListView彈性效果的實(shí)現(xiàn)方法

    這篇文章主要為大家詳細(xì)介紹了Android ListView彈性效果的實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Android中dumpsys命令用法簡(jiǎn)單介紹

    Android中dumpsys命令用法簡(jiǎn)單介紹

    這篇文章主要介紹了Android中dumpsys命令用法簡(jiǎn)單介紹的相關(guān)資料,需要的朋友可以參考下
    2017-03-03

最新評(píng)論

东乌珠穆沁旗| 中江县| 德保县| 洛南县| 濮阳市| 左权县| 阿瓦提县| 班玛县| 威信县| 吉安县| 新乡县| 蓬溪县| 唐海县| 莆田市| 西充县| 志丹县| 榕江县| 资中县| 武城县| 沂水县| 新巴尔虎左旗| 肥东县| 石屏县| 全南县| 安国市| 玉溪市| 富阳市| 抚顺市| 自治县| 乐至县| 长阳| 连南| 岳西县| 兴国县| 墨玉县| 古丈县| 兰坪| 剑川县| 景谷| 乾安县| 休宁县|