35.Android之帶刪除按鈕EditText學習

來源:互聯網
上載者:User

標籤:

今天實現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         <ImageView18             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         <EditText25             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         <Button36             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     @Override17     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             @Override25             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         @Override37         public void beforeTextChanged(CharSequence s, int start, int count,38                 int after) {39             // TODO Auto-generated method stub40 41         }42 43         @Override44         public void onTextChanged(CharSequence s, int start, int before,45                 int count) {46             // TODO Auto-generated method stub47 48         }49 50         @Override51         public void afterTextChanged(Editable s) {52             if (clearEditText.getText().toString() != null53                     && !clearEditText.getText().toString().equals("")) {54                 clearbtn.setVisibility(View.VISIBLE);55             } else {56                 clearbtn.setVisibility(View.INVISIBLE);57             }58 59         }60 61     };62 63 }

 運行後輸入資訊

按下刪除控制項變為:

 

35.Android之帶刪除按鈕EditText學習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.