android 开发webview如何加载html

 我来答
匿名用户
2014-11-12
展开全部
本人弄的个类似实例以供参考:
 
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
 
@SuppressLint("SetJavaScriptEnabled")
public class Onlinedic extends Activity {
        private TextView result;
        private EditText editText;
        private WebView webView;
        private String[] url = {"file:///android_asset/cn2en.html","file:///android_asset/en2cn.html",
                        "file:///android_asset/ge2cn.html","file:///android_asset/cn2ge.html",
                        "file:///android_asset/en2ge.html","file:///android_asset/ge2en.html"};
        private String[] chooseStr={"英","中","德","中","德","英"};
        private Handler tHandler = new Handler();
                 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.onlinedic);
         
        webView=(WebView)findViewById(R.id.toutput);
        final Button submit=(Button)findViewById(R.id.submit);
        editText=(EditText)findViewById(R.id.tinput);
        result=(TextView)findViewById(R.id.result);
        final Spinner l_select=(Spinner)findViewById(R.id.l_select);
   
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, 
                        android.R.layout.simple_spinner_item,chooseStr);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
        l_select.setPrompt("请选择翻译的方式");
        l_select.setOnItemSelectedListener(new OnItemSelectedListener() {
 
                        @Override
                        public void onItemSelected(AdapterView<?> arg0, View arg1,
                                        int arg2, long arg3) {
                                if(((TextView)arg1).getText()==chooseStr[0])
                                        webView.loadUrl(url[0]);
                                if(((TextView)arg1).getText()==chooseStr[1])
                                        webView.loadUrl(url[1]);
                                if(((TextView)arg1).getText()==chooseStr[2])
                                        webView.loadUrl(url[2]);
                                if(((TextView)arg1).getText()==chooseStr[3])
                                        webView.loadUrl(url[3]);
                                if(((TextView)arg1).getText()==chooseStr[4])
                                        webView.loadUrl(url[4]);
                                if(((TextView)arg1).getText()==chooseStr[5])
                                        webView.loadUrl(url[5]);
                        }
 
                        @Override
                        public void onNothingSelected(AdapterView<?> arg0) {
                                webView.loadUrl(url[0]);
                        }
                });
        l_select.setAdapter(adapter);
         
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSaveFormData(false);
        webSettings.setSavePassword(false);
        webSettings.setSupportZoom(false);
         
        submit.setOnClickListener(new OnClickListener() {                        
                        @Override
                        public void onClick(View v) {
                                result.setVisibility(TextView.VISIBLE);
                                webView.setVisibility(WebView.VISIBLE);
                                tHandler.post(new Runnable(){
                                public void run(){
                                  if (editText.getText().toString() != ""){
                                          webView.loadUrl("javascript:translate('"+ 
                                                          editText.getText().toString() + "')");
                                  }
                                }
                            });
                        }
                });
    }
}
回忆526
推荐于2016-09-10 · TA获得超过4165个赞
知道大有可为答主
回答量:3276
采纳率:87%
帮助的人:1893万
展开全部

1.String customHtml = "<html><body><font color='red'>hello baidu!</font></body></html>";

首先写了一个html代码段,用来显示一段红色的字体;

2.webview.loadData(customHtml, "text/html", "UTF-8");  加载定义的代码,并设定编码格式和字符集;

3.运行效果;如图:

        

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式