Detailed explanation of the use of Android custom control EditText

This example shares the specific code of Android custom control EditText for your reference. The specific contents are as follows

There are three types of custom controls:

1. Self drawn control 2. Combined control 3. Inherited control

After the code has been uploaded to GitHub, all custom controls will be placed in this warehouse

demand

Here, due to the needs of the project, a user-defined EditText is implemented, mainly including two points. One is the tool icon, for example, click to clear the EditText content. One is the prompt icon hintin on the left of EditText, such as the icon in front of the account password.

In order to make this control more extensible, two click event interfaces are set. For toolicon, the default click event is to clear EditText content. If it needs to be changed, set relevant click events in the code.

step

Inherit EditText, write attrs.xml, create declare styleable, and write myedittext for use in layout

realization

Gets the properties set in the layout file

What is returned here is a typedarray array. After obtaining it, you can obtain the properties set in the layout file

Set resource picture

EditText inherits from textview. There are two methods in textview

The first method is to set the location of the resource picture. The difference between the first method and the second method is that in the first method, the size of the resource picture is obtained by the system, and the second method is to set the size through layoutparams.

Set click event

The pictures we set through setcomposundrawables() and other methods. Since there is no relevant picture click processing interface provided in the parent class, ontouchevent() can be overridden to realize relevant click events. We can judge whether our fingers click on relevant pictures only according to the position of the falling point or lifting point of our fingers. Here, I choose to handle when the finger is raised

Complete code

attrs.xml:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>