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 ...
随机推荐
- Spring MVC Spring MyBatis 整合 - 快速上手
我个人比较喜欢写注释,在工作中对注释的重要性看的也比较高,所以大部分文字都在注释中,代码外的文字会写的偏少,关键能懂就行 先看一下整合后的工程目录(单工程,多工程以后会采用maven) 5个packa ...
- Mybaits学习总结1
http://www.cnblogs.com/xdp-gacl/p/4261895.html 参考了这篇文章搭建了Mybaits环境,原作者有些地方没有标注使用某种编码,我是自学SQL的,所以深知编码 ...
- jq 操作table
转载于:http://www.jb51.net/article/34633.htm jquery获取table中的某行全部td的内容方法,需要的朋友可以参考一下 <table>< ...
- sass、git、ruby的安装与使用。
安装sass时必须先安装ruby,在安装ruby时勾选Add Ruby executables to your PATH这个选项,添加环境变量,不然以后使用编译软件的时候会提示找不到ruby环境 sa ...
- action中result没有值
action中result没有值,访问action会输出action中的所有数据,输出类型为.action类型 .
- tcpdump参数应用
详细参数: http://www.cnblogs.com/ggjucheng/archive/2012/01/14/2322659.html 我用到的参数: 一 tcpdump重要参数 -i 指定监听 ...
- Objective-c归档的概念和用法
‘
- SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)
使用 SSM ( Spring . SpringMVC 和 Mybatis )已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没 ...
- 使用mobile.changePage()时出现的问题(转)
使用mobile.changePage()页面跳转,当跳转到目标页面时,目标页面中的初始化js如$().ready()及其他引入的js都无法执 行,重新刷新页面后才会执行.想到changePage() ...
- display:inline-block的坑
一直用display:inline-block做某种导航栏还很爽,突然有一个柱状图的需求便也这么做了,于是成功被坑. 简简单单个需求,大致这样 只用几个li加上display:inline-block ...