Android Edittext 清空按钮功能的实现

本文将介绍如何在Android应用中为EditText添加清除按钮功能。通过布局XML配置及代码实现,详细展示了如何实现点击按钮清空输入框内容的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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("");
			}
		});




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值