如何处理 android studio 中的 webview 点击事件?
How to handle webview click event in android studio?
我在我的应用程序中使用 webview 来显示 canvas。在我的网络视图上有单击此处按钮。我想在网络视图上单击此 'click here' 按钮打开新的 activity。不知道怎么处理这个点击event.Need求助尽快
以下是我的 webview class 代码:
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);
}
下面是在网络视图中打开 url 后的样子:
将 JavaScript 代码绑定到 Android 代码
在开发专门为 Android 应用程序中的 WebView 设计的 Web 应用程序时,您可以在 JavaScript 代码和客户端 Android 代码之间创建接口。例如,您的 JavaScript 代码可以调用 Android 代码中的方法来启动 activity.
要在您的 JavaScript 和 Android 代码之间绑定一个新接口,请调用 addJavascriptInterface(),将一个 class 实例传递给它以绑定到您的 JavaScript 和一个您的 JavaScript 可以调用以访问 class.
的接口名称
注意:如果您将 targetSdkVersion 设置为 17 或更高版本,则必须将 @JavascriptInterface 注释添加到您希望 JavaScript 可用的任何方法(该方法也必须是 public).如果您不提供注释,则当 运行 在 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 中为 JavaScript 运行 创建一个名为 Android 的界面。此时,您的 Web 应用程序可以访问 WebAppInterface class。例如,这里有一些 HTML 和 JavaScript 在用户单击按钮时使用新界面调用 class 的 "startNewActivity" 方法:
<input type="button" value="Click Here" onClick="showActivity()" />
<script type="text/javascript">
function showActivity() {
Android.startNewActivity();
}
</script>
我在我的应用程序中使用 webview 来显示 canvas。在我的网络视图上有单击此处按钮。我想在网络视图上单击此 'click here' 按钮打开新的 activity。不知道怎么处理这个点击event.Need求助尽快
以下是我的 webview class 代码:
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);
}
下面是在网络视图中打开 url 后的样子:
将 JavaScript 代码绑定到 Android 代码
在开发专门为 Android 应用程序中的 WebView 设计的 Web 应用程序时,您可以在 JavaScript 代码和客户端 Android 代码之间创建接口。例如,您的 JavaScript 代码可以调用 Android 代码中的方法来启动 activity.
要在您的 JavaScript 和 Android 代码之间绑定一个新接口,请调用 addJavascriptInterface(),将一个 class 实例传递给它以绑定到您的 JavaScript 和一个您的 JavaScript 可以调用以访问 class.
的接口名称注意:如果您将 targetSdkVersion 设置为 17 或更高版本,则必须将 @JavascriptInterface 注释添加到您希望 JavaScript 可用的任何方法(该方法也必须是 public).如果您不提供注释,则当 运行 在 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 中为 JavaScript 运行 创建一个名为 Android 的界面。此时,您的 Web 应用程序可以访问 WebAppInterface class。例如,这里有一些 HTML 和 JavaScript 在用户单击按钮时使用新界面调用 class 的 "startNewActivity" 方法:
<input type="button" value="Click Here" onClick="showActivity()" />
<script type="text/javascript">
function showActivity() {
Android.startNewActivity();
}
</script>