Android--> easily creates an input box with a Delete button (EditText) with emoji expression filtering

Source: Internet
Author: User

Input box with delete button, this is standard, the implementation of a lot of ways, open source is also a lot of.

But, nothing is like a blind toss.

Speak.

Only on the basis of the original to add the extension. Relatively few intrusions, using the same method as the original. No castration.

Full code:

 Public  class exedittext extends appcompatedittext {Rect Clearrect =NewRect (); Public Exedittext(Context context) {Super(context); } Public Exedittext(context context, AttributeSet attrs) {Super(context, attrs); } Public Exedittext(context context, AttributeSet attrs,intDEFSTYLEATTR) {Super(Context, attrs, defstyleattr); }@Override    protected void Onattachedtowindow() {Super. Onattachedtowindow ();//Add emoji expression filtering without affecting the default filter rules        if("Emoji". Equalsignorecase (String.valueof (Gettag ()))) {FinalInputfilter[] Filters = Getfilters ();Finalinputfilter[] Newfilters =NewInputfilter[filters.length +1]; System.arraycopy (Filters,0, Newfilters,0, filters.length); Newfilters[filters.length] =NewEmojifilter ();        SetFilters (newfilters); }    }@Override    protected void onfocuschanged(BooleanFocusedintdirection, Rect Previouslyfocusedrect) {Super. onfocuschanged (focused, direction, previouslyfocusedrect);///focus change, check if you need to show Delete buttonCheckedit (focused); }@Override    protected void onsizechanged(intWintHintOLDW,intOLDH) {Super. onsizechanged (W, H, OLDW, OLDH);//To calculate the rectangular area where the Delete button is locatedClearrect.set (W-getpaddingright ()-Math.min (W, h), Getpaddingtop (), W-getpaddingright (), Math.min (W, h)-GetPaddingB    Ottom ()); }@Override     Public Boolean ontouchevent(Motionevent event) {//Check Click to delete button area and currently have focus        if(event.getaction () = = Motionevent.action_down && isFocused ()) {//Check if the button rectangle area is being deleted, press the            if(Checkclear (Event.getx (), Event.gety ())) {//If the current is empty, do not handle touch events, otherwise empty the text to achieve the effect of deletion                if(! Textutils.isempty (GetText ())) {SetText ("");return true; }            }        }return Super. Ontouchevent (event); }Private void Checkedit(BooleanFocused) {if(Textutils.isempty (GetText ()) | |!focused) {Setcompounddrawableswithintrinsicbounds (0,0,0,0); }Else{//Here is the key, using the native method to display the delete icon.Setcompounddrawableswithintrinsicbounds (0,0, R.drawable.base_edit_delete,0); }    }//coordinates are in the button area    Private Boolean Checkclear(floatXfloatY) {returnClearrect.contains ((int) x), (int) y); }@Override    protected void ontextchanged(charsequence text,intStartintLengthbefore,intLengthafter) {Super. ontextchanged (text, start, Lengthbefore, Lengthafter);//Time to checkCheckedit (true); }}

Emoji Expression Filter:

May be some special emoji expression, or the character expression can not be filtered, sorry;
If you find, please tell, let us perfect together, thank you!

 Public  class emojifilter implements inputfilter {Pattern Emojipattern = Pattern.compile ("[\ud83c\udc00-\ud83c\udfff]| [\ud83d\udc00-\ud83d\udfff]| [\U2600-\U27FF] ", Pattern.unicode_case | pattern.case_insensitive);Private intMmax =-1;//No filter length by default     Public Emojifilter() {    } Public Emojifilter(intMax) {Mmax = max; }/** * Detects if there is a emoji expression * * @param source * @return  * *     Public Static Boolean Containsemoji(Charsequence Source) {intLen = Source.length (); for(inti =0; i < Len; i++) {CharCodepoint = Source.charat (i);if(!isemojicharacter (codepoint)) {//If it does not match, then the character is a emoji expression                return true; }        }return false; }/** * Determines if emoji * * @param a single character compared to codepoint * @return  */    Private Static Boolean Isemojicharacter(CharCodepoint) {return(Codepoint = =0x0) || (Codepoint = =0x9) || (Codepoint = =0xA) || (Codepoint = =0xD) || ((Codepoint >=0x20) && (codepoint <=0xd7ff)) || ((Codepoint >=0xe000) && (codepoint <=0xFFFD)) || ((Codepoint >=0x10000) && (codepoint <=0x10ffff)); }@Override     PublicCharsequenceFilter(Charsequence source,intStartintEnd, spanned dest,intDStart,intDEnd) {if( This. Emojipattern.matcher (source). Find () | | Containsemoji (source)) {return ""; }if(Mmax! =-1) {intKeep = Mmax-(Dest.length ()-(Dend-dstart));if(Keep <=0) {return ""; }Else if(Keep >= End-start) {return NULL;//Keep original}Else{keep + = start;if(Character.ishighsurrogate (Source.charat (Keep-1)) {--keep;if(keep = = start) {return ""; }                }returnSource.subsequence (start, keep); }        }returnSource }}

At this point: The article is over, if there is doubt: QQ Group android:274306954 swift:399799363 welcome you to join.

Android--> easily creates an input box with a Delete button (EditText) with emoji expression filtering

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.