我正试图触发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 (Android4.4)
发布于 2014-05-22 14:26:18
我找到了答案。创建两个框架的网页
<html>
<frameset rows=20%,80%>
<frame src="frame1.html" name="myFrame1" />
<frame src="frame2.html" name="myFrame2" />
</frameset>
</html>并将目标设置为myFrame2的一个链接放在frame1.html 1.html中。
<a id="link" href="frame3.html" target="myFrame2">Click me!</a>https://stackoverflow.com/questions/23567735
复制相似问题