请问如何用webview实现想自带浏览器的弹出窗口功能。

解决方案 »

  1.   

    按照你说的意思是想要在弹出窗口里面嵌入一个webView?
    先自定义一个dialog,然后布局文件里面嵌入webView,
    最后实例化这个dialog,就可以啦..
      

  2.   

    我也觉得可能是这个思路,用一个popupwindow来弹出,但是不知道如何来写
      

  3.   

    那直接把WebView 添加的popWindow的父容器里面。试试。
      

  4.   

    为什么不能实现,用dialog,popWindow都可以实现,dialog都可以来弹窗播放视频,popWindow播放视频有缺陷,顺手给个以前的小例子package cn.com.adchina;import android.content.Context;
    import android.graphics.Color;
    import android.view.Gravity;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.PopupWindow;public class DirectorPopupWindow {
    private Context context;
    private ImageView image;
    private Button closeButton;
    private PopupWindow popupWindow;
    public DirectorPopupWindow() {
    super();
    } public DirectorPopupWindow(Context context) {
    this.context = context;
    init();
    }

    private void init()
    {
    LinearLayout linearLayout = new LinearLayout(context);
    linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
    linearLayout.setOrientation(LinearLayout.VERTICAL);
    linearLayout.setGravity(Gravity.RIGHT);
    closeButton = new Button(context);
    closeButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT) );
    closeButton.setText("关闭");
    closeButton.setTextSize(10);
    closeButton.setTextColor(Color.BLACK);
    closeButton.setBackgroundColor(Color.WHITE);
    image = new ImageView(context);
    image.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT) );
    linearLayout.addView(closeButton);
    linearLayout.addView(image);
            popupWindow = new PopupWindow(linearLayout,LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            popupWindow.setAnimationStyle(R.style.MyAnimationName);
            closeButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    onDimiss();
    }
    });
    }

    private void setContentImage()
    {
    image.setImageResource(R.drawable.sina);
    }

    public void startReceive()
    {
    setContentImage();
    }

    public void onShow(View parent)
    {
    if(parent!=null && !popupWindow.isShowing())
    {
    popupWindow.showAtLocation(parent, Gravity.BOTTOM|Gravity.RIGHT, 0, 0);
    }
    }

    public void onDimiss()
    {
    if(popupWindow.isShowing())
    {
    popupWindow.dismiss();
    }
    }
    }
    在Activity中这样调用
     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            initView();
            
            popupWindow = new DirectorPopupWindow(this);
            popupWindow.startReceive();
        }
      

  5.   

    少了一段activity中显示的代码popupWindow.onShow(findViewById(R.id.linear));// 此处传的参数是指你想把PopupWindow显示在哪个控件上
      

  6.   

    可能我没有说清楚,这个才是问题的核心:在webview里点击链接(链接是用JS window.open打开的),弹出如上的窗口,并返回赋值(赋值也是js的 window.opener.document.getElementById("txtCH").value = document.getElementById("txt1").value;)。并且我试了安卓上的其他浏览器基本都不能满足要求,只有系统自带的这个可以。所谓“难着不会,会着不难”,所以还希望知道的和我联系。
      

  7.   

    不是很明白为什么一定要这样做。
    webview控件通过js是可以很好的跟你的java代码交互的,可以双向控制。
    对于带弹出的,需要这行代码:webViewCtl.setWebChromeClient(new WebChromeClient());通常,弹出的url会是about:blank,所以需要在shouldOverrideUrlLoading中进行判断
    webViewCtl.setWebViewClient(new WebViewClient() {
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url.equals("about:blank")) {
    } else {
    view.loadUrl(url);
    }
    return true;
    }
    });