android 开发webview如何加载p标签的html
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android 开发webview如何加载p标签的html相关的知识,希望对你有一定的参考价值。
本人弄的个类似实例以供参考: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() + "\')");
);
);
参考技术A 如果你要获取P的html代码 然后直接 loadUrl("html代码")就可以了; 参考技术B 本人弄的个类似实例以供参考:
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 adapter=new ArrayAdapter(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() + "')");
);
);
android webview怎么加载ionic的html5
1.如何播放:近来工作要用Android的webview播放HTML5 video标签。做了下试验,用Android自带的Browser可以装载含有video标签的HTML5页面,而且能正常播放,但同样的网页用webview来加载就没法播放HTML5中的video。
在网上查了许多资料,所查的资料可以参考我转载那些文章,经过无数次测试,终于成功了,做法如下:
第一步当然是要先有个Webivew对象,是写在main.xml也好,或者动态创建也罢,总之有了一个Webview对象即可,假设其名为m_webview,然后设置一些属性:
m_webview.getSettings().setJavaScriptEnabled(true);
m_webview.setWebChromeClient(m_chromeClient);
本来以为要设置m_webview.getSettings().setPluginsEnabled(true);但后来发现去掉后也不影响效果。上面主要的是setWebChromeClient这个,要设置一个WebChromeClient对象给webview,WebChromeClient对象这样创建:
private WebChromeClient m_chromeClient = new WebChromeClient()
@Override
public void onShowCustomView(View view, CustomViewCallback callback)
// TODO Auto-generated method stub
; 参考技术A 主要代码如下:
//启用数据库
webSettings.setDatabaseEnabled(true);
String dir = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
//启用地理定位
webSettings.setGeolocationEnabled(true);
//设置定位的数据库路径
webSettings.setGeolocationDatabasePath(dir);
//最重要的方法,一定要设置,这就是出不来的主要原因
webSettings.setDomStorageEnabled(true)
//配置权限(同样在WebChromeClient中实现)
public void onGeolocationPermissionsShowPrompt(String origin,
GeolocationPermissions.Callback callback)
callback.invoke(origin, true, false);
super.onGeolocationPermissionsShowPrompt(origin, callback);
配置权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />本回答被提问者采纳
以上是关于android 开发webview如何加载p标签的html的主要内容,如果未能解决你的问题,请参考以下文章
如何加载图像 url 以适合我在 android 中的 webview?
如何在android Fragment中为webview显示全屏加载微调器?
在android webview应用程序的页面上单击链接时如何防止显示加载微调器?