Android custom EditText with picture control on the right

preface

Recently, the user login module of the project needs an EditText with a picture on the right. The picture can set the click effect, so I checked the data and made a custom EditText to facilitate reuse in the future.

principle

The following is the code for customizing EditText. The specific difficulty is to realize the click monitoring of pictures, because Google officials have not given a monitoring API to directly implement the pictures in EditText. My approach is to bind an ontouchlistener to the whole control, and then monitor the click event to detect whether the X coordinate of the click position is within the coverage of the picture (2 in getcomposundrawables() [2] below represents the picture on the right side of EditText). If so, execute the click event.

Just use this control in XML (remember to add pictures, otherwise it will be equivalent to an ordinary EditText):

In the activity, you only need to load normally:

To achieve the effect, click the picture and toast will appear:

Reference article:

The drawableright property of EditText in Android sets the click event

Android monitors edittex pictures

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
分享
二维码
< <上一篇
下一篇>>