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

Android 自定义EditText输入框带清空按钮

程序员文章站 2023-11-26 22:44:58
android 自定义edittext输入框带清空按钮 当用户输入字符后 edittext会自动在输入框的内部右侧出现删除按钮 重写edittext达到简化布局的效果...

android 自定义edittext输入框带清空按钮

当用户输入字符后 edittext会自动在输入框的内部右侧出现删除按钮

重写edittext达到简化布局的效果

效果图:

Android 自定义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"//显示

android:cursorvisible="false"//隐藏

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!