0

我正在尝试触发WebChromeClient.onRequestFocus()回调。为了获取有关何时调用的信息,我深入研究了 android 项目测试。

我从 2009 年开始搜索WebChromeClient 的测试用例,并创建了以下代码来重现测试行为,这似乎不起作用。

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Activity activity = this;
    final WebView webView = (WebView)findViewById(R.id.webView2);

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView.getSettings().setSupportMultipleWindows(true);

    webView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onCloseWindow(WebView window) {
            super.onCloseWindow(window);
            Log.i("", "================ onCloseWindow()===============================================================");
        }

        @Override
        public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture,
                                      Message resultMsg) {
            WebView childView = new WebView(activity);
            childView.setWebChromeClient(this);
            WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
            transport.setWebView(childView);
            resultMsg.sendToTarget();
            Log.i("", "================ onCreateWindow()===============================================================");
            return true;
        }

        @Override
        public void onRequestFocus(WebView view) {
            Log.i("", "================ onRequestFocus()===============================================================");
        }
    });


    webView.loadData("<html><body onLoad='"
            + "childWindow = window.open();"
            + "childWindow.document.title = \"javascript child window\";"
            + "childWindow.document.write(\"javascript child window\");"
            + "childWindow.focus();"
            + "setTimeout(function(){childWindow.close();}, 10000);"
            + "'><h1>Hello World!</h1></body></html>", "text/html", "UTF-8");
}

调试会话的 Logcat 确认调用onCreateWindow()onCloseWindow()函数,但没有onRequestFocus()调用。

有任何想法吗?

我使用 API 级别 19 (Android 4.4)

4

1 回答 1

0

我找到了答案。用两个框架创建网页

<html>
<frameset rows=20%,80%>
    <frame src="frame1.html" name="myFrame1" />
    <frame src="frame2.html" name="myFrame2" />
</frameset>
</html>

并在 frame1.html 中放置一个链接,目标设置为 myFrame2

<a id="link" href="frame3.html" target="myFrame2">Click me!</a>
于 2014-05-22T14:26:18.797 回答