35.Android之带删除按钮EditText学习
今天实现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学习的更多相关文章
- 模拟邮箱输入邮箱地址、收藏标签。input框输入内容后回车,内容显示成小方块并带删除按钮。
模拟邮箱输入邮箱地址.收藏标签: 文本框输入文字后按回车键或者分号键,输入框中的文字变成小块并带删除按钮和操作. 页面代码: <!DOCTYPE html> <%@ page lan ...
- 带删除的EditText
在安卓开发中EditText是比较常用的控件之一,那我们平常看到EditText填写了内容之后右边会出现一个删除的按钮,这样可以方便用户对其中文本清空操作,是非常人性化的,我们可以重写EditText ...
- Android 自带后退按钮的使用
一.后退按钮有两种定义,分别是向上按钮和返回按钮:向上按钮:偏向于一种父子关系:返回按钮:反映的是一种前后关系 向上按钮:在清单文件中需要添加后退功能按钮的Activity中添加parentActiv ...
- Android自定义View带有删除按钮的EditText
转载请注明出处http://blog.csdn.net/xiaanming/article/details/11066685 今天给大家带来一个很实用的小控件ClearEditText,就是在Andr ...
- [Android]自己定义带删除输入框
在项目开发中,带删除button输入框也是人们经常常使用到的,该文章便介绍一下怎样创建一个带删除输入框.当中,须要解决的问题例如以下: a)创建自己定义editText类 b)在自己定义editTex ...
- 带清空按钮的EditText
public class ClearEditText extends EditText implements OnFocusChangeListener, TextWatcher { // 删除按钮的 ...
- android自定义文本框,后面带清空按钮
android常见的带清空按钮的文本框,获得焦点时如果有内容则显示,否则不显示 package com.qc.health.weight; import com.qc.health.R; import ...
- 删除Android自带软件方法及adb remount 失败解决方案
删除Android自带软件方法 1.在电脑上打开cmd,然后输入命令 adb remount adb shell su 2.接着就是Linux命令行模式了,输入 cd system/app 3然后输入 ...
- Android ListView实现仿iPhone实现左滑删除按钮
需要自定义ListView.这里就交FloatDelListView吧. 复写onTouchEvent方法.如下: @Override public boolean onTouchEvent(Moti ...
随机推荐
- 2014 Super Training #7 F Power of Fibonacci --数学+逆元+快速幂
原题:ZOJ 3774 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3774 --------------------- ...
- centos下pip安装mysql_python
今天在使用pip安装mysql_python时,遇到一些问题,现记录下来. 1.执行pip install mysql-python时,报错 Running setup.py egg_info for ...
- Android Studio下载与安装
Android Studio下载与安装 1 2 3 4 5 分步阅读 百度经验:jingyan.baidu.com 自从Google宣布Android Studio将取代Eclipse,正式成为官方集 ...
- yepnope初体验
真是一个百花齐放的项目,在熟悉代码的过程中,看到各种前端.后端技术,这回又冒出一个yepnope的东东,搜索了一下,不是什么新技术,打开官方网站,已经弃用的通知非常醒目,但仍提供相关文档在github ...
- title与h1的区别
title与h1的联系: 从网站角度看,title更重于网站信息.title可以直接告诉搜索引擎和用户这个网站是关于什么主题和内容的. 从文章角度看,h1则是用于概括文章主题.用户进入内容页,想看到的 ...
- 【MySQL】PREPARE 的应用
简单的用set或者declare语句定义变量,然后直接作为sql的表名是不行的,mysql会把变量名当作表名.在其他的sql数据库中也是如此,mssql的解决方法是将整条sql语句作为变量,其中穿插变 ...
- 【WPF】FillRule
获取或设置如何组合此 GeometryGroup 中所包含对象的相交区域. Dependency property identifier field: FillRuleProperty FillRul ...
- [转]VMware虚拟机上网络连接(network type)的三种模式--bridged、host-only、NAT
转自:http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/03/15/1985084.html VMWare提供了三种工作模式,它们是brid ...
- Linux 结束进程
一个进程由于以下5个原因中的一个终止 --main函数调用return; --调用exit函数--C语言库函数: --调用_exit函数--系统调用 --调用abort函数 --被一个信号终止.(ki ...
- Vim中split的使用方法
Vim中split的使用方法 一.作用 用split可以显示两个不同的文件:或者同时显示一个文件的两个不同地方:又或者并排比较两个文件.这一切都可以通过分割窗口实现.如下图,左边的两个窗口是mytoo ...