您的位置:首页 > 移动开发 > Android开发

Android WebView JavaScript交互

2014-05-08 19:35 405 查看
介绍一下,Android中Webview与JavaScript的交互,首先是在布局文件里添加webview控件:

[html] view
plaincopy

<WebView  

        android:id="@+id/webview"  

        android:layout_width="fill_parent"  

        android:layout_height="fill_parent" />  

  然后是在manifest里添加权限:

[html] view
plaincopy

<uses-permission android:name="and  

  要是webview能够与JavaScript交互,首先需要webview要启用JavaScript:

[html] view
plaincopy

WebSettings webSettings = myWebView.getSettings();  

        webSettings.setJavaScriptEnabled(true);  

  然后创建JavaScript的接口:

[java] view
plaincopy

public class WebAppInterface {  

        Context mContext;  

  

        /** Instantiate the interface and set the context */  

        WebAppInterface(Context c) {  

            mContext = c;  

        }  

  

        /** Show a toast from the web page */  

        @JavascriptInterface  

        public void showToast(String toast) {  

            Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();  

        }  

    }  

  给webview添加JavaScript接口:

[html] view
plaincopy

myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");  

  本地JavaScript文件:

[javascript] view
plaincopy

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

  

<script type="text/javascript">  

    function showAndroidToast(toast) {  

        Android.showToast(toast);  

    }  

</script>  

  整个代码如下:

[java] view
plaincopy

public class MainActivity extends Activity {  

    private WebView myWebView;  

  

    @Override  

    protected void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        setContentView(R.layout.activity_main);  

        myWebView = (WebView) findViewById(R.id.webview);  

        WebSettings webSettings = myWebView.getSettings();  

        webSettings.setJavaScriptEnabled(true);  

        myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");  

        ProcessWebString();  

  

    }  

  

    public class WebAppInterface {  

        Context mContext;  

  

        /** Instantiate the interface and set the context */  

        WebAppInterface(Context c) {  

            mContext = c;  

        }  

  

        /** Show a toast from the web page */  

        @JavascriptInterface  

        public void showToast(String toast) {  

            Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();  

        }  

    }  

  

    private void ProcessWebString() {  

        // 加载 asset 文件  

        String tpl = getFromAssets("web_tpl.html");  

        myWebView.loadDataWithBaseURL(null, tpl, "text/html", "utf-8", null);  

    }  

  

    /* 

     * 获取html文件 

     */  

    public String getFromAssets(String fileName) {  

        try {  

            InputStreamReader inputReader = new InputStreamReader(  

                    getResources().getAssets().open(fileName));  

            BufferedReader bufReader = new BufferedReader(inputReader);  

            String line = "";  

            String Result = "";  

            while ((line = bufReader.readLine()) != null)  

                Result += line;  

            return Result;  

        } catch (Exception e) {  

            e.printStackTrace();  

        }  

        return "";  

    }  

  

}  

  运行效果:



转自:http://blog.csdn.net/phj_981805903/article/details/12573735
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: