Android SDK支持那些方式显示富文本信息?以及文本插入

Android SDK支持富文本信息方式

1.使用TextView 组件可以显示富文本信息,在TextView组件中可以使用富文本标签显示富文本信息,这种标签类似于HTML标签,但比HTML标签简单。支持有限的几种显示富文本的方式。如<font>标签用于设置文字和颜色,<b>用于设置粗体。包含这些标签的文本不能直接作为TextView.setText方法的参数值。而要先使用Html.fromHtml方法将这些文本转化成CharSequence对象,然后再将该对象作为TextView.setText方法的参数值。

>使用WebView组件显示HTML页面。

>继承View类或其子类,并覆盖onDraw方法,在该方法找那个直接绘制富文本或图像。

>上面3种方法都支持图文混排效果。但第一张那个方法在显示图像是(使用<img>标签)需要实现ImageGetter接口,并通过ImageGetter.getDrawable方法返回封装图像资源的Drawable对象。

>在TextView 组件中显示图像还可以使用ImageSpan对象,ImageSpan 对象用于封装Bitmap 对象,并通过SpannableString对象封装ImageSpan对象,最后将SpanableString对象作为TextView.setText方法参数值将图像显示在TextView组件上。

 

public class AndrodTActivity extends Activity implements OnClickListener {
 TextView tv_;
 TextView tv1;
 TextView tv2;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  tv_ = (TextView) findViewById(R.id.tv_);
  tv1 = (TextView) findViewById(R.id.tv1);
  tv2 = (TextView) findViewById(R.id.tv2);
  // 使用<img>标签在TextView组建中显示图像
  CharSequence charSequence = Html.fromHtml("<img>", new ImageGetter() {
   @Override
   public Drawable getDrawable(String source) {
    // 装载图像资源
    Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);
    // 设置要显示图像的大小(按原大小显示)
    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    return drawable;
   }
  }, null);
  tv_.setText(charSequence);
  // 使用<font>标签
  CharSequence c = Html.fromHtml("<font color=\"#ff0000\" size=\"30\">cheyanxu</font>");
  tv1.setText(c);

  // 使用ImageSpan对象在TextView组建中显示图像
  Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
  // 根据bitmap创建ImageSpan对象
  ImageSpan imageSpan = new ImageSpan(bitmap);
  // 创建一个SpanableString对象,以便插入用ImageSpan对象封装的图像
  SpannableString spannableString = new SpannableString("icon");
  // 用ImageSpan对象替换icon
  spannableString.setSpan(imageSpan, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  // 将图像显示在TextView组件上
  tv2.setText(spannableString);
  tv2.append("cheyanxu");
  tv2.append(spannableString);
 }

 @Override
 public void onClick(View v) {

 }

}

来个具体的回答。 包括文本操作(连接,插入):

EditText mTextInput=(EditText)findViewById(R.id.input);//EditText对象

int index = mTextInput.getSelectionStart();//获取光标所在位置

String text="I want to input str";

(方法1):

Editable edit = mTextInput.getEditableText();//获取EditText的文字

if (index < 0 || index >= edit.length() ){

       edit.append(text);

}else{

      edit.insert(index,text);//光标所在位置插入文字

 

转载于:https://my.oschina.net/u/573470/blog/122103

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值