admin管理员组文章数量:1130349
1.编写布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android/apk/res/android"
xmlns:app="http://schemas.android/apk/res-auto"
xmlns:tools="http://schemas.android/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.dejunmonsdk.WebViewActivity">
<ProgressBar
android:id="@+id/commonsdk_webview_pb"
android:layout_width="match_parent"
android:layout_height="5dp"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"/>
<WebView
android:id="@+id/commonsdk_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#afe"></WebView>
</LinearLayout>
2.WebView的使用
public class WebViewActivity extends BaseActivity {
private WebView mCommonsdkWebview;
private ProgressBar mCommonsdkWebviewPb;
public static final String WEB_VIEW_URL="web_view_url";
private String url;
public static final int PB_MAX=100;//进度条的最大值
public static void startWenViewActivity(BaseActivity fromActivity,String url){
Bundle bundle=new Bundle();
bundle.putString(WEB_VIEW_URL,url);
fromActivity.openActivityWithIntent(WebViewActivity.class,bundle);
}
@Override
protected BasePresenter createPresenter() {
return null;
}
@Override
protected void initView() {
mCommonsdkWebview = (WebView) findViewById(R.idmonsdk_webview);
mCommonsdkWebviewPb = (ProgressBar) findViewById(R.idmonsdk_webview_pb);
mCommonsdkWebviewPb.setMax(PB_MAX);
}
@Override
protected void initData(Bundle savedInstanceState) {
url=getIntent().getStringExtra(WEB_VIEW_URL);
mCommonsdkWebview.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {//设置网页加载进度
super.onProgressChanged(view, newProgress);
if (newProgress>=100){
mCommonsdkWebviewPb.setVisibility(View.GONE);
}else{
mCommonsdkWebviewPb.setProgress(newProgress);
}
}
});
mCommonsdkWebview.setWebViewClient(new WebViewClient(){
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)//android5.0以上手机才有效果
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return super.shouldOverrideUrlLoading(view, request);
}
});
WebSettings settings = mCommonsdkWebview.getSettings();
settings.setDomStorageEnabled(true);//这设置存储 不设置显示不出来
settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);//没有网使用缓存
settings.setJavaScriptEnabled(true);//设置网页可操作
settings.setUseWideViewPort(true);//设置视图可缩放和下面一起设置 似乎没效果
settings.setLoadWithOverviewMode(true);
mCommonsdkWebview.loadUrl(url);
}
@Override
protected int getLayoutId() {
return R.layout.activity_web_view;
}
}1.编写布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android/apk/res/android"
xmlns:app="http://schemas.android/apk/res-auto"
xmlns:tools="http://schemas.android/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.dejunmonsdk.WebViewActivity">
<ProgressBar
android:id="@+id/commonsdk_webview_pb"
android:layout_width="match_parent"
android:layout_height="5dp"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"/>
<WebView
android:id="@+id/commonsdk_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#afe"></WebView>
</LinearLayout>
2.WebView的使用
public class WebViewActivity extends BaseActivity {
private WebView mCommonsdkWebview;
private ProgressBar mCommonsdkWebviewPb;
public static final String WEB_VIEW_URL="web_view_url";
private String url;
public static final int PB_MAX=100;//进度条的最大值
public static void startWenViewActivity(BaseActivity fromActivity,String url){
Bundle bundle=new Bundle();
bundle.putString(WEB_VIEW_URL,url);
fromActivity.openActivityWithIntent(WebViewActivity.class,bundle);
}
@Override
protected BasePresenter createPresenter() {
return null;
}
@Override
protected void initView() {
mCommonsdkWebview = (WebView) findViewById(R.idmonsdk_webview);
mCommonsdkWebviewPb = (ProgressBar) findViewById(R.idmonsdk_webview_pb);
mCommonsdkWebviewPb.setMax(PB_MAX);
}
@Override
protected void initData(Bundle savedInstanceState) {
url=getIntent().getStringExtra(WEB_VIEW_URL);
mCommonsdkWebview.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {//设置网页加载进度
super.onProgressChanged(view, newProgress);
if (newProgress>=100){
mCommonsdkWebviewPb.setVisibility(View.GONE);
}else{
mCommonsdkWebviewPb.setProgress(newProgress);
}
}
});
mCommonsdkWebview.setWebViewClient(new WebViewClient(){
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)//android5.0以上手机才有效果
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return super.shouldOverrideUrlLoading(view, request);
}
});
WebSettings settings = mCommonsdkWebview.getSettings();
settings.setDomStorageEnabled(true);//这设置存储 不设置显示不出来
settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);//没有网使用缓存
settings.setJavaScriptEnabled(true);//设置网页可操作
settings.setUseWideViewPort(true);//设置视图可缩放和下面一起设置 似乎没效果
settings.setLoadWithOverviewMode(true);
mCommonsdkWebview.loadUrl(url);
}
@Override
protected int getLayoutId() {
return R.layout.activity_web_view;
}
}本文标签: 第三方打开网页浏览器AndroidWebView
版权声明:本文标题:Android WebView的使用及调用第三方浏览器打开网页 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/jiaocheng/1728365066a852313.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论