今天实现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. C++ 箭头-> 双冒号:: 点号.操作符区别

    点 (.) 如果变量是一个对象或者对象引用,则用它来访问对象成员. 箭头( ->) 如果变量是一个对象指针,则用它来访问对象成员. 双冒号 (::) 如果操作目标是一个具有名空间的标识符,则用它 ...

  2. R之字符串连接函数paste

    函数paste的一般使用格式为: paste(..., sep = " ", collapse = NULL) 其中...表示一个或多个R可以被转化为字符型的对象:参数sep表示分 ...

  3. [WPF]资源字典——程序集之间的资源共享 简单换皮肤

    直接上代码,已便已后自己查况阅,新手也可以看! 1.新建一个资料类和一个WPF工程 2.APP.XAML应该资源字典,注意应Source格式,前面一定要有“/” <ResourceDiction ...

  4. 混合语言编程:启用CLR(公共语言运行时编译)让C#调用C++

    前言 关于混合C#和C++的编程方式,本人之前写过一篇博客(参见混合语言编程:C#使用原生的Directx和OpenGL),在之前的博客中,介绍了在C#的Winform和WPF下使用原生的Direct ...

  5. Android 中调试手段 打印函数调用栈信息

    下面来简单介绍下 android 中的一种调试方法. 在 android 的 app 开发与调试中,经常需要用到打 Log 的方式来查看函数调用点. 这里介绍一种方法来打印当前栈中的函数调用关系 St ...

  6. 突然想起android与mfc差异

    两者都可以算作是客户端程序,都是做上位机用的.而且都是被动执行. 相同点: 1.MFC中,它是由 project的名字 里面的某个成员函数来初始化,窗体,以及窗体里面的变量. 后面都是监听消息循环.数 ...

  7. php基础02:变量

    1.创建变量 <?php $num1 = 15; $num2 = 15.5; echo $num1+$num2; echo "<br>"; ?> 2.Loc ...

  8. Caffe学习系列(17):模型各层数据和参数可视化

    cifar10的各层数据和参数可视化 .caret,.dropup>.btn>.caret{border-top-color:#000!important}.label{border:1p ...

  9. REST: C#调用REST API (zz)

    由于辞职的原因,最近正在忙于找工作.在这段期间收到了一家公司的上机测试题,一共两道题,其中一道题是关于REST API的应用.虽然在面试时,我已经说过,不懂REST,但那面试PM还是给了一道这题让我做 ...

  10. 如何在WPF中引用Windows.System.Forms.Integration

    转自 http://www.cnblogs.com/sinozhang1988/archive/2012/11/28/2792804.html “未找到程序集 WindowsFormsIntegrat ...