关于用webview开发浏览器 请问如何用webview实现想自带浏览器的弹出窗口功能。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 按照你说的意思是想要在弹出窗口里面嵌入一个webView?先自定义一个dialog,然后布局文件里面嵌入webView,最后实例化这个dialog,就可以啦.. 我也觉得可能是这个思路,用一个popupwindow来弹出,但是不知道如何来写 那直接把WebView 添加的popWindow的父容器里面。试试。 为什么不能实现,用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(); } 少了一段activity中显示的代码popupWindow.onShow(findViewById(R.id.linear));// 此处传的参数是指你想把PopupWindow显示在哪个控件上 可能我没有说清楚,这个才是问题的核心:在webview里点击链接(链接是用JS window.open打开的),弹出如上的窗口,并返回赋值(赋值也是js的 window.opener.document.getElementById("txtCH").value = document.getElementById("txt1").value;)。并且我试了安卓上的其他浏览器基本都不能满足要求,只有系统自带的这个可以。所谓“难着不会,会着不难”,所以还希望知道的和我联系。 不是很明白为什么一定要这样做。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; } }); 蓝牙连接问题 求指教 Listview设置OnItemClickListener事件点击没有响应 下载源码导入后AndroidManifest.xml小红叉 急,怎么定制AlertDialog Android学习笔记(1)-----Android项目目录结构 请问关于获取字符串 textView 设置颜色的问题 求助:关于Android market上传app的小问题 请教关于Android中setMargins不可用的问题。 关于不同版本的socket通信问题~ 如何在图片的某个特定区域画上字符串 求助,ListView显示条目颜色动态设置问题。
先自定义一个dialog,然后布局文件里面嵌入webView,
最后实例化这个dialog,就可以啦..
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();
}
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;
}
});