首页 > Web开发 > 详细

WebView

时间:2014-11-29 17:28:39      阅读:253      评论:0      收藏:0      [点我收藏+]

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");

    bubuko.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

      bubuko.com,布布扣

<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"); } }

    bubuko.com,布布扣  bubuko.com,布布扣

    One more thing to say

      bubuko.com,布布扣

      bubuko.com,布布扣

    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.

 

 

 

    

WebView

原文:http://www.cnblogs.com/iMirror/p/4131089.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!