Custom Control-input box with delete button

Source: Internet
Author: User

Let's take a look at an eidttext combination control with the delete button. The delete button is displayed after the user input. You can click it to cancel the user input. First, write a custom control layout:

 
<? XML version = "1.0" encoding = "UTF-8"?> <Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <edittext Android: Id = "@ + ID/ET" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: singleline = "true"/> <imagebutton Android: Id = "@ + ID/IB" Android: visibility = "gone" Android: src = "@ drawable/menu_delete" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Background = "#00000000" Android: layout_alignright = "@ + ID/ET"/> </relativelayout>

To achieve the button Effect on the right of the input box, be sure to hide the button. Write an editcancel class to delete user input. The textwatch interface is used to listen for text changes in the input box. It's easy to use. Just implement the three methods. ViewCode:

Import Android. content. context;
Import Android. Text. editable;
Import Android. Text. textwatcher;
Import Android. util. attributeset;
Import Android. View. layoutinflater;
Import Android. View. view;
Import Android. widget. edittext;
Import Android. widget. imagebutton;
Import Android. widget. linearlayout;

Public class editcancel extends linearlayout implements edtinterface {
Imagebutton Ib;
Edittext et;
Public editcancel (context ){
Super (context );
}
Public editcancel (context, attributeset attrs ){
Super (context, attrs );
Layoutinflater. From (context). Inflate (R. layout. custom_editview, this, true );
Init ();
}
Private void Init (){
IB = (imagebutton) findviewbyid (R. Id. Ib );
ET = (edittext) findviewbyid (R. Id. Et );
Et. addtextchangedlistener (TW); // bind a listener for text changes to the input box.
// Add button click event
Ib. setonclicklistener (New onclicklistener (){
@ Override
Public void onclick (view v ){
Hidebtn (); // hide button
Et. settext (""); // set the content of the input box to null.
}
});
}
// When the input box status changes, the corresponding method is called.
Textwatcher Tw = new textwatcher (){
@ Override
Public void ontextchanged (charsequence S, int start, int before, int count ){
// Todo auto-generated method stub
}
@ Override
Public void beforetextchanged (charsequence S, int start, int count, int after ){
// Todo auto-generated method stub
}
// Call after the text changes
@ Override
Public void aftertextchanged (editable s ){
If (S. Length () = 0 ){
Hidebtn (); // hide button
} Else {
Showbtn (); // display button
}
}
};

@ Override
Public void hidebtn (){
// The setting button is invisible.
If (IB. isshown () Ib. setvisibility (view. Gone );
}
@ Override
Public void showbtn (){
// The setting button is visible.
If (! Ib. isshown () Ib. setvisibility (view. Visible );
}
}

Interface edtinterface {
Public void hidebtn ();
Public void showbtn ();
}

 

The text is judged in the aftertextchanged method of the textwatch interface. If the length is 0, the button is hidden. Otherwise, the button is displayed.

In addition, implement the click event of imagebutton (that is, the cross), delete the content in the input box, and hide the button.

 

 

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.