2015-04-03 86 views
0

我在我的應用程序中使用webview來顯示畫布。在我的webview上點擊這裏按鈕。我想在webview上點擊這個'點擊這裏'按鈕來打開新的活動。我不知道如何處理這個點擊事件,需要儘快幫助。如何在android studio中處理webview點擊事件?

以下是我的WebView類代碼:

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中打開該網址後:

table view

+0

嘗試[這](http://stackoverflow.com/questions/ 20917235/webviews-html-button-click-detection-in-activityjava-code) – Kunu 2015-04-03 09:41:31

回答

3

結合JavaScript代碼的Android代碼

在開發專門爲您的Android應用程序中的WebView設計的Web應用程序時,您可以創建界面您的JavaScript代碼和客戶端Android代碼。例如,您的JavaScript代碼可以在您的Android代碼中調用一個方法來啓動活動。

要綁定JavaScript和Android代碼之間的新接口,請調用addJavascriptInterface(),將其傳遞給一個類實例以綁定到您的JavaScript以及您的JavaScript可以調用的接口名稱以訪問該類。

注意:如果您已將targetSdkVersion設置爲17或更高,則必須將@JavascriptInterface註釋添加到您希望可用於JavaScript的方法(該方法也必須是公共的)。如果您不提供註釋,則在運行Android 4.2或更高版本時,您的網頁無法訪問該方法。

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

這將在WebView中創建一個名爲Android的JavaScript for JavaScript接口。此時,您的Web應用程序可以訪問WebAppInterface類。例如,這裏的一些HTML和JavaScript調用使用新接口的類的你「startNewActivity」的方法,當用戶點擊一個按鈕:

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

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

我應該在哪裏寫 2015-04-03 10:42:00

+0

在你的html或php頁面。您嘗試在Web視圖中加載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); – 2015-04-03 10:59:43

+0

該按鈕是你的html頁面的一部分正確! – 2015-04-03 11:00:27