Imitation millet sticky note text mixed edittext solve the end of the inserted word bug

Source: Internet
Author: User

Always want to achieve like millet sticky notes, such as text-to-picture mixed results, the way to collect online is nothing more than three kinds:
1, the custom layout, each picture is a imageview, inserts the picture to insert the EditText, the disadvantage is realizes the complexity, cannot select the picture and the text as well as the millet sticky note
2, through the html.fromhtml (source), the picture can be loaded into the imagegetter, the implementation of no bug, but only display HTML, when EditText SetText, want to remove the previous HTML format
The picture gets an obj character and looks at a lot of blogs, including StackOverflow and does not give a way to parse the spanned object from the editable. If anyone has a way to hope not stingy tell me.
3, through Imagespan and spannablestring, this is my method of implementation, and is more ideal, not only can write EditText, but also from the EditText to parse out the text mix layout.

1. Insert picture to Cursor position

/** * Map the picture path to bitmap, and then show it to EditText via spannablestring and Imagespan*/    Private voidSetimageview () {//If the edittext already has a imagespan of the same resource, the picture is no longer readImagespan Imagespan =getimagespanfromexistence (ImagePath); if(Imagespan! =NULL) {insertintoeditor (Imagespan, ImagePath); return; }         if(ImagePath! =NULL&& (!imagepath.equals ("NULL"))                && (!"". Equals (ImagePath)))            {Insertintoeditor (ImagePath); /** No longer using cached mode*/        }    } /** * Get Imagespan from the current edittext, or return NULL if it exists * * @return*/    PrivateImagespan getimagespanfromexistence (String source) {Editable edit=Contenttext.gettext (); Imagespan[] Spans= Edit.getspans (0, Edit.length (), Imagespan.class);  for(Imagespan ip:spans) {intStart =Edit.getspanstart (IP); intEnd =edit.getspanend (IP); String Path=edit.tostring (). substring (start, end); Path= Path.substring (5, Path.length ()-5); if(source.equals (path)) {LOGG.D ("Find existed Imagespan"); return NewImagespan (Ip.getdrawable (), imagespan.align_baseline); }        }        return NULL; } /** * insert Imagespan to cursor position, for EditText already has imagespan case * * @param IP * imagespan * @param path * Path*/    Private voidInsertintoeditor (imagespan span, String path) {if((""+ Path +"img>"). Length () +contenttext.gettext (). Length () >max_content) {Toast.maketext (Getapplicationcontext (), R.string. Toast_reached_max_text, -). Show (); return; } spannablestring SS=NewSpannablestring (""+ Path +"img>"); if(Span = =NULL)            Throw NewNullPointerException ("span cant be null"); Ss.setspan (span,0, (""+ Path +"img>"). Length (), spannable.span_exclusive_exclusive); Editable et= Contenttext.gettext ();//get the content in EditText first        intStart =Contenttext.getselectionstart (); Et.insert (Start, SS);//set the position of the SS to be addedContenttext.setselection (start + ss.length ());//the cursor is displayed in the last face of the EditText settingLOGG.D ("Insertintoeditor by using existed Imagespan"); } /** * insert Imagespan to cursor position, for edittext without picture * * @param path * picture Paths*/    Private voidinsertintoeditor (String path) {if((""+ Path +"img>"). Length () +contenttext.gettext (). Length () >max_content) {Toast.maketext (Getapplicationcontext (), R.string. Toast_reached_max_text, -). Show (); return; } spannablestring SS=NewSpannablestring (""+ Path +"img>"); //cache mode is no longer used//Bitmap BM = meditorhelper.getimage (path);Bitmap BM =Picturehelper.getimagefrompath (ImagePath, ScreenWidth*0.7F, ScreenWidth *0.7F,false, -, Editor. This, Imgpadding,false); if(BM = =NULL) {            Throw NewNullPointerException ("BM cant be null"); } Imagespan span=NewImagespan ( This, BM, imagespan.align_baseline); Ss.setspan (span,0, (""+ Path +"img>"). Length (), spannable.span_exclusive_exclusive); Editable et= Contenttext.gettext ();//get the content in EditText first        intStart =Contenttext.getselectionstart (); Et.insert (Start, SS);//Insert Picture at cursor//contenttext.setselection (start + ss.length ());//the cursor is displayed in the last face of the EditText settingLOGG.D ("insertintoeditor by loading new"); }

Before inserting the picture will look for EditText have the same address of the Imagespan, if there is no longer read the new bitmap, direct reuse drawable

2, solve the rear insert bug\

Bug Source: http://www.baidufe.com/item/65fd7eba51123bbe80bc.html
I haven't been able to understand the author's half-angle stance.
I implemented a method that, when the cursor reaches the end of the picture, jumps directly to the correct position
Implementation: http://mxw3755.iteye.com/admin/blogs/2164905
When the cursor is embedded in the spannablestring character from the end of the previous bit, we can place the cursor at position +1, so that the cursor will be between two pictures
3. How to save the edittext of the Analytic graphics and text mix

You can save the location of the Imagespan to ArrayList, and how to save to the database refer to the http://mxw3755.iteye.com/admin/blogs/2165147

Android graphics combined, using spannablestring and Imagespan class

Drawable drawable =getresources (). getdrawable (ID); Drawable.setbounds (0,0, Drawable.getintrinsicwidth (), Drawable.getintrinsicheight ()); //The text that needs to be processed, [smile] is the text that needs to be substitutedSpannablestring spannable =NewSpannablestring (GetText (). toString () +"[Smile]"); //to replace the specified text with a picture, you need to use ImagespanImagespan span =NewImagespan (drawable, imagespan.align_baseline); //start replacing, note that the 2nd and 3rd parameters indicate where to start replacing where to replace end (start and end)//The last parameter is similar to a set in mathematics, [5,12] represents 5 to 12, including 5 but excludingSpannable.setspan (span, GetText (). Length (), GetText (). Length () +"[Smile]". Length (), spannable.span_inclusive_exclusive); SetText (spannable); 

Fake millet Sticky notes mixed edittext to solve the trailing insert text bug

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.