32Spannable的使用(Android显示html带图片 (表情开发))

Android中显示html文件要用Html.fromHtml(...)处理过的返回值,返回值可以成为setText()的参数。只显示带文本的html可以用下面的方法处理html文件。

public static Spanned fromHtml (String source)  

显示带图片的html要用下面的方法处理html文件。

public static Spanned fromHtml (String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)  
<span style="font-family: Simsun;font-size:14px; text-indent: 2em; background-color: rgb(255, 255, 255);">ImageGetter 为处理html中<img>的处理器,生成Drawable对象并返回。</span>

创建ImageGetter 主要实现下面的方法,source为<img>标签中src属性的值。

public Drawable getDrawable(String source)  

下例为在TextView和EditView中显示html,并插入图片。

下图只显示html文字,点击按钮会在TextView和EditView文本后添加图片。


代码:

package com.example.test;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
	TextView tv;
	EditText et;
	Button addPic;
	String ct;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et=(EditText) this.findViewById(R.id.editText1);
        
        tv=(TextView) this.findViewById(R.id.tv);
        ct="aaa<font color=\"red\">aaa</font>";
        addPic=(Button) this.findViewById(R.id.AddPic);
        addPic.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				ct+="<img src=\""+R.drawable.ic_launcher+"\"/>";
				 refreshView();
			}
        	
        });
       
       refreshView();
        
        
    }
    private void refreshView(){
    	 et.setText(Html.fromHtml(ct,imageGetter,null));
         tv.setText(Html.fromHtml(ct,imageGetter,null));
    }
    ImageGetter imageGetter = new ImageGetter()  
    {  
        @Override  
        public Drawable getDrawable(String source)  
        {  
            int id = Integer.parseInt(source);  
            Drawable d = getResources().getDrawable(id);  
            d.setBounds(0, 0, d.getIntrinsicWidth(), d .getIntrinsicHeight());  
            return d;  
        }  
    };  
}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" 
    
    android:orientation="vertical"
    >

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         />
    <EditText 
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    
    <Button 
        android:id="@+id/AddPic"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        
        />

</LinearLayout>







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一、[Android实例]实现TextView里的文字有不同颜色 转eoe:http://www.eoeandroid.com/thread-4496-1-1.html import android.text.Html; TextView t3 = (TextView) findViewById(R.id.text3); t3.setText( Html.fromHtml( "<b>text3:</b> Text with a " + "<a href=\"http://www.google.com\">link</a> " + "created in the Java source code using HTML.")); 二、TextView显示html文件中的图片 转javaeye:http://da-en.javaeye.com/blog/712415 我们知道要让TextView解析和显示Html代码。可以使用 Spanned text = Html.fromHtml(source); tv.setText(text); 来实现,这个用起来简单方便。 但是,怎样让TextView也显示Html中<image>节点的图像呢? 我们可以看到fromHtml还有另一个重构: fromHtml(String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler) 实现一下ImageGetter就可以让图片显示了: ImageGetter imgGetter = new Html.ImageGetter() { @Override public Drawable getDrawable(String source) { Drawable drawable = null; drawable = Drawable.createFromPath(source); // Or fetch it from the URL // Important drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable .getIntrinsicHeight()); return drawable; } }; 至于TagHandler,我们这里不需要使用,可以直接传null。 参考文档: http://tech-droid.blogspot.com/2010/06/textview-with-html-content.html英语好的朋友就直接看英文文档吧。 三、Android---文字中插入表情 转载自:http://blog.163.com/spf9190@126/blog/static/50207531201091545954587/ 这段时间在做一个短信项目,需要实现短信中插入表情的功能,本一位非常困难,经过一段时间的研究,发现还是比较簡単的,现在总结如下。 以短信输入框为例,短信的输入框是一个EditText,它的append方法不仅可以加入字符串,还可以添加HTML标记。以下就是使用HTML标记添加表情的具体操作。 首先需要构建一个ImageGetter,作用是通过HTML标记获得对应在res目录下的图片: ImageGetter imageGetter = new ImageGetter() { @Override public Drawable getDrawable(String source) { int id = Integer.parseInt(source); //根据id从资源文件中获取图片对象 Drawable d = getResources().getDrawable(id); d.setBounds(0, 0, d.getIntrinsicWidth(),d.getIntrinsicHeight()); return d; } }; 然后就可以直接往EditText视图中添加 inputLable.append(Html.fromHtml("<img src='"+clickedImageId+"'/>", imageGetter, null)); 其中 Html.fromHtml("<img src='"+clickedImageId+"'/>"就是HTML图片标记,在Android中支持了部分HTML标记的使用(这方面我还在继续研究),HTML标记必须被Html.fromHtml修饰。imageGetter即为之前创建的ImageGetter类型的对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值