inserting textview inside edittext in android?

2019-06-22 14:00发布

问题:

I am trying to insert a textview inside a Edittext as in below

can anyone pls give me ideas about how to implement it or something.

thanks :)

update: the textviews in the edittext will vary dynamically.

回答1:

I think you cannot set TextView inside EditText. But alternatively you can make it look that way. Here are the steps

  1. convert textview to BitmapDrawable
  2. convert bitmap drawable into SpanableString (ImageSpan)
  3. then set this returned SpannableString to your EditText

I have recently completed this kind of ui and i have explained in my blog Making GoSmsPro/Evernote like EditText



回答2:

Try this sample code. It fits your requirement.

PopupWindow pw = new PopupWindow(
                                 this.getViewInflate().inflate(R.layout.pw_layout, 
                                                               null, true, null), 
                                100,100,true);
// display the popup in the center
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
pw.update(20,20,100,100);

For the views inside EditText part, you 'll have to extend the EditText. Overview of how it should look is as follows.

class emailEditText extends EditText {

List<NamePhotoView> ViewlistOfSelectedContacts; // create new

    void addContact(String name, Bitmap photo) {
       // create a new 'NamePhotoView' and add to the ViewlistOfSelectedContacts
    }

    @Override
    void onDraw(Canvas canvas) {
        for (....) {
            // draw all views from the list. left to right.
        }
    // you have to some how offset the text start coordinate. then call super
    super.onDraw(canvas);
    }
}


回答3:

The way Android implements it, the so called Chips UI, can be found here.
Essentially it's a custom span that draws the components (image, text, etc.) over the text, plus some logic to show the popup.



回答4:

you can create a component based on what you need like you mentioned above with frame layout. You may have a FrameLayout or RelativeLayout with two views such as EditText and TextView while text view is on top of EditText. also you can put image view if you want in right like your picture.

Although you don't need to create a component and easily you can do this in your XML layout, if you have plan to use it regularly through your application it is good practice to create those views as a component.