Objective
The recent project to do user Login module needs a picture on the right side of the edittext, the picture can set the click Effect, so on the search data made a custom edittext out, easy to reuse later.
Principle
The following is a custom edittext code, the specific difficulty is to achieve the click of the picture Monitor, because the official Google has not yet given a direct implementation of the edittext inside the image of the monitoring API. My practice is to bind the entire control to a ontouchlistener, and then monitor the click event to see if the X coordinates of the click position are within the range of the picture (Getcompounddrawables () [2] 2 is the picture on the right side of the EditText), If yes, the Click event is performed.
Package scut.userlogin;
Import Android.content.Context;
Import Android.util.AttributeSet;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.widget.EditText;
/** * Created by Yany on 2016/7/23. /public class Edittext_passworddisplay extends EditText implements View.ontouchlistener {//need to implement several of the following constructors, otherwise it may not load this
EditText Control public Edittext_passworddisplay (context) {super (context);
Init ();
Public Edittext_passworddisplay (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);
Init ();
Public Edittext_passworddisplay (context, AttributeSet attrs) {Super (context, attrs);
Init ();
}//Initialize the control, bind listener public void init () {Setontouchlistener (this); @Override public boolean Ontouch (View V, motionevent event) {//If not pressed, do not process, if it is pressed but there is no picture, do not process if (event.getact Ion () = = Motionevent.action_up && this.getcompounddrawables () [2]!= null) {//detect if the x-coordinate of the click area is within the picture range if (event.get X () > this.GetWidth ()-This.getpaddingright ()-This.getcompounddrawables () [2].getintrinsicwidth ()] {//Click here to do image processing System
. OUT.PRINTLN ("click Area");
Messageshow.showtoast (GetContext (), "clicked on Picture");
return false;
return false;
}
}
You only need to use this control in XML (remember to add a picture, otherwise it would be equivalent to an ordinary EditText):
<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=
"http://schemas.android.com/apk" /res/android "
xmlns:tools=" Http://schemas.android.com/tools "
android:layout_width=" Match_parent
" android:layout_height= "Match_parent"
android:paddingbottom= "@dimen/activity_vertical_margin"
android: paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
tools:context= "Scut.userlogin.RegisterActivity3" >
<scut.userlogin.edittext_passworddisplay
android:layout_width= "Match_parent"
android: layout_height= "Wrap_content"
android:id= "@+id/edittext_passwordregisterinput"
android:inputtype= " Textpassword "
android:hint=" Please enter the login password "
android:drawableright= @mipmap/ic_launcher"
android:layout _margintop= "50DP"/>
</RelativeLayout>
It is only necessary to load the activity normally:
Private Edittext_passworddisplay et_passwordregisterinput;
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (R.LAYOUT.ACTIVITY_REGISTER3);
Init ();
}
private void Init () {
et_passwordregisterinput = (edittext_passworddisplay) Findviewbyid (r.id.edittext_ passwordregisterinput);
}
To achieve the effect, click on the picture will appear toast:
Reference articles:
Android EditText drawableright property settings Click event
Android to monitor Edittex's image
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.