展会信息港展会大全

Android SDK支持那些方式显示富文本信息?以及文本插入
来源:互联网   发布日期:2015-11-15 10:14:50   浏览:1752次  

导读:Android SDK支持富文本信息方式1.使用TextView 组件可以显示富文本信息,在TextView组件中可以使用富文本标签显示富文本信息,这种标签类似于HTML标签,但比HTML标签简单。支持有限的几 种显示富文本的方式。......

Android SDK支持富文本信息方式

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

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

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

>上面3种方法都支持图文混排效果。但第一张那个方法在显示图像是(使用标签)需要实现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);

// 使用标签在TextView组建中显示图像

CharSequence charSequence = Html.fromHtml("", 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);

// 使用标签

CharSequence c = Html.fromHtml("cheyanxu");

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);//光标所在位置插入文字

赞助本站

人工智能实验室

相关热词: 富文本 插入

相关内容
AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港