Android Edittext 清空按钮功能的实现

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

1. 布局xml(仅贴出输入框部分)

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1.5"
        android:gravity="bottom" >

        <TextView
            android:id="@+id/txv_username"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="20dp"
            android:layout_weight="1.5"
            android:text="@string/user_name"
            android:textSize="@dimen/BigLabelFontSize" />

        <EditText
            android:id="@+id/edt_username"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="10dp"
            android:layout_weight="4"
            android:background="@drawable/edit_bg_login"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:singleLine="true" >
        </EditText>

        <Button
            android:id="@+id/button_name_clear"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="20dp"
            android:background="@drawable/delete"
            android:visibility="invisible" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal" >

        <View
            android:layout_width="10dp"
            android:layout_height="0.5dp"
            android:background="#FFFFFF" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="0.5dp"
            android:background="#D4D4D4" />

        <View
            android:layout_width="10dp"
            android:layout_height="0.5dp"
            android:background="#FFFFFF" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1.5"
        android:gravity="bottom" >

        <TextView
            android:id="@+id/txv_password"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="20dp"
            android:layout_weight="1.5"
            android:text="@string/pass_word"
            android:textSize="@dimen/BigLabelFontSize" />

        <EditText
            android:id="@+id/edt_password"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="10dp"
            android:layout_weight="4"
            android:background="@drawable/edit_bg_login"
            android:inputType="textPassword"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:singleLine="true" >
        </EditText>

        <Button
            android:id="@+id/button_psw_clear"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="20dp"
            android:background="@drawable/delete"
            android:visibility="invisible" />
    </LinearLayout>


2. 代码实现(仅贴出实现清除按钮功能部分代码)

        // 清空用户名按钮
	private Button clearNameButton = null;
	// 清空密码按钮
	private Button clearPswButton = null;
	// 用户名输入框
	private EditText edtUserCode = null;
	// 密码输入框
	private EditText edtPassWord = null;


                edtUserCode = (EditText) this.findViewById(R.id.edt_username);
		edtPassWord = (EditText) this.findViewById(R.id.edt_password);
		edtUserCode.setSelection(edtUserCode.getText().length());
		edtPassWord.setSelection(edtPassWord.getText().length());
		clearNameButton = (Button) findViewById(R.id.button_name_clear);
		clearPswButton = (Button) findViewById(R.id.button_psw_clear);		
		/**
		 * 用户名输入框监听事件
		 */
		edtUserCode.addTextChangedListener(new TextWatcher() {

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				// TODO Auto-generated method stub
				if (edtUserCode.getText().toString() != null
						&& !edtUserCode.getText().toString().equals("")) {
					clearNameButton.setVisibility(View.VISIBLE);
				} else {
					clearNameButton.setVisibility(View.INVISIBLE);
				}
			}

		});

		/**
		 * 密码输入框监听事件
		 */
		edtPassWord.addTextChangedListener(new TextWatcher() {

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				// TODO Auto-generated method stub
				if (edtPassWord.getText().toString() != null
						&& !edtPassWord.getText().toString().equals("")) {
					clearPswButton.setVisibility(View.VISIBLE);
				} else {
					clearPswButton.setVisibility(View.INVISIBLE);
				}
			}

		});

		/**
		 * 清空用户名按钮的监听事件
		 */
		clearNameButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				edtUserCode.setText("");
			}
		});

		/**
		 * 清空密码按钮的监听事件
		 */
		clearPswButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				edtPassWord.setText("");
			}
		});




