我正在尝试使用android webview实现类似Facebook的功能。没有“确认”对话框,它工作正常。但是当它需要确认时它不起作用。这是我正在使用的代码片段。
private void setUpWebView() {
likeWebView.setVerticalScrollBarEnabled(false);
likeWebView.setHorizontalScrollBarEnabled(false);
likeWebView.setWebViewClient(new FacebookWebViewClient());
likeWebView.setWebChromeClient(new MyChromeClient());
likeWebView.getSettings().setJavaScriptEnabled(true);
String url = getFacebookLikeUrl();
likeWebView.loadUrl(url);
}
我也在设置ViewClient和WebChromeClient。
private String getFacebookLikeUrl() {
return "http://www.facebook.com/plugins/like.php?" + "href="
+ URLEncoder.encode(mUrl) + "&access_token="
+ URLEncoder.encode(facebook.getAccessToken());
}
请帮我解决这个问题。提前谢谢。
答案 0 :(得分:13)
Facebook就像确认在新窗口中打开confirm_widget。因此,您的webview应该支持多窗口打开。对于这个setJavaScriptCanOpenWindowsAutomatically(true)和setSupportMultipleWindows(true)为您的webview-
private void setUpWebView() {
likeWebView = new WebView(getContext());
likeWebView.setWebViewClient(new FacebookWebViewClient());
likeWebView.setWebChromeClient(new MyChromeClient());
final WebSettings webSettings = likeWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setSupportMultipleWindows(true);
String url = getFacebookLikeUrl();
likeWebView.loadUrl(url);
likeWebView.setLayoutParams(FILL);
mContent.addView(likeWebView);
}
Facebook就像确认调用onCreateWindow()方法一样。 SO覆盖WebChromeClient中的onCreateWindow方法 -
final class MyChromeClient extends WebChromeClient {
// Add new webview in same window
@Override
public boolean onCreateWindow(WebView view, boolean dialog,
boolean userGesture, Message resultMsg) {
WebView childView = new WebView(getContext());
childView.getSettings().setJavaScriptEnabled(true);
childView.setWebChromeClient(this);
childView.setWebViewClient(new FacebookWebViewClient());
childView.setLayoutParams(FILL);
mContent.addView(childView);
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(childView);
resultMsg.sendToTarget();
return true;
}
// remove new added webview whenever onCloseWindow gets called for new webview.
@Override
public void onCloseWindow(WebView window) {
mContent.removeViewAt(mContent.getChildCount() - 1);
}
}
confirm_widget用于在用户单击“赞”或“取消”时调用onCloseWindow。在此方法中删除上次添加的webview。
答案 1 :(得分:5)
我将此用于Stripe Checkout,它会在移动设备中打开一个用于付款的新窗口。
基于@ Shweta的回复:
在您的活动中:
package myapp.app;
/*** imports ***/
public class LoggedActivity extends FragmentActivity
{
public WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.logged);
// retrieve the main container
LinearLayout container = (LinearLayout) findViewById(R.id.logged_webviews_container);
// layout params applied to the webviews in order to fit 100% the parent container
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
myWebView = new WebView(this);
myWebView.setLayoutParams(layoutParams);
myWebView.setWebViewClient(new BetterWebViewClient(this));
WebSettings settings = myWebView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setSupportMultipleWindows(true);
// on this instruction, we set our extended class below as the Parent Webview webchromeclient
myWebView.setWebChromeClient(new PopupWebView(this, myWebView, container, layoutParams));
// load URL
myWebView.loadUrl('http://www.mywebsite.com');
container.addView(myWebView);
}
}
添加此类扩展WebChromeClient
package myapp.app;
/*** imports ***/
public class PopupWebChromeClient extends WebChromeClient {
protected Activity activity;
protected WebView parentWebView;
protected RelativeLayout container;
protected WebView popupView;
PopupWebChromeClient(
Activity activity,
WebView parentWebView,
RelativeLayout container
)
{
super();
this.activity = activity;
this.parentWebView = parentWebView;
this.container = container;
}
@Override
public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, Message resultMsg) {
this.parentWebView.setVisibility(WebView.GONE);
this.popupView = new WebView(this.activity);
// setup popuview and add
this.popupView.getSettings().setJavaScriptEnabled(true);
this.popupView.setWebChromeClient(this);
this.popupView.setWebViewClient(new ApkfWebViewClient(this.activity, true));
this.popupView.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT
));
this.container.addView(this.popupView);
// send popup window infos back to main (cross-document messaging)
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(popupView);
resultMsg.sendToTarget();
return true;
}
// remove new added webview on close
@Override
public void onCloseWindow(WebView window) {
this.popupView.setVisibility(WebView.GONE);
this.parentWebView.setVisibility(WebView.VISIBLE);
}
}
在你的布局xml中,不要设置webview,因为我们会动态创建它们。