Android - Webview page requires double clicking - how to handle?

别说谁变了你拦得住时间么 提交于 2019-12-13 06:26:40

问题


My app is simply a WebView to display a specific website. Users are not able to simply use the Chrome app because of the inherent programming in Android which alters the functions of tapping.

For simplicity, let's say the website displays a calendar and on that calendar are boxes. The boxes can either be single clicked to be selected or double clicked to be opened to display the event details.

The website has coding that handles what to do if an event is single or double clicked:

<div class="boardpiece clickable" onclick="select(event, this);" ondblclick="open('5657849');"

I'm completely fine with using longpress to simulate the double click. Here's where my confusion begins. How to I tell my app that when the user performs a SingleTap on the item, it needs to activate the "onclick" the website is calling for? I also need to tell the app that when the user performs a longpress on that same item, activate the "ondblclick" event you see there in the website coding?

Here is my activity code:

import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.SslErrorHandler;
import android.net.http.SslError;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;

public class MainActivity extends Activity{
    WebView webview;    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webview = (WebView) findViewById(R.id.webview);   
        //Do we want/need to enable Java?
        webview.getSettings().setJavaScriptEnabled(true); 
        //Here we allow for zoom controls - pinch
        webview.getSettings().setBuiltInZoomControls(true);
        //Here we remove the zoom control buttons - requires API 11
        webview.getSettings().setDisplayZoomControls(false);
        //Here we clear the Cache and SSL Preferences       
        webview.clearCache(true);
        webview.clearSslPreferences();
        //Do we need to enable scroll bars to allow people to scroll left and right?        
        webview.setHorizontalScrollBarEnabled(true);
        webview.setVerticalScrollBarEnabled(true);
        webview.setWebViewClient(new WebViewClient());
        webview.loadUrl("https://website");

        final GestureDetector gd = new GestureDetector(new MyGestureDetector());
        View.OnTouchListener gl = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                return gd.onTouchEvent(event);
            }
        };
        webview.setOnTouchListener(gl);
    }

    class MyGestureDetector extends SimpleOnGestureListener {    
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            Log.i("", "DoubleTap");
            return true;
        }   

        public boolean onSingleTap(MotionEvent e) {
            Log.i("", "SingleTap");
            return true;
        }
    }    


// Ignore SSL certificate errors    
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        handler.proceed();
    }

//Would like to have a Menu Button to refresh the page - or really just bring you to the login page - for use when the session times out    
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(1, 1, 0, "Reload");
    //getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
    }

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()){
    case R.id.reload:
        webview.loadUrl("website");
        return true;
    }
    return super.onOptionsItemSelected(item);

}

}

回答1:


I think you're looking at this wrong. Instead of detecting the double tap in Android and trying to inject it into your Javascript you could be detecting the double tap in Javascript. Just keep the time of the last tap and if it's been recently enough the second tap is part of a double tap.



来源:https://stackoverflow.com/questions/20810023/android-webview-page-requires-double-clicking-how-to-handle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!