Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the function of cleaning and jitter through custom EditText in Android

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to clear and jitter through custom EditText in Android". In daily operation, I believe that many people have doubts about how to clear and jitter through custom EditText in Android. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to clear and jitter through custom EditText in Android". Next, please follow the editor to study!

The source code is as follows

Public class ClearEditText extends EditText implements View.OnFocusChangeListener,TextWatcher {/ * reference to delete button * / private Drawable mClearDrawable; / * whether the control has focus * / private boolean hasFoucs; public ClearEditText (Context context) {this (context, null) } public ClearEditText (Context context, AttributeSet attrs) {/ / the construction method here is also very important. Without adding many attributes, you cannot define this (context, attrs, android.R.attr.editTextStyle) in XML;} public ClearEditText (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); init () } private void init () {/ / get the DrawableRight of EditText. If it is not set, we will use the default picture. 2 is to get the order of the picture on the right is left, upper and right.) mClearDrawable = getCompoundDrawables () [2]; if (mClearDrawable = = null) {/ / throw new / / NullPointerException ("You can add drawableRight attribute in XML"); mClearDrawable = getResources (). GetDrawable (R.drawable.icon_clear_input) } mClearDrawable.setBounds (0,0, mClearDrawable.getIntrinsicWidth (), mClearDrawable.getIntrinsicHeight ()); / / default sets the hidden icon setClearIconVisible (false); / / sets the listening setOnFocusChangeListener (this) for focus changes; / / sets the listening addTextChangedListener (this) for changes in the input box. } / * because we cannot directly set the click event to the EditText, we simulate the click event by remembering where we press it when we press it in the width of the EditText-* the gap between the icon and the right side of the control-the width of the icon and the width of the EditText-the gap between the icon and the right side of the control The vertical direction does not consider * / @ Override public boolean onTouchEvent (MotionEvent event) {if (event.getAction () = = MotionEvent.ACTION_UP) {if (getCompoundDrawables () [2]! = null) {boolean touchable = event.getX () > (getWidth ()-getTotalPaddingRight ()) & & (event.getX ())

< ((getWidth() - getPaddingRight()))); if (touchable) { this.setText(""); } } } return super.onTouchEvent(event); } / * 当ClearEditText焦点发生变化的时候,判断里面字符串长度设置清除图标的显示与隐藏 */ @Override public void onFocusChange(View v, boolean hasFocus) { this.hasFoucs = hasFocus; if (hasFocus) { setClearIconVisible(getText().length() >

0);} else {setClearIconVisible (false);}} / * set the display and hiding of the clear icon. Call setCompoundDrawables to draw the EditText * * @ param visible * / protected void setClearIconVisible (boolean visible) {Drawable right = visible? MClearDrawable: null; setCompoundDrawables (getCompoundDrawables () [0], getCompoundDrawables () [1], right, getCompoundDrawables () [3]);} / * callback method * / @ Override public void onTextChanged (CharSequence s, int start, int count, int after) {if (hasFoucs) {setClearIconVisible (s.length () > 0) when the content in the input box changes } @ Override public void beforeTextChanged (CharSequence s, int start, int count,int after) {} @ Override public void afterTextChanged (Editable s) {} / * animate the sloshing * / public void setShakeAnimation () {this.startAnimation (shakeAnimation (5)) } / * wobble animation * * @ param counts * how many times to wobble in 1 second * @ return * / public static Animation shakeAnimation (int counts) {Animation translateAnimation = new TranslateAnimation (0,10,0,0); / / set a circular accelerator, the number of times passed in will produce the effect of wobble. TranslateAnimation.setInterpolator (new CycleInterpolator (counts)); translateAnimation.setDuration; return translateAnimation;}}

The method of use is the same as the normal EditText:

At this point, the study on "how to clear and jitter through custom EditText in Android" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report