Android WebView showToast On按钮单击
我正在开发一个使用WebView显示网页的应用程序。 该网页包含注册表和“下一步”按钮。我想要 了解如果用户 按“下一步”,但未能填充输入区域或数据未填充 不符合要求的标准。我已经编写了验证逻辑, 但是我不太清楚祝酒词,有人能给我指出正确的方向吗Android WebView showToast On按钮单击,android,android-toast,Android,Android Toast,我正在开发一个使用WebView显示网页的应用程序。 该网页包含注册表和“下一步”按钮。我想要 了解如果用户 按“下一步”,但未能填充输入区域或数据未填充 不符合要求的标准。我已经编写了验证逻辑, 但是我不太清楚祝酒词,有人能给我指出正确的方向吗 方向?您可以通过实现一个简单的JavaScript接口来解决问题。下面是一个简单的示例 assets/button_demo.xml <html> <head></head> <body style="marg
方向?您可以通过实现一个简单的JavaScript接口来解决问题。下面是一个简单的示例 assets/button_demo.xml
<html>
<head></head>
<body style="margin:0; padding:0;">
<style type="text/css">
#demo {
height: 100%;
width: 100%;
margin-left: auto;
margin-right: auto;
overflow: auto;
}
#demo a {
font-family: sans-serif;
font-size: 8pt;
width: 95%;
height: 35px;
display: block;
margin-left: auto;
margin-right: auto;
text-align: center;
padding: 5px;
margin: 1px;
border: 1px solid blue;
text-decoration: none;
color: blue;
word-wrap: break-word;
}
</style>
<script type="text/javascript">
function showToast(str){
Android.showToast(str);
}
</script>
<div id="demo">
<a href="javascript: showToast('test')">button</a>
</div>
</body>
</html>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/webView"
android:layout_centerVertical="true"/>
</RelativeLayout>
#演示{
身高:100%;
宽度:100%;
左边距:自动;
右边距:自动;
溢出:自动;
}
#演示a{
字体系列:无衬线;
字号:8pt;
宽度:95%;
高度:35px;
显示:块;
左边距:自动;
右边距:自动;
文本对齐:居中;
填充物:5px;
保证金:1px;
边框:1px纯蓝色;
文字装饰:无;
颜色:蓝色;
单词包装:打断单词;
}
函数showtoos(str){
Android.showtoos(str);
}
活动\u main.xml
<html>
<head></head>
<body style="margin:0; padding:0;">
<style type="text/css">
#demo {
height: 100%;
width: 100%;
margin-left: auto;
margin-right: auto;
overflow: auto;
}
#demo a {
font-family: sans-serif;
font-size: 8pt;
width: 95%;
height: 35px;
display: block;
margin-left: auto;
margin-right: auto;
text-align: center;
padding: 5px;
margin: 1px;
border: 1px solid blue;
text-decoration: none;
color: blue;
word-wrap: break-word;
}
</style>
<script type="text/javascript">
function showToast(str){
Android.showToast(str);
}
</script>
<div id="demo">
<a href="javascript: showToast('test')">button</a>
</div>
</body>
</html>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/webView"
android:layout_centerVertical="true"/>
</RelativeLayout>
MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import java.lang.ref.WeakReference;
public class MainActivity extends ActionBarActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new MyWebClient());
webView.addJavascriptInterface(new WebInterface(MainActivity.this), "Android");
webView.loadUrl("file:///android_asset/button_demo.html");
}
private class MyWebClient extends WebViewClient {
/**
* It is a good idea to let proper web browsers handle URLs you don't know otherwise
* you could be putting your users to danger by injecting JavaScript interface
*/
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//return super.shouldOverrideUrlLoading(view, url);
Uri uri;
try {
uri = Uri.parse(url);
} catch (NullPointerException e) {
// let Android deal with this
return true;
}
String host = uri.getHost(); //Host is null when user clicked on email, phone number, ...
if (host != null && host.equals("stackoverflow.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
else {
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs or anything else (email, phone number, ...)
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
@Override
public void onPageFinished(WebView view, String url) {
Log.d(TAG, "Page finished loading");
super.onPageFinished(view, url);
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
// No connection or HTTP error e.g. 404
Log.d(TAG, "Received webview error: " + errorCode + " - " + description + " ; " + failingUrl);
}
}
private final class WebInterface {
/**
* 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.
*/
private WeakReference<Activity> mContextRef;
private Toast toast;
public WebInterface(Activity context) {
this.mContextRef = new WeakReference<Activity>(context);
}
@JavascriptInterface
public void showToast(final String toastMsg) {
if (TextUtils.isEmpty(toastMsg) || mContextRef.get() == null) {
return;
}
// JavaScript doesn't run on the UI thread, make sure you do anything UI related like this
// You don't need this for the Toast, but otherwise it's a good idea
mContextRef.get().runOnUiThread(new Runnable() {
@Override
public void run() {
if (toast != null) {
toast.cancel();
}
toast = Toast.makeText(mContextRef.get(), toastMsg, Toast.LENGTH_SHORT);
toast.show();
}
});
}
}
}
导入android.app.Activity;
导入android.content.Intent;
导入android.net.Uri;
导入android.os.Bundle;
导入android.support.v7.app.ActionBarActivity;
导入android.text.TextUtils;
导入android.util.Log;
导入android.webkit.JavascriptInterface;
导入android.webkit.WebSettings;
导入android.webkit.WebView;
导入android.webkit.WebViewClient;
导入android.widget.Toast;
导入java.lang.ref.WeakReference;
公共类MainActivity扩展了ActionBarActivity{
私有静态最终字符串标记=MainActivity.class.getSimpleName();
私有网络视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView=(webView)findviewbyd(R.id.webView);
WebSettings WebSettings=webView.getSettings();
setJavaScriptEnabled(true);
setWebViewClient(新的MyWebClient());
addJavascriptInterface(新的WebInterface(MainActivity.this),“Android”);
webView.loadUrl(“file:///android_asset/button_demo.html");
}
私有类MyWebClient扩展了WebViewClient{
/**
*让适当的web浏览器处理您不知道的URL是一个好主意
*通过注入JavaScript接口,您可能会将用户置于危险之中
*/
@凌驾
公共布尔值shouldOverrideUrlLoading(WebView视图,字符串url){
//返回super.shouldOverrideUrlLoading(视图、url);
Uri;
试一试{
uri=uri.parse(url);
}捕获(NullPointerException e){
//让Android来处理这个问题
返回true;
}
字符串host=uri.getHost();//当用户单击电子邮件、电话号码等时,主机为空。。。
if(host!=null&&host.equals(“stackoverflow.com”)){
//这是我的网站,所以不要覆盖;让我的WebView加载页面
返回false;
}
否则{
//否则,该链接不适用于我网站上的页面,因此启动另一个处理URL或其他任何内容(电子邮件、电话号码等)的活动
Intent Intent=newintent(Intent.ACTION_视图,Uri.parse(url));
星触觉(意向);
返回true;
}
}
@凌驾
公共void onPageFinished(WebView视图,字符串url){
Log.d(标签“页面已完成加载”);
super.onPageFinished(视图、url);
}
@凌驾
public void onReceivedError(WebView视图、int错误代码、字符串描述、字符串失败URL){
//无连接或HTTP错误,例如404
Log.d(标记“接收到的网络视图错误:“+errorCode+”-“+description+”;“+failingUrl”);
}
}
私有最终类WebInterface{
/**
*警告:如果您已将targetSdkVersion设置为17或更高,则必须
*将@JavascriptInterface注释添加到所需的任何方法中
*可用于JavaScript(该方法也必须是公共的)
*如果不提供注释,则您的网页无法访问该方法
*在Android 4.2或更高版本上运行时。
*/
私有WeakReference mContextRef;
私人吐司;
公共Web界面(活动上下文){
this.mContextRef=新的WeakReference(上下文);
}
@JavascriptInterface
公共无效演示Toast(最终字符串toastMsg){
if(TextUtils.isEmpty(toastMsg)| | mContextRef.get()==null){
返回;
}
//JavaScript不会在UI线程上运行,请确保执行与UI相关的任何操作
//你不需要这个来祝酒,但除此之外这是个好主意
mContextRef.get(){
@凌驾
公开募捐{
if(toast!=null){
toast.cancel();
}
toast=toast.makeText(mContextRef.get(),toastMsg,toast.LENGTH\u SHORT);
toast.show();
}
});
}
}
}
Android.showtoos(str)中的代码>未定义Android,因为结果将是一个Javascript错误,如“未捕获引用错误:未定义Android”。那么,如何摆脱这种局面呢?