这是利用TextView显示Html代码做的一个简单的Demo。效果如下:
点击“微笑”按钮添加表情,点击“发送”显示。
1.布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".TestTextViewImageActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/editText"
android:layout_width="260dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/send"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="发送" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/smile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="微笑" />
<Button
android:id="@+id/angry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="生气" />
</LinearLayout>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
2.在res/下添加smile.png和angry.png两张图片
3.Activity代码
package com.vicken.test.textview.image;
import java.lang.reflect.Field;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class TestTextViewImageActivity extends Activity implements
OnClickListener {
/** Called when the activity is first created. */
Button smileBtn, angryBtn, sendBtn;
TextView textView;
EditText editText;
String sendString = "";
String[] emoTags = { "/angry", "/smile" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
smileBtn = (Button) findViewById(R.id.smile);
smileBtn.setOnClickListener(this);
angryBtn = (Button) findViewById(R.id.angry);
angryBtn.setOnClickListener(this);
sendBtn = (Button) findViewById(R.id.send);
sendBtn.setOnClickListener(this);
textView = (TextView) findViewById(R.id.textView1);
editText = (EditText) findViewById(R.id.editText);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.send:
textView.setText(formatString(replaeStrWithHtmlTag(editText
.getText().toString())));
editText.setText("");
break;
case R.id.smile:
editText.setText(editText.getText().toString() + "/smile");
break;
case R.id.angry:
editText.setText(editText.getText().toString() + "/angry");
break;
}
}
// 将特定字符串替换为html标签
public String replaeStrWithHtmlTag(String str) {
String replaseStr = str;
for (int i = 0; i < emoTags.length; i++) {
replaseStr = replaseStr.replace(emoTags[i], "<img src='"
+ emoTags[i].substring(1) + "'/>");
}
return replaseStr;
}
// 格式化包含html标签的字符串
public CharSequence formatString(String htmlString) {
CharSequence ch = Html.fromHtml(htmlString, new ImageGetter() {
@Override
public Drawable getDrawable(String source) {
Drawable drawable = getResources().getDrawable(
getResourceId(source));
drawable.setBounds(0, 0, drawable.getIntrinsicWidth() * 2,
drawable.getIntrinsicHeight() * 2);
return drawable;
}
}, null);
return ch;
}
// 利用反射机制,通过资源名字得到资源的ID
public int getResourceId(String resName) {
try {
Field field = R.drawable.class.getField(resName);
return Integer.parseInt(field.get(null).toString());
} catch (Exception e) {
Log.d("TAG", "faild to get resource ID !");
}
return 0;
}
}
PS:当点击“发送”按钮时,首先会将输入内容中的表情标签用html标签封装并代替,再取得html中的图片资源,最后显示。之所以这样做是为了可以将输入的表情标签回删,如下图: