WebView

1. WebView

  The WebView class is an extension of Android's View class that allows you to display web pages as a part of your activity layout.

  It does not include any features of a fully developed web browser, such as navigation controls or an address bar. All that WebView does,

    by default, is show a web page.

 

2. Adding a WebView to Your Application

 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="WebView" />
    
 <WebView 
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.example.com");

    

3. Using JavaScript in WebView

  Once JavaScript is enabled, you can also create interfaces between your application code and your JavaScript code.

  For example, JavaScript code can call a method in your Android code to display a Dialog , instead of using JavaScript's alert() function.

  3.1 Enabling JavaScript

    retrieve WebSettings with getSettings(), then enable JavaScript with setJavaScriptEnabled().

WebView myWebView = (WebView)findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

  3.2 Binding JavaScript code to Android code

    <1>Create a html in the tomcat. only a button in this HTML

      

<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />

<script type="text/javascript">
    function showAndroidToast(toast) {
        Mirror.showToast(toast);
    }
</script>

    <2>create a class in the project

//WebAppInterface
package mirror.android.minibrowser; import android.content.Context; import android.webkit.JavascriptInterface; import android.widget.Toast;
public class WebAppInterface { Context mContext; public WebAppInterface(Context c) { mContext = c; } @JavascriptInterface // this annotation is necessary!!!! public void showToast(String tosat){ Toast.makeText(mContext, "JavaScriptInterface", Toast.LENGTH_LONG).show(); } }

    <3> use addJavascriptInterface to create an interface called Mirror for JavaScript running in the WebView

public class MiniBrowser extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mini_browser);
        
        WebView myWebView = (WebView)findViewById(R.id.webView);
        
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        
     //(object, interfacename that JavaScript can call to access the Android class) myWebView.addJavascriptInterface(
new WebAppInterface(this), "Mirror"); myWebView.loadUrl("http://192.168.1.111:8080/MiniWeb/minibrowser.html"); } }

      

    One more thing to say

      

      

    The object that is bound to your JavaScript runs in another thread and not in the thread in which it was constructed.

 

 

4. Handling Page Navigation

  When the user clicks a link from a web page in your WebView , the default behavior is for Android to launch an application that handles URLs

    However, you can override this behavior for your WebView, so links open within your WebView

myWebView.setWebViewClient(new MyWebViewClient());
 private class MyWebViewClient extends WebViewClient{

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if(Uri.parse(url).getHost().equals("www.example.com")){return false;
            }
            // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }
    }

  Navigating web page history

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
        myWebView.goBack();
        return true;
    }
    // If it wasn't the Back key or there's no web page history, bubble up to the default
    // system behavior (probably exit the activity)
    return super.onKeyDown(keyCode, event);
}

 

5.

 

 

 

    

转载于:https://www.cnblogs.com/iMirror/p/4131089.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值