This article shares the specific code of Android custom control EditText for your reference. The specific content is as follows
There are three types of custom controls:
1. Self-drawing control
2. Combination controls
3. Inherit the control
The code has been uploaded togithub
In the future, custom controls will be placed in this repository
need
Here, due to the need for the project, a custom EditText is implemented, which mainly implements two points. One is the tool icon toolIcon, for example, click to clear the EditText content. A hintIcon on the left side of EditText, such as the icon in front of the account password.
In order to make this control more scalable, two click event interfaces are set up. For toolIcon, the default click event is to clear EditText content. If you need to change it, set the relevant click event in the code.
step
Inherit EditText
Write, create declare-styleable
Write MyEditText
Used in layout
accomplish
Get the properties set in the layout file
Here is a TypedArray array. After obtaining it, you can get the properties set in the layout file.
private void init(Context context, AttributeSet attrs) { TypedArray typedArray = ().obtainStyledAttributes( attrs, , 0, 0); hintIcon = (.MyEditText_hintIcon); toolIcon = (.MyEditText_toolIcon); fixed = (.MyEditText_fixed, true); if (toolIcon != null && fixed) { setHeight(()); } setCompoundDrawablesWithIntrinsicBounds(hintIcon, null, null, null); setCompoundDrawablePadding(10); (); onClickListenerWithEditTextToolIcon = new OnClickListenerWithEditTextToolIcon() { @Override public void onClick() { setText(""); } }; }
Set resource pictures
EditText is inherited from TextView. There are two methods in TextView.
setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom) setCompoundDrawables(left, top, right, bottom)
It is to set the location of the resource picture. The difference between the first method and the second method is that the size of the resource picture in the first method is the system to obtain the inherent size of the image, and the second method is to set the size by yourself through LayoutParams.
Setting up click events
We set the pictures through setCompoundDrawables() and other methods. Since there is no relevant image click processing interface in the parent class, we can rewrite onTouchEvent() to achieve related click events. We only need to determine whether the finger has clicked on the relevant image based on the position of the landing point or lifting point of our finger. Here, I chose to handle it when my fingers are lifted
/** * Override the touchEvent to judge whether click toolIcon or hintIcon * * @param event motionEvent * @return super */ @Override public boolean onTouchEvent(MotionEvent event) { if (() == MotionEvent.ACTION_UP) { if (hintIcon != null) { if (() < () && () > 0) { if (getCompoundDrawables()[0] != null && onClickListenerWithEditTextHintIcon != null) { (); } } } if (toolIcon != null) { if (() > (getWidth() - ()) && () < getWidth()) { if (getCompoundDrawables()[2] != null ) { (); } } } } return (event); } /** * interface when click hintIcon */ public interface OnClickListenerWithEditTextHintIcon { void onClick(); } /** * interface when click toolIcon */ public interface OnClickListenerWithEditTextToolIcon { void onClick(); }
Complete code
package ; import ; import ; import ; import ; import ; import ; import ; /** * Custom widget of EditText with two icon. * Created by lizhongquan on 16-1-6. */ public class MyEditText extends EditText { private Drawable hintIcon = null; private Drawable toolIcon = null; /** * HintIcon clickListener */ private OnClickListenerWithEditTextHintIcon onClickListenerWithEditTextHintIcon = null; /** * Default clear the EditText */ private OnClickListenerWithEditTextToolIcon onClickListenerWithEditTextToolIcon = null; /** * Default fixed the Height */ private boolean fixed = true; public MyEditText(Context context) { super(context); } public MyEditText(Context context, AttributeSet attrs) { super(context, attrs); init(context, attrs); } public MyEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs); } private void init(Context context, AttributeSet attrs) { TypedArray typedArray = ().obtainStyledAttributes( attrs, , 0, 0); hintIcon = (.MyEditText_hintIcon); toolIcon = (.MyEditText_toolIcon); fixed = (.MyEditText_fixed, true); if (toolIcon != null && fixed) { setHeight(()); } setCompoundDrawablesWithIntrinsicBounds(hintIcon, null, null, null); setCompoundDrawablePadding(10); (); onClickListenerWithEditTextToolIcon = new OnClickListenerWithEditTextToolIcon() { @Override public void onClick() { setText(""); } }; } /** * Override the touchEvent to judge whether click toolIcon or hintIcon * * @param event motionEvent * @return super */ @Override public boolean onTouchEvent(MotionEvent event) { if (() == MotionEvent.ACTION_UP) { if (hintIcon != null) { if (() < () && () > 0) { if (getCompoundDrawables()[0] != null && onClickListenerWithEditTextHintIcon != null) { (); } } } if (toolIcon != null) { if (() > (getWidth() - ()) && () < getWidth()) { if (getCompoundDrawables()[2] != null ) { (); } } } } return (event); } /** * the clickListener of click hintIcon * * @param clickListenerOfHintIcon OnClickListenerWithEditTextHintIcon */ public void setOnClickListenerWithEditTextHintIcon( OnClickListenerWithEditTextHintIcon clickListenerOfHintIcon) { = clickListenerOfHintIcon; } /** * the clickListener of click toolIcon * * @param clickListenerOfToolIcon OnClickListenerWithEditTextToolIcon */ public void setOnClickListenerWithEditTextToolIcon( OnClickListenerWithEditTextToolIcon clickListenerOfToolIcon) { = clickListenerOfToolIcon; } /** * onTextChange * * @param text text * @param start start * @param lengthBefore lengthBefore * @param lengthAfter lengthAfter */ @Override protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { (text, start, lengthBefore, lengthAfter); if (() > 0 && getCompoundDrawables()[2] == null && toolIcon != null) { // (10, 0, 10, 0); setCompoundDrawablesWithIntrinsicBounds(hintIcon, null, toolIcon, null); } if (() == 0 && getCompoundDrawables()[2] != null && toolIcon != null) { setCompoundDrawablesWithIntrinsicBounds(hintIcon, null, null, null); } } /** * interface when click hintIcon */ public interface OnClickListenerWithEditTextHintIcon { void onClick(); } /** * interface when click toolIcon */ public interface OnClickListenerWithEditTextToolIcon { void onClick(); } }
:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyEditText"> <attr name="hintIcon" format="integer" /> <attr name="toolIcon" format="integer" /> <attr name="fixed" format="boolean" /> </declare-styleable> </resources>
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.