How to replace String with an image inside an EditText?

前端 未结 2 986
生来不讨喜
生来不讨喜 2021-01-24 12:13

I am currently working on a simple chat application. I want to add emoticons features in this app. I already have a function to receive string from other users. What I want is-

2条回答
  •  面向向阳花
    2021-01-24 12:24

    It can easily be implemented by using SpannableString Here is a sample implementation in code.

    package com.jslan.nabin.emosample;
    
    import android.graphics.drawable.Drawable;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.text.Spannable;
    import android.text.SpannableString;
    import android.text.style.ImageSpan;
    import android.widget.EditText;
    
    
    public class MainActivity extends ActionBarActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            EditText textBox = (EditText) findViewById(R.id.textBox);//I have created an EditText named textBox in activity_main.xml
            Drawable d;
            ImageSpan span;
            String abc = "some :D message :D";
            SpannableString ss = new SpannableString(abc);
            for(int i = 0; i

提交回复
热议问题