Today implements the EditText with the delete function in Android, the effect is as follows:
When you enter content, EditText changes to an edit box with a Delete function button.
Implementation code is very simple, directly on the code,
Layout file xml:
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns: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 <LinearLayout9 Android:layout_width= "Match_parent"Ten Android:layout_height= "Wrap_content" One Android:background= "@drawable/back" A Android:layout_marginbottom= "5DP" - Android:layout_marginleft= "5DP" - Android:layout_marginright= "5DP" the Android:layout_margintop= "5DP" > - - <ImageView - Android:id= "@+id/search_img" + Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" + Android:paddingtop= "7DP" A android:src= "@drawable/search" /> at - <EditText - Android:id= "@+id/cleartext" - Android:layout_width= "Fill_parent" - Android:layout_height= "Wrap_content" - Android:layout_weight= "1" in Android:background= "@null" - Android:hint= "Search" to android:imeoptions= "Actiondone" + Android:singleline= "true" > - </EditText> the * <Button $ Android:id= "@+id/clear_btn"Panax Notoginseng Android:layout_width= "40DP" - Android:layout_height= "40DP" the Android:background= "@drawable/clear" /> + </LinearLayout> A the </LinearLayout>
Activity code:
1 PackageCom.example.cleartextdemo;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 Importandroid.text.Editable;6 ImportAndroid.text.TextWatcher;7 ImportAndroid.view.View;8 ImportAndroid.widget.Button;9 ImportAndroid.widget.EditText;Ten One Public classMainactivityextendsActivity { A - PrivateEditText Clearedittext; - PrivateButton clearbtn; the - @Override - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); - +Clearedittext =(EditText) Findviewbyid (r.id.cleartext); ACLEARBTN =(Button) Findviewbyid (R.ID.CLEAR_BTN); atClearbtn.setonclicklistener (NewView.onclicklistener () { - @Override - Public voidOnClick (View v) { -Clearedittext.settext (""); - } - }); in - Clearedittext.addtextchangedlistener (mtextwatcher); to + } - theTextwatcher Mtextwatcher =NewTextwatcher () { * $ @OverridePanax Notoginseng Public voidBeforetextchanged (Charsequence S,intStartintCount, - intAfter ) { the //TODO auto-generated Method Stub + A } the + @Override - Public voidOnTextChanged (Charsequence S,intStartintbefore, $ intcount) { $ //TODO auto-generated Method Stub - - } the - @OverrideWuyi Public voidaftertextchanged (Editable s) { the if(Clearedittext.gettext (). toString ()! =NULL -&&!clearedittext.gettext (). toString (). Equals ("")) { Wu clearbtn.setvisibility (view.visible); -}Else { About clearbtn.setvisibility (view.invisible); $ } - - } - A }; + the}
Post-run input information
Pressing the Delete control changes to:
35.Android with the Delete button EditText learning