android 开发webview如何加载html
2个回答
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() + "')");
}
}
});
}
});
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询