• 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)  

    ImageGetter 为处理html中<img>的处理器,生成Drawable对象并返回。 

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

    public Drawable getDrawable(String source)  

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

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

    public class AndroidTest2Activity 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.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.icon+""/>";
                     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;  
            }  
        };  

    1.跳转到浏览器直接访问页面,这段代码是在Activity中拷贝来的,所以有startActivity()方法

    Uri uri = Uri.parse("http://www.baidu.com"); //要链接的地址
    
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent);

    2.使用TextView显示HTML方法

    TextView text1 = (TextView)findViewById(R.id.TextView02);
    text1.setText(Html.fromHtml(“<font size='20'>网页内容</font>”));

    3.直接使用android中自带的显示网页组件WebView

    webview = (WebView) findViewById(R.id.WebView01);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.loadUrl("http://www.baidu.com"); 
  • 相关阅读:
    [数分提高]2014-2015-2第6教学周第2次课(2015-04-09)
    牛吃草问题
    [家里蹲大学数学杂志]第395期中科院2015年高校招生考试试题
    PostreSQL崩溃试验全记录
    EnterpriseDb公司的Postgres Enterprise Manager 安装图解
    EnterpriseDB公司的 Postgres Solution Pack (一)
    PostgreSQL调整内存与IO的参数说明
    查内存命令之free
    PPAS通过DBLink,访问Oracle数据库
    PPAS的MTK tool 工具使用说明
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/4206650.html
Copyright © 2020-2023  润新知