Detailed explanation of Android input box control with clearing function
Detailed explanation of Android input box control with clearing function
Today, I saw a good custom input box control, so I recorded it.
Good results:
1、 Customize a class named clearedittext
Set the click and input listening code inside,
Setcleariconvisable() method is used to set the methods of hiding and displaying clear icons. We do not call setvisibility() method here. Setvisibility() method is for view. We can call setcomposundrawables (drawable left, drawable top, drawable right, drawable bottom) to set up, down, left and right icons
Setonfocuschangelistener (this) sets focus change listening for the input box. If the input box has focus, we judge whether the value of the input box is empty. If it is empty, the clear icon will be hidden, otherwise it will be displayed
Addtextchangedlistener (this) sets content change monitoring for the input box. In fact, it is very simple. When the content in the input box changes, we need to deal with the display and hiding and clear the small icons. If the content length is not 0, we will display it or hide it. However, we need the focus of the input box to change the display or hiding. Why do we need the focus, For example, in our login interface, we save the user name and password. When we log in to oncreate (), we display the saved password in the user name input box and password input box. The content in the input box changes, resulting in the small clear icons in the user name input box and password input box. Obviously, this is not the effect we want, So a judgment on whether there is focus is added
Setshakeanimation(), which is a method to shake the input box left and right. I have seen similar functions in an application before. When the user name is wrong, the input box will shake. It feels very interesting. In fact, it mainly uses a mobile animation, and then sets the change rate of the animation to a sinusoidal curve, and then directly uses it in the layout file. Effect of use
Android input box control with clear function is over.
Thank you for reading, hope to help you, thank you for your support to this site!