2013-05-11 61 views
5

我正在用webview開發一個應用程序,但我不知道如何啓用JavaScript來在webview中顯示警告對話框。如何在Webview中顯示android alert對話框?

我曾試過這一個,但這對我來說不起作用。 首先,我做了webview和websettings對象 ,然後設置下列參數。

webSettings.setJavaScriptEnabled(true); 

    webSettings.setBuiltInZoomControls(true); 

    mWebView.requestFocusFromTouch(); 
+0

http://stackoverflow.com/questions/8634319/how-to-set-different -title-for-alert-dialog-when-webview-page-is-loaded – Raghunandan 2013-05-11 05:39:51

+0

@Ganesh:你想顯示android原生AlertDialog(http://developer.android.com/reference/android/app/AlertDialog.html) ? – 2013-05-11 05:41:37

回答

16

根據您的代碼,我認爲您不要設置以下設置Webviewclient和Webcromeclient以在Webview中啓用Javascript。

mWebView.setWebViewClient(new WebViewClient()); 
mWebView.setWebChromeClient(new WebChromeClient()); 

然後你用下面的代碼加載你的Html頁面。

mWebView.loadUrl("file:///android_asset/"+Your_Html_Page); 

這是您的Web視圖中的Html部分。

<!DOCTYPE html> 
<html> 
<head> 
    <script> 
    function myFunction() 
    { 
     alert("Hello! I am an alert box!"); 
    } 
    </script> 
</head> 
<body> 

<input type="button" onclick="myFunction()" value="Show alert box" /> 
</body> 
</html> 

試試這可能會有幫助。

+0

謝謝....它適用於我! – Vasu 2013-05-11 05:50:32

+0

完美。這解決了我得到一個JS警報顯示的問題,非常感謝。 – Railto 2015-01-24 10:00:13

+0

當我添加上面兩行時,它正在工作。但是,我怎樣才能刪除警報消息的URL。 – gnganpath 2015-02-24 06:32:23

0

試試這個代碼:

public OnClickListener imageButtonViewOnClickListener = new OnClickListener() { 
public void onClick(View v) { 

LayoutInflater inflater = LayoutInflater.from(MyActivity.this); 

// error here 
View alertDialogView = inflater.inflate(R.layout.alert_dialog_layout, null); 

WebView myWebView = (WebView) findViewById(R.id.DialogWebView); 
myWebView.loadData(webContent, "text/html", "utf-8"); 
AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this); 
builder.setView(alertDialogView); 

builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int which) { 
     dialog.cancel(); 
    } 
}).show(); 
} 
}; 

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" android:layout_height="wrap_content" 
android:orientation="vertical"> 

<WebView android:id="@+id/DialogWebView" android:layout_height="wrap_content" 
android:layout_width="fill_parent" android:layout_marginLeft="20dip" 
android:layout_marginRight="20dip" 
android:textAppearance="?android:attr/textAppearanceMedium" /> 
2

如果要顯示JavaScript alert函數的消息框,則不強制使用WebChromeClient。您可以在您的JavaScript代碼和客戶端Android代碼之間創建接口。

在下面的例子中,JavaScript代碼可以調用在Android代碼的方法來顯示Dialog,代替使用JavaScript的alert()功能。我發現這是顯示警報的最方便和廣泛支持的方式。

包括在你的Android應用程序的類:

文件:WebAppInterface.java

import android.content.Context; 
import android.webkit.JavascriptInterface; 

public class WebAppInterface { 
    Context mContext; 

    /** Instantiate the interface and set the context */ 
    WebAppInterface(Context c) { 
     mContext = c; 
    } 

    /** Show a Message box from the web page */ 
    @JavascriptInterface 
    public void androidAlert(String message) { 
     DialogBox dbx = new DialogBox(); 
     dbx.dialogBox(message, "I get it", "",mContext); 
    } 
} 

文件:DialogBox.java

import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.Context; 
import android.content.DialogInterface; 

public class DialogBox {  
    public boolean dialogBox(String msg, String okButton, String cancelButton, final Context activity) { 
     Dialog v = null; 
     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity); 
     alertDialogBuilder.setMessage(msg); 
     if (okButton != "") { 
      alertDialogBuilder.setPositiveButton(okButton, 
        new DialogInterface.OnClickListener() {  
         @Override 
         public void onClick(DialogInterface arg0, int arg1) { /**/ } 
        }); 
     }  
     if (cancelButton != "") { 
      alertDialogBuilder.setNegativeButton(cancelButton, 
        new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface arg0, int arg1) { /**/ } 
        }); 
     } 

     AlertDialog alertDialog = alertDialogBuilder.create(); 
     alertDialog.show(); 
     return true; 
    }  
} 

接下來,綁定WebAppInterface級的JavaScript,在你的WebViewaddJavascriptInterface()運行,並命名接口Android

WebView mWebView = (WebView) findViewById(R.id.webview); 
mWebView.addJavascriptInterface(new WebAppInterface(this), "Android"); 

這將創建一個名爲Android對JavaScript的運行WebView的接口。此時,您的Web應用程序可以訪問WebAppInterface類。下面是一些HTML和JavaScript,當用戶點擊一個按鈕,創建使用新界面,一個消息框:

<input type="button" value="Alert!" onClick="javascript:Android.androidAlert('It works!');" /> 

在點擊按鈕時,Android接口用於調用WebAppInterface.androidAlert()方法。

有點警告:使用addJavascriptInterface()允許JavaScript來控制你的Android應用程序。這可能是非常有用的功能危險安全問題。因此,除非您編寫出現在WebView中的所有HTML和JavaScript,否則不應使用addJavascriptInterface()

1

爲了使您的WebView的Java腳本的對話可以用這個試試...在波紋管的網址... URL