Android中EditText后添加删除按钮后的实现

本文介绍如何在Android应用中实现带有清除按钮的搜索框及可自定义颜色的水波纹效果,并提供完整的代码示例。

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

编写不易,如有转载,请声明出处:http://blog.youkuaiyun.com/zxc514257857/article/details/73530704

Demo展示图片

这里写图片描述

关键代码

  • 点击叉叉置空EditText
mDelete.setOnClickListener(new View.OnClickListener() {
	@Override
    public void onClick(View v) {
        mEditText.setText("");
    }
});
  • EditText的监听,如果有内容显示叉叉,如果没内容隐藏叉叉
mEditText.addTextChangedListener(new TextWatcher() {
	@Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (s.length() == 0) {
            mDelete.setVisibility(View.GONE);
        } else {
            mDelete.setVisibility(View.VISIBLE);
        }
    }
});
  • 搜索的水波纹效果实现
// (layout)activity_main.xml
<TextView
	android:id="@+id/textview"
	android:layout_width="0dp"
	android:layout_height="wrap_content"
	android:layout_weight="1"
	android:text="搜索"
	android:gravity="center"
	android:textStyle="bold"
	android:textSize="16sp"
	android:textColor="#fff"
	android:background="@drawable/ripple_bg">
---------------------------------------------------------------------------------------------------
// (drawable)ripple_bg.xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="@color/colorAccent">

</ripple>

  注:为了实现可自定义颜色的无界水波纹效果,将 minSdkVersion 调到了21,也就是没有适配5.0以下,如果要适配5.0以下,可参考:Android开发中的水波纹效果实现 http://blog.youkuaiyun.com/zxc514257857/article/details/73200900

全部代码

// (layout)activity_main.xml
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.zhumei.deletedemo.MainActivity">

    <LinearLayout
        android:gravity="center_vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#3F51B5">

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="34dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="10dp"
            android:layout_weight="5">

            <EditText
                android:id="@+id/edittext"
                android:singleLine="true"
                android:textSize="16sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/search_frame"
                android:maxLength="10"
                android:hint="请输入商品名称..."/>

            <ImageView
                android:id="@+id/delete"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_marginRight="10dp"
                android:background="@drawable/delete"
                android:visibility="gone"/>

        </RelativeLayout>

        <TextView
            android:id="@+id/textview"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="搜索"
            android:gravity="center"
            android:textStyle="bold"
            android:textSize="16sp"
            android:textColor="#fff"
            android:background="@drawable/ripple_bg">

        </TextView>
    </LinearLayout>
</RelativeLayout>

---------------------------------------------------------------------------------------------------
// (drawable)ripple_bg.xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="@color/colorAccent">

</ripple>
---------------------------------------------------------------------------------------------------
// MainActivity
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private EditText mEditText;
    private ImageView mDelete;
    private TextView mTextview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    public void initView(){
        mEditText = (EditText) findViewById(R.id.edittext);
        mDelete = (ImageView) findViewById(R.id.delete);
        mTextview = (TextView) findViewById(R.id.textview);
    }

    public void initData(){
        mDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEditText.setText("");
            }
        });

        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 0) {
                    mDelete.setVisibility(View.GONE);
                } else {
                    mDelete.setVisibility(View.VISIBLE);
                }
            }
        });

        mTextview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "搜索!", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Demo下载请移步:http://download.youkuaiyun.com/detail/zxc514257857/9876463


----------因本人才疏学浅,如博客或Demo中有错误的地方请大家随意指出,与大家一起讨论,共同进步,谢谢!----------

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DreamBackTo

感谢各位金主大大(* _ *)

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值