Android清空编辑框数据功能实现 引言在项目开发中你肯定会遇到这样的需求,输入到EditText中的数据一个一个清理太麻烦了,需要有一个按钮直接能实现一次删除整个EditText控件中的数据,那么接下来我就给大家封装一个方法,有这样的需求直接调用就好了。话不多说,直接上代码:/** *清空编辑框按钮 * @param editText 需要清空EditText控件 * @param delIm 阅读详情

相关推荐

Android EditText清空按钮

Android EditText清空按钮 可直接使用

Android开发实现清空按钮EditText示例

主要介绍了Android开发实现清空按钮EditText,结合具体实例形式分析了Android实现EditText清空按钮功能相关操作技巧,非常具有实用价值,需要的朋友可以参考下

MyEditText自定义带清除按钮的输入框

本资源是自定义带清除按钮的TextView,提供完善的属性设置,包括字长、清除按钮样式、字号、字颜色、hint、text属性设置,并都提供相应的代码设置方法,而且暴露出对应方法获取EditText拉进行更详细比如inputype的设置,大大简化了工程编码。 工程采用Android Studio编写。

android keyboardview 清空,重写Keyboard

对应的编码绘制步骤1、写keyboard的xml文件android:horizontalGap="12dp"android:verticalGap="14dp"android:keyWidth="33.33333%p"android:keyHeight="8%p">android:codes="49"android:keyLabel="1">android:codes="50"an...

weixin_39728221的博客 587

android editText清空功能! 详细步骤

有LinearLayout+EditText+ImageView 的组合 也有自己封装实现在xml中使用

weixin_57038660的博客 3077

Android 自定义EditText输入框 带清空按钮,深入浅出Android性能调优

/ 距离屏幕的距离。(img-f48YTCed-1711137120573)]Android高级架构师之路很漫长,一起共勉吧!Android高级架构师之路很漫长,一起共勉吧!// 控制图片的显示。

m0_57473261的博客 1003

Android EditText清空按钮&动画

安卓开发中有时经常会用到编辑框(登录、找回密码等场景),但是安卓的EditText竟然不自带清空内容的功能,网上找了下资料自定义带清空功能EditText还是比较多的,但是自带明文和密文切换的基本没有,所以自己没事干做了一个带点动画效果的自定义EditText

M家杰的博客 8803

android开发的取消清空按钮,Android Edittext 清空按钮功能实现

1. 布局xml(仅贴出输入框部分)2. 代码实现(仅贴出实现清除按钮功能部分代码)// 清空用户名按钮private Button clearNameButton = null;// 清空密码按钮private Button clearPswButton = null;// 用户名输入框private EditText edtUserCode = null;// 密码输入框private Edi...

weixin_29650949的博客 1044

android按下按钮清空,Android开发实现清空按钮EditText示例

本文实例讲述了Android开发实现清空按钮EditText。分享给大家供大家参考,具体如下:一、效果图:二、具体代码:import android.content.Context;import android.graphics.drawable.Drawable;import android.support.v4.content.ContextCompat;import android.su...

weixin_39603823的博客 534

EditText获取焦点显示按钮,点击按钮清空EditText

//用户触摸EditText得到焦点事件 mEtAccountUserPassword.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View arg0, boolean hasFocus) { // T

u014039918的博客 2146

Android Edittext 添加按钮

Android Edittext 添加按钮由于项目需求,现在需要在edittext上加一个删除图片按钮,话不多说 看代码先看效果图

冷树树的小窝 3821

带有一清空功能EditText

介绍  很常见的一个功能,大部分app在登录界面都会实现这个功能了。因为在掘金上看了一篇类似的文章,所以决定自己实践一下。   下图为实现效果:常见实现方法 组合控件,EditText + Button  实现简单,可以单独使用。 自定义View,继承EditText,通过EditText自带的Drawable来实现。  布局复杂度低 继承EditText实现功能需要考虑的问题根据业务

Siobhan的专栏 4931

Android开发中,使用 EditText 输入内容,如何进行一清空内容处理

本文仅为个人的处理方式,希望能对您有所帮助,欢迎各位留言指正 1、text.xml示例: " class="hljs "><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout...

weixin_34352449的博客 885

Android EditText 自定义控件 带消除功能

Android EditText 自定义控件 带消除功能

Hxuejie的专栏 474

Android EditText删除功能实现

今天做了一个EditText删除按钮Demo感觉有点用,所以分享一下! 效果图片 实现 1.在EditText上面加一个图片 (1)添加图片就比较简单了在EditText里面设置属性 android:drawableRight="@drawable/delete"  2.获取到图片的区域 (1)这里实现需要用onTouch事件 @Override  public bo

li_Struggle的专栏 1759

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

addTextChangedListener(new TextWatcher() { // 对文本内容改变进行监听。// 距离屏幕的距离。设置光标的颜色 设置@null 表示光标的颜色和输入框的字体颜色相同。android:cursorVisible=“false”//隐藏。android:cursorVisible=“true”//显示。// 初始化edittext 控件。// 控制图片的显示。

2501_90499511的博客 855
上一篇: Android Edittext 清空按钮功能 自定义
下一篇: SVN服务器的搭建
KeenSpace
博客等级 码龄12年 4粉丝 29原创
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值