我在WebView中有超链接的问题。我已经解决了解决类似问题的所有链接,但没有找到任何解决问题的问题。
我有一个自定义的Web视图,该视图在某些手势上实现了一些动作。我猜这是为超链接的触摸。
在这是我拥有的自定义网络视图:
public class newBTWebView extends WebView implements QuickAction.OnDismissListener, PopoverView.PopoverViewDelegate, GestureDetector.OnGestureListener {
public newBTWebView(Context context) {
super(context);
this.ctx = context;
setup(context);
init(context);
}
public newBTWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.ctx = context;
setup(context);
init(context);
}
public void init(Context context) {
this.context = context;
setInitialScale(100);
this.getSettings().setJavaScriptEnabled(true);
Configuration conf = getResources().getConfiguration();
int screenLayout = 1;
try
{
Field field = conf.getClass().getDeclaredField("screenLayout");
screenLayout = field.getInt(conf);
}
catch (Exception e) {
}
loadingNewChap = false;
screenType = screenLayout & 15;
gestureScanner = new GestureDetector(this);
this.setVerticalScrollBarEnabled(false);
this.setHorizontalScrollBarEnabled(false);
this.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
countDown++;
}
if(event.getAction() == MotionEvent.ACTION_UP && isLongPress)
{
//do selection
}
return gestureScanner.onTouchEvent(event);
}
});
}
我还实施了手势的功能:onflingdo,onsingletapup,onshowpress,onlongpress,ondown
这就是我在活动中称呼网络浏览量的方式:
testWV = (newBTWebView) findViewById(R.id.mywebview1);
testWV.setVerticalScrollBarEnabled(false);
testWV.setHorizontalScrollBarEnabled(false);
testWV.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
super.shouldOverrideUrlLoading(view, url);
return false;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
}
@Override
public void onPageFinished(WebView view, String url) {
}
}
如何保持手势并仍然捕获超链接点击?
this.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
countDown++;
}
if(event.getAction() == MotionEvent.ACTION_UP && isLongPress)
{
//do selection
}
gestureScanner.onTouchEvent(event);
return false;
}
});
在OnTouch回调中,返回false以实现默认触摸行为。
请使用以下代码,它将解决该问题:
testWV .setWebChromeClient(new WebChromeClient());
testWV .getSettings().setJavaScriptEnabled(true);
testWV .getSettings().setPluginsEnabled(true);
testWV .loadUrl(url);
testWV .setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
});