zoukankan      html  css  js  c++  java
  • 35.Android之带删除按钮EditText学习

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

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

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

    布局文件xml:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:background="#000000"
     6     android:orientation="vertical" >
     7 
     8     <LinearLayout
     9         android:layout_width="match_parent"
    10         android:layout_height="wrap_content"
    11         android:background="@drawable/back" 
    12         android:layout_marginBottom="5dp"
    13         android:layout_marginLeft="5dp"
    14         android:layout_marginRight="5dp"
    15         android:layout_marginTop="5dp" >
    16 
    17         <ImageView
    18             android:id="@+id/search_img"
    19             android:layout_width="wrap_content"
    20             android:layout_height="wrap_content"
    21             android:paddingTop="7dp"
    22             android:src="@drawable/search" />
    23 
    24         <EditText
    25             android:id="@+id/clearText"
    26             android:layout_width="fill_parent"
    27             android:layout_height="wrap_content"
    28             android:layout_weight="1"
    29             android:background="@null"
    30             android:hint="搜索"
    31             android:imeOptions="actionDone"
    32             android:singleLine="true" >
    33         </EditText>
    34 
    35         <Button
    36             android:id="@+id/clear_btn"
    37             android:layout_width="40dp"
    38             android:layout_height="40dp"
    39             android:background="@drawable/clear" />
    40     </LinearLayout>
    41 
    42 </LinearLayout>

    activity代码:

     1 package com.example.cleartextdemo;
     2 
     3 import android.app.Activity;
     4 import android.os.Bundle;
     5 import android.text.Editable;
     6 import android.text.TextWatcher;
     7 import android.view.View;
     8 import android.widget.Button;
     9 import android.widget.EditText;
    10 
    11 public class MainActivity extends Activity {
    12 
    13     private EditText clearEditText;
    14     private Button clearbtn;
    15 
    16     @Override
    17     protected void onCreate(Bundle savedInstanceState) {
    18         super.onCreate(savedInstanceState);
    19         setContentView(R.layout.activity_main);
    20 
    21         clearEditText = (EditText) findViewById(R.id.clearText);
    22         clearbtn = (Button) findViewById(R.id.clear_btn);
    23         clearbtn.setOnClickListener(new View.OnClickListener() {
    24             @Override
    25             public void onClick(View v) {
    26                 clearEditText.setText("");
    27             }
    28         });
    29 
    30         clearEditText.addTextChangedListener(mTextWatcher);
    31 
    32     }
    33 
    34     TextWatcher mTextWatcher = new TextWatcher() {
    35 
    36         @Override
    37         public void beforeTextChanged(CharSequence s, int start, int count,
    38                 int after) {
    39             // TODO Auto-generated method stub
    40 
    41         }
    42 
    43         @Override
    44         public void onTextChanged(CharSequence s, int start, int before,
    45                 int count) {
    46             // TODO Auto-generated method stub
    47 
    48         }
    49 
    50         @Override
    51         public void afterTextChanged(Editable s) {
    52             if (clearEditText.getText().toString() != null
    53                     && !clearEditText.getText().toString().equals("")) {
    54                 clearbtn.setVisibility(View.VISIBLE);
    55             } else {
    56                 clearbtn.setVisibility(View.INVISIBLE);
    57             }
    58 
    59         }
    60 
    61     };
    62 
    63 }

     运行后输入信息如图:

    按下删除控件变为:

  • 相关阅读:
    微信跳一跳Python辅助无需配置一键操作
    人工智能三:机器学习、人工智能学习自学资料路线计划
    mysql安装配置、主从复制配置详解
    kafka安装使用配置1.1
    azkaban安装步骤
    flume安装
    zookeeper知识
    zookeeper安装
    mysql语法难点
    mysql安装
  • 原文地址:https://www.cnblogs.com/benchao/p/5160748.html
Copyright © 2011-2022 走看看