android WebView加载H5去地址栏的两种方式
2017-09-27 16:04
411 查看
android WebView加载H5去地址栏的两种方式,先上图
四个布局文件:
activity_main.xml
activity_second.xml
activity_no_title.xml
activity_no_title2.xml
四个java文件
MainActivity.class
SecondAcitivty.class
NoTitleActivity.class
NoTitleActivity2.class
源码下载:
http://download.csdn.net/download/qq_31939617/9997944
四个布局文件:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:padding="30dp"> <Button android:id="@+id/btn_normal" android:layout_width="match_parent" android:layout_height="wrap_content" 4000 android:text="正常加载未处理的WebView" /> <Button android:id="@+id/btn_noTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/btn_normal" android:layout_marginTop="10dp" android:text="去除标题的WebView" /> <Button android:id="@+id/btn_noTitle2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/btn_noTitle" android:layout_marginTop="10dp" android:text="去除标题的2" /> </RelativeLayout>
activity_second.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.dr.dr_webview.SecondActivity"> <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent"></WebView> </RelativeLayout>
activity_no_title.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.dr.dr_webview.SecondActivity"> <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent"></WebView> </RelativeLayout>
activity_no_title2.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.dr.dr_webview.SecondActivity"> <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent"></WebView> </RelativeLayout>
四个java文件
MainActivity.class
package com.dr.dr_webview; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn_normal = (Button) findViewById(R.id.btn_normal); Button btn_noTitle = (Button) findViewById(R.id.btn_noTitle); Button btn_noTitle2 = (Button) findViewById(R.id.btn_noTitle2); btn_normal.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,SecondActivity.class); startActivity(intent); } }); btn_noTitle.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,NoTitleActivity.class); startActivity(intent); } }); btn_noTitle2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,NoTitleActivity2.class); startActivity(intent); } }); } }
SecondAcitivty.class
package com.dr.dr_webview; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.WebSettings; import android.webkit.WebView; public class SecondActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); WebView webView = (WebView) findViewById(R.id.webview); WebSettings webSettings = webView.getSettings(); d4e4 webSettings.setJavaScriptEnabled(true); webSettings.setUseWideViewPort(true); webSettings.setLoadWithOverviewMode(true); webView.loadUrl("http://www.baidu.com"); } }
NoTitleActivity.class
package com.dr.dr_webview; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; public class NoTitleActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_no_title); WebView webView = (WebView) findViewById(R.id.webview); WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setUseWideViewPort(true); webSettings.setLoadWithOverviewMode(true); webView.loadUrl("http://www.baidu.com"); webView.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); //编写 javaScript方法 /* String javascript = "javascript:function hideOther() {" + "document.getElementsByTagName('body')[0].innerHTML;" + "document.getElementsByTagName('div')[0].style.display='none';" + "document.getElementsByTagName('div')[3].style.display='none';" + "document.getElementsByClassName('dropdown')[0].style.display='none';" + "document.getElementsByClassName('min')[0].remove();" + "var divs = document.getElementsByTagName('div');" + "var lastDiv = divs[divs.length-1];" + "lastDiv.remove();" + "document.getElementsByClassName('showme')[0].remove();" + "document.getElementsByClassName('nei-t3')[1].remove();}";*/ String javascript = "javascript:function hideOther() {" + "document.getElementsByTagName('body')[0].innerHTML;" + "document.getElementsByTagName('div')[0].style.display='none';" + "document.getElementsByTagName('div')[3].style.display='none';" + "document.getElementsByClassName('dropdown')[0].style.display='none';" + "document.getElementsByClassName('min')[0].remove();" + "var divs = document.getElementsByTagName('div');" + "var lastDiv = divs[divs.length-1];" + "lastDiv.remove();" + "document.getElementsByClassName('showme')[0].remove();" //+ // "document.getElementsByClassName('nei-t3')[1].remove();}" ; //创建方法 view.loadUrl(javascript); //加载方法 view.loadUrl("javascript:hideOther();"); } }); } }
NoTitleActivity2.class
package com.dr.dr_webview; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; public class NoTitleActivity2 extends AppCompatActivity { String url; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_no_title2); WebView mWebView = (WebView) findViewById(R.id.webview); url = "http://www.baidu.com"; WebSettings setting = mWebView.getSettings(); setting.setJavaScriptEnabled(true);//让webview支持javascript setting.setDefaultTextEncodingName("utf-8");//设置字符集 //setting.setBlockNetworkImage(true);//设置不能访问网络图片 // setting.setSupportZoom(true);//开启网页的缩放 // setting.setBuiltInZoomControls(true); setting.setLoadWithOverviewMode(true);//设置网页缩放至手机大小 setting.setUseWideViewPort(true); mWebView.getSettings().setJavaScriptEnabled(true);//让webview支持javascript mWebView.setWebChromeClient(new WebChromeClient());//支持特殊javascript mWebView.loadUrl(url); mWebView.setWebViewClient(new MyWebViewClient()); } private class MyWebViewClient extends WebViewClient { @Override // 在WebView中而不在默认浏览器中显示页面 public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } } }
源码下载:
http://download.csdn.net/download/qq_31939617/9997944
相关文章推荐
- Android的WebView加载网页的几种方式及网络异常处理
- Android原生webView加载h5页面出现加载错乱不完全问题
- webView--Android中webView加载H5时,H5页面在不同的手机可能出现文字或者图标类似换行的现象
- Android中webView加载H5绑定cookie
- android用webview加载H5页面出现点击事件失效的问题解决
- 安卓Android WebView 加载H5的解决问题
- android webview 加载H5页面,设置字体不随系统设置变化
- android webview打开pdf 的两种方式
- Android_webview加载资源方式
- Android webview加载H5空白页问题
- Android中WebView与JavaScript交互的两种方式
- android------引导页两种实现方式(原生和WebView网页实现)
- android中webView加载H5,JS不能调用问题的解决
- Android中加载WebView的H5全屏视频播放
- android webview 加载h5 相关问题
- webView 加载页面的两种请求方式: get 和 post
- Android在webview下加载本地html的方式
- Android用webView加载h5的页面,h5获取本地的图库中的图片
- Android平台下 Webview加载h5页面,交互,及本地化缓存
- android加载bingmaps地图-------webview方式