首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >触发WebChromeClient onRequestFocus

触发WebChromeClient onRequestFocus
EN

Stack Overflow用户
提问于 2014-05-09 14:53:33
回答 1查看 636关注 0票数 0

我正试图触发WebChromeClient.onRequestFocus()回调。为了获取何时被调用的信息,我深入到了android项目测试中。

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

代码语言:javascript
复制
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 (Android4.4)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-22 14:26:18

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

代码语言:javascript
复制
<html>
<frameset rows=20%,80%>
    <frame src="frame1.html" name="myFrame1" />
    <frame src="frame2.html" name="myFrame2" />
</frameset>
</html>

并将目标设置为myFrame2的一个链接放在frame1.html 1.html中。

代码语言:javascript
复制
<a id="link" href="frame3.html" target="myFrame2">Click me!</a>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23567735

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档