今天实现Android里自定义带删除功能的EditText,效果如下:

当输入内容时,EditText变为带有一个删除功能按钮的编辑框,如图:

实现代码很简单,直接上代码,

布局文件xml:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/back"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp" > <ImageView
android:id="@+id/search_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="7dp"
android:src="@drawable/search" /> <EditText
android:id="@+id/clearText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:hint="搜索"
android:imeOptions="actionDone"
android:singleLine="true" >
</EditText> <Button
android:id="@+id/clear_btn"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/clear" />
</LinearLayout> </LinearLayout>

activity代码:

 package com.example.cleartextdemo;

 import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText; public class MainActivity extends Activity { private EditText clearEditText;
private Button clearbtn; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); clearEditText = (EditText) findViewById(R.id.clearText);
clearbtn = (Button) findViewById(R.id.clear_btn);
clearbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clearEditText.setText("");
}
}); clearEditText.addTextChangedListener(mTextWatcher); } TextWatcher mTextWatcher = new TextWatcher() { @Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub } @Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub } @Override
public void afterTextChanged(Editable s) {
if (clearEditText.getText().toString() != null
&& !clearEditText.getText().toString().equals("")) {
clearbtn.setVisibility(View.VISIBLE);
} else {
clearbtn.setVisibility(View.INVISIBLE);
} } }; }

运行后输入信息如图:

按下删除控件变为:

35.Android之带删除按钮EditText学习的更多相关文章

  1. 模拟邮箱输入邮箱地址、收藏标签。input框输入内容后回车,内容显示成小方块并带删除按钮。

    模拟邮箱输入邮箱地址.收藏标签: 文本框输入文字后按回车键或者分号键,输入框中的文字变成小块并带删除按钮和操作. 页面代码: <!DOCTYPE html> <%@ page lan ...

  2. 带删除的EditText

    在安卓开发中EditText是比较常用的控件之一,那我们平常看到EditText填写了内容之后右边会出现一个删除的按钮,这样可以方便用户对其中文本清空操作,是非常人性化的,我们可以重写EditText ...

  3. Android 自带后退按钮的使用

    一.后退按钮有两种定义,分别是向上按钮和返回按钮:向上按钮:偏向于一种父子关系:返回按钮:反映的是一种前后关系 向上按钮:在清单文件中需要添加后退功能按钮的Activity中添加parentActiv ...

  4. Android自定义View带有删除按钮的EditText

    转载请注明出处http://blog.csdn.net/xiaanming/article/details/11066685 今天给大家带来一个很实用的小控件ClearEditText,就是在Andr ...

  5. [Android]自己定义带删除输入框

    在项目开发中,带删除button输入框也是人们经常常使用到的,该文章便介绍一下怎样创建一个带删除输入框.当中,须要解决的问题例如以下: a)创建自己定义editText类 b)在自己定义editTex ...

  6. 带清空按钮的EditText

    public class ClearEditText extends EditText implements OnFocusChangeListener, TextWatcher { // 删除按钮的 ...

  7. android自定义文本框,后面带清空按钮

    android常见的带清空按钮的文本框,获得焦点时如果有内容则显示,否则不显示 package com.qc.health.weight; import com.qc.health.R; import ...

  8. 删除Android自带软件方法及adb remount 失败解决方案

    删除Android自带软件方法 1.在电脑上打开cmd,然后输入命令 adb remount adb shell su 2.接着就是Linux命令行模式了,输入 cd system/app 3然后输入 ...

  9. Android ListView实现仿iPhone实现左滑删除按钮

    需要自定义ListView.这里就交FloatDelListView吧. 复写onTouchEvent方法.如下: @Override public boolean onTouchEvent(Moti ...

随机推荐

  1. POJ 3398 Perfect Service --最小支配集

    题目链接:http://poj.org/problem?id=3398 这题可以用两种上述讲的两种算法解:http://www.cnblogs.com/whatbeg/p/3776612.html 第 ...

  2. POJ 1845 Sumdiv 【逆元】

    题意:求A^B的所有因子之和 很容易知道,先把分解得到,那么得到,那么 的所有因子和的表达式如下 第一种做法是分治求等比数列的和  用递归二分求等比数列1+pi+pi^2+pi^3+...+pi^n: ...

  3. Spring 一二事(3) - 别名

    别名就是可以通过另外一个名字来访问如下,已有bean:helloWorld3,那么定义别名(alias )后,就能使用“abc”来访问 <bean id="helloWorld3&qu ...

  4. Java虚拟机详解03----常用JVM配置参数

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  5. 在spring环境下集成ActiveMQ

    1.参考文献 Spring集成ActiveMQ配置 Spring JMS异步发收消息 ActiveMQ 2.环境 在前面的一篇ActiveMQ入门实例中我们实现了消息的异步传送,这篇博文将如何在spr ...

  6. java 21 - 13 IO流之序列化和反序列化

    序列化流:把对象按照流一样的方式存入文本文件或者在网络中传输.对象 -- 流数据(ObjectOutputStream) 构造方法:ObjectInputStream(InputStream in) ...

  7. 使用jQuery向asp.net Mvc传递复杂json数据-ModelBinder篇

    调用jQuery的ajax方法时,jQuery会根据post或者get协议对参数data进行序列化; 如果提交的数据使用复杂的json数据,例如: {userId:32323,userName:{fi ...

  8. 010医疗项目-模块一:用户添加的实现(Dao,Service,Action,增加页面调试,提交页面调试)

    要实现的效果:

  9. 15Mybatis_输出类型

    输出类型分为两种:1.resultType          和         2.resultMap 接下来先讲解resultType: 使用resultType进行输出映射,只有查询出来的列名和 ...

  10. OAF页面隐藏右上角的全局按钮(主页,注销等)

    OAPageLayoutBean page = pageContext.getPageLayoutBean(); page.prepareForRendering(pageContext); page ...