在我的Android应用中,我正在显示一个网页。有时网页会打开一个模态弹出/对话框,但滚动焦点仍然保留在网页中而不是弹出窗口中。我如何控制焦点?
答案 0 :(得分:1)
您可以创建一个WebAppInterface来显示对话框。
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 showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}}
并在WebView上设置此界面
WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
这将在WebView中创建一个名为Android for JavaScript的界面。此时,您的Web应用程序可以访问WebAppInterface类。例如,这里有一些HTML和JavaScript,当用户点击按钮时,使用新界面创建一个toast消息:
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
<script type="text/javascript">
function showAndroidToast(toast) {
Android.showToast(toast);
}
无需从JavaScript初始化Android界面。 WebView会自动将其提供给您的网页。因此,只需单击按钮,showAndroidToast()函数就会使用Android界面调用WebAppInterface.showToast()方法。
Fount:http://developer.android.com/intl/es/guide/webapps/webview.html