如何处理在Android Studio中的WebView点击事件? [英] How to handle webview click event in android studio?

查看:740
本文介绍了如何处理在Android Studio中的WebView点击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的应用程序的WebView显示画布。在我的WebView有点击此处按钮。我想对这个网页视图点击这里按钮,点击打开新的活动。我不知道如何尽快处理这个点击event.Need帮助​​越好。

I am using webview in my application to show canvas. On my webview there is click here button. I want to open new activity on click of this 'click here' button on webview. I dont know how to handle this click event.Need help as soon as possible.

以下是我的WebView类code:

Following is my webview class code:

public class TableWebView extends Activity {

    private SharedPreferences sharedPreferences;
    private String customer_id,hotel_id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_table_web_view);
        WebView wvtable= (WebView)findViewById(R.id.webViewTable);
        wvtable.setWebViewClient(new MyViewClient());
        WebSettings webSettings = wvtable.getSettings();
        webSettings.setJavaScriptEnabled(true);

        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        customer_id = sharedPreferences.getString("cust_id", "0");//if no customerid found automatically 0 will be used
        hotel_id=sharedPreferences.getString("hotel_master_id","0");

        wvtable.loadUrl("http://192.168.100.169/zoilo_admin/public/index.php/show-hotel-view?hotel_id="+hotel_id+"&customer_id="+customer_id+"&date_time="+ Util.selected_date_time);



    }

在这里是如何看起来在WebView中打开该网址后:

And here is how it looks after opening that url in webview:

推荐答案

绑定的JavaScript code到Android code

Binding JavaScript code to Android code

在开发的为的WebView专门在你的Andr​​oid应用程序的Web应用程序,你可以创建你的JavaScript code和客户端的Andr​​oid code之间的接口。比如,你的JavaScript code可以调用的方法在你的Andr​​oid code启动活动。

When developing a web application that's designed specifically for the WebView in your Android application, you can create interfaces between your JavaScript code and client-side Android code. For example, your JavaScript code can call a method in your Android code to start activity.

要绑定你的JavaScript和Android code之间一个新的接口,调用addJavascriptInterface(),传递一个类的实例绑定到你的JavaScript和你的JavaScript可以调用访问类的接口名称。

To bind a new interface between your JavaScript and Android code, call addJavascriptInterface(), passing it a class instance to bind to your JavaScript and an interface name that your JavaScript can call to access the class.

注意:如果你已经设置你的targetSdkVersion到17或更高,则必须将@JavascriptInterface注释添加到您希望提供给您的JavaScript(该方法也必须是公共的)任何方法。如果你不提供批注,在Android 4.2或运行时,该方法不为你的网页中较高的。

Caution: If you've set your targetSdkVersion to 17 or higher, you must add the @JavascriptInterface annotation to any method that you want available to your JavaScript (the method must also be public). If you do not provide the annotation, the method is not accessible by your web page when running on Android 4.2 or higher.

public class TableWebView extends Activity {

    private SharedPreferences sharedPreferences;
    private String customer_id,hotel_id;
    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_table_web_view);

        WebView wvtable= (WebView)findViewById(R.id.webViewTable);

        wvtable.setWebViewClient(new MyViewClient());

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

        WebSettings webSettings = wvtable.getSettings();

        webSettings.setJavaScriptEnabled(true);

        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        customer_id = sharedPreferences.getString("cust_id", "0");//if no customerid found automatically 0 will be used
        hotel_id=sharedPreferences.getString("hotel_master_id","0");

        wvtable.loadUrl("http://192.168.100.169/zoilo_admin/public/index.php/show-hotel-view?hotel_id="+hotel_id+"&customer_id="+customer_id+"&date_time="+ Util.selected_date_time);
}



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 startNewActivity() {
        //startActivity(new Intent(this, youactivityname.class));
    } 
} 

这将创建一个名为Android的在WebView中运行的JavaScript的接口。在这一点上,Web应用程序可以访问WebAppInterface类。例如,这里的一些HTML和调用使用新接口的类的你startNewActivity方法的JavaScript,当用户点击一个按钮:

This creates an interface called Android for JavaScript running in the WebView. At this point, your web application has access to the WebAppInterface class. For example, here's some HTML and JavaScript that call your "startNewActivity" method of your class using the new interface when the user clicks a button:

<input type="button" value="Click Here" onClick="showActivity()" />

<script type="text/javascript">
    function showActivity() {
        Android.startNewActivity();
    }
</script>

这篇关于如何处理在Android Studio中的WebView点击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