在TextView或者編輯框EditText中添加圖片的方法,代碼如下:
為和林格爾等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及和林格爾網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都做網(wǎng)站、網(wǎng)站制作、和林格爾網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
import java.util.regex.Matcher; import java.util.regex.Pattern; import android.os.Bundle; import android.app.Activity; import android.text.Spannable; import android.text.SpannableStringBuilder; import android.text.style.ImageSpan; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv = (TextView) findViewById(R.id.tv); String str = "sdfasd★fasljeojg★fad falsdkjf★asjdkf alsdj★foawpjf sajfadsjf ladjfa ls"; SpannableStringBuilder builder = new SpannableStringBuilder(str); String flag = "★"; Pattern pattern = Pattern.compile(flag); Matcher matcher = pattern.matcher(str); while (matcher.find()) { builder.setSpan(new ImageSpan(this, R.drawable.ic_launcher), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } tv.setText(builder); EditText ed = (EditText) findViewById(R.id.editText1); ed.setText(builder); } }
布局文件代碼如下:
效果圖: