Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Android WebView链接在将onTouch listener设置为该链接后,只需双击即可响应_Javascript_Android_Webview - Fatal编程技术网

Javascript Android WebView链接在将onTouch listener设置为该链接后,只需双击即可响应

Javascript Android WebView链接在将onTouch listener设置为该链接后,只需双击即可响应,javascript,android,webview,Javascript,Android,Webview,我创建了一个WebView,在我的WebView中有一个按钮,当它单击时,它会更改WebView中的页面。我还在WebView上设置了一个触控监听器。但是在设置了触控监听器之后,按钮只会在快速双击时响应。我的意思是你应该点击两次,点击之间没有连续的间隔。这是我的密码。我们将非常感谢您的帮助 @SuppressLint("SetJavaScriptEnabled") public class DashboardFragment extends Fragment implements

我创建了一个WebView,在我的WebView中有一个按钮,当它单击时,它会更改WebView中的页面。我还在WebView上设置了一个触控监听器。但是在设置了触控监听器之后,按钮只会在快速双击时响应。我的意思是你应该点击两次,点击之间没有连续的间隔。这是我的密码。我们将非常感谢您的帮助

    @SuppressLint("SetJavaScriptEnabled")
    public class DashboardFragment extends Fragment implements SolutionHandler {
        private static final String SAVE_DASHBOARD_PAGE = "saveDashboardPage";
        private static final String SAVE_DASHBOARD_WEB_STATE = "saveDashboardWebState";


    private static final String DASHBOARD_URL = "https://api.myApp.com/api/dashboard/";
    private static final String LOGOUT_URL = "myapp://logout/";
    private static final String SOLUTION_URL = "mytapp://solution/open?solution_id";
    private static final String MY_APP = "myapp://";


        private View dashboardView;
        private WebView dashboardWebView;

        private String currentUrl;
        private User user;

        private LoadingDialog loadingDialog;

        private Bundle webState;

        private int supportUrlAcessCount = 0;

        public DashboardFragment() {
             setRetainInstance(true);
        }

        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            dashboardView = inflater.inflate(R.layout.fragment_dashboard, container, false);

            user = User.sharedUser(getActivity());

            if (savedInstanceState != null) {
                currentUrl = savedInstanceState.getString(SAVE_DASHBOARD_PAGE);
                webState = savedInstanceState.getBundle(SAVE_DASHBOARD_WEB_STATE);
            }

            if (currentUrl == null || currentUrl.length() == 0) {
                currentUrl = DASHBOARD_URL + user.hash();
            }

            loadingDialog = new LoadingDialog(getActivity(), R.string.loading_dashboard, R.string.icon_arrows_cw, true);
            WebChromeClient webClient = new WebChromeClient();

            if (dashboardWebView == null) {
                dashboardWebView = new WebView(getActivity());
                dashboardWebView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

                dashboardWebView.setVerticalScrollBarEnabled(true);
                dashboardWebView.setHorizontalScrollBarEnabled(true);
                dashboardWebView.requestFocusFromTouch();
                dashboardWebView.getSettings().setAppCachePath(getActivity().getCacheDir().getAbsolutePath());
                dashboardWebView.getSettings().setAppCacheEnabled(true);
                dashboardWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);

    //          dashboardWebView.setOnTouchListener(webViewTouchListener);

                dashboardWebView.setWebChromeClient(webClient);
                dashboardWebView.setWebViewClient(new WebViewClient() {
                    @Override
                    public void onPageFinished(WebView view, String url) {
                        if (url.startsWith(DASHBOARD_URL)) {
                            view.clearHistory();
                            loadingDialog.dismiss();
                        }
                    }

                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        if (url != null && url.startsWith(OHITAPP)) {
                            if (url.startsWith(SOLUTION_URL)) {
                                loadingDialog.setStringResource(R.string.loading_solution);
                                loadingDialog.show();

                                Locker.lock(getActivity());

                                Pattern pattern = Pattern.compile("[0-9]+");
                                Matcher matcher = pattern.matcher(url);

                                String solutionId = "";

                                if (matcher.find()) {
                                    solutionId = matcher.group();
                                }

                                APITalker.sharedTalker().getSolution(user.hash(), Integer.valueOf(solutionId), false,
                                        APITalker.CALL_TYPES.CALL_TYPE_DEEP_LINK, DashboardFragment.this);
                            } else if (url.startsWith(LOGOUT_URL)) {
                                loadingDialog.dismiss();
                                ErrorHelper.showError(TalkersConstants.HASH_INVALID, (SuperActivity) getActivity());
                            }

                            return true;
                        } else if (url.startsWith("http://support")) {
                            supportUrlAcessCount++;
                            if (supportUrlAcessCount == 2) {
                                ((MenuActivity) getActivity()).changeFragment(6, false, false, false, null, null);

                                return true;
                            }

                            return false;
                        } else {
                            return false;
                        }
                    }


                    @Override
                    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                        showNoInternetError();
                    }
                });

                if (webState == null) {
                    if (CheckConnectionHelper.isNetworkAvailable(getActivity())) {
                        loadingDialog.show();
                        dashboardWebView.loadUrl(currentUrl);
                    } else {
                        ErrorHelper.showError(TalkersConstants.JUST_FAILURE, (SuperActivity) getActivity());
                        dashboardWebView.loadUrl("about:blank");
                    }


                    dashboardWebView.getSettings().setSupportZoom(true);
                    dashboardWebView.getSettings().setBuiltInZoomControls(true);
                    dashboardWebView.getSettings().setDisplayZoomControls(false);
                    dashboardWebView.getSettings().setLoadWithOverviewMode(true);
                    dashboardWebView.getSettings().setUseWideViewPort(true);
                    dashboardWebView.getSettings().setJavaScriptEnabled(true);
                } else {
                    dashboardWebView.restoreState(webState);
                }
            } else {
                if (webState == null) {
                    loadingDialog.show();
                    dashboardWebView.loadUrl(currentUrl);

                    dashboardWebView.getSettings().setSupportZoom(true);
                    dashboardWebView.getSettings().setBuiltInZoomControls(true);
                    dashboardWebView.getSettings().setDisplayZoomControls(false);
                    dashboardWebView.getSettings().setLoadWithOverviewMode(true);
                    dashboardWebView.getSettings().setUseWideViewPort(true);
                    dashboardWebView.getSettings().setJavaScriptEnabled(true);
                } else {
                    dashboardWebView.restoreState(webState);
                }
            }

            LinearLayout dashboardContainer = (LinearLayout) dashboardView.findViewById(R.id.dashboard_container);
            dashboardContainer.addView(dashboardWebView);

            getActivity().getWindow().getDecorView().getViewTreeObserver()
                    .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                        @Override
                        public void onGlobalLayout() {
                            dashboardView.requestLayout();
                        }
                    });

    //      SuperActivity.savedLastClickTime = System.currentTimeMillis();

            return dashboardView;
        }

        @Override
        public void onDestroyView() {
            LinearLayout dashboardContainer = (LinearLayout) dashboardView.findViewById(R.id.dashboard_container);
            dashboardContainer.removeView(dashboardWebView);

            super.onDestroyView();
        }

        @Override
        public void onSaveInstanceState(Bundle outState) {
            outState.putString(SAVE_DASHBOARD_PAGE, getCurrentPage());
            outState.putBundle(SAVE_DASHBOARD_WEB_STATE, getCurrentWebViewState());

            super.onSaveInstanceState(outState);
        }

        public String getCurrentPage() {
            return dashboardWebView.getUrl();
        }

        public Bundle getCurrentWebViewState() {
            Bundle outState = new Bundle();
            dashboardWebView.saveState(outState);

            return outState;
        }

        @Override
        public void solutionSuccess(int solutionId, boolean isVoice, String title, String html, String speech) {
            loadingDialog.dismiss();

            Solution solution = new Solution(solutionId, title);

            ArrayList<Solution> solutions = new ArrayList<>();
            solutions.add(solution);

            ActivityOptions options = ActivityOptions.makeCustomAnimation(getActivity(), R.anim.flip_right_in, R.anim.flip_right_out);
            Intent solutionIntent = new Intent(getActivity(), SolutionActivity.class);
            solutionIntent.putExtra(SolutionActivity.ACCESS_METHOD_EXTRA, APITalker.ACCESS_METHODS.ACCESS_METHOD_DEEPLINKSOLUTION);
            solutionIntent.putExtra(SolutionActivity.HTML_EXTRA, html);
            solutionIntent.putExtra(SolutionActivity.SPEECH_EXTRA, speech);
            solutionIntent.putParcelableArrayListExtra(SolutionActivity.SOLUTIONS_EXTRA, solutions);
            solutionIntent.putExtra(SolutionActivity.POSITION_EXTRA, 0);
            startActivityForResult(solutionIntent, SolutionActivity.REQUEST_CODE, options.toBundle());

            Locker.unlock(getActivity());
        }

        @Override
        public void solutionFailure(String error) {
            loadingDialog.dismiss();

            if (error.equals(TalkersConstants.HASH_INVALID)) {
                ((MenuActivity) getActivity()).logout(null);
            }

            ErrorHelper.showError(error, (SuperActivity) getActivity());

            Locker.unlock(getActivity());
        }

        public boolean navigatesBack() {
            if (dashboardWebView != null && dashboardWebView.canGoBack()) {
                dashboardWebView.goBack();

                return true;
            }

            return false;
        }

        public void showNoInternetError() {
            loadingDialog.dismiss();
            ErrorHelper.showError(TalkersConstants.JUST_FAILURE, (SuperActivity) getActivity());
            dashboardWebView.loadUrl("about:blank");
        }

        public void reloadWebView() {
            if (dashboardWebView.getUrl() == null
                    || dashboardWebView.getUrl().isEmpty()
                    || dashboardWebView.getUrl().equals("about:blank")) {
                dashboardWebView.loadUrl(currentUrl);
            }
        }

        OnTouchListener webViewTouchListener = new OnTouchListener() {

            @Override
            public boolean onTouch(View view, MotionEvent event) {

                if (System.currentTimeMillis() - SuperActivity.savedLastClickTime < 2000) {
                    Log.d("I AM IN A FALSE", "FALSE");
                    return false;
                }

                SuperActivity.savedLastClickTime = System.currentTimeMillis();
                Log.d("I AM IN A TOUCH LISTENER", "TOUCH LISTENER");
                view.performClick();
                return true;
            }
        };




    }

所以,每次点击应用程序都会进入touch listener 3次:第一次是进入touch listener,第二次是进入“false”部分。但是为什么呢?

请把你的行程公布出来,当然可以。但正如你所见,这很简单您正在使用fragment,然后在布局中使用clickable=“true”进行尝试不幸的是,这没有帮助,您在哪里放大了“dashboardView”视图?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dashboard_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

</LinearLayout>
@Override
        public boolean onTouch(View view, MotionEvent event) {

            if (System.currentTimeMillis() - SuperActivity.savedLastClickTime < 2000) {
                Log.d("I AM IN A FALSE", "FALSE");
                return false;
            }

            SuperActivity.savedLastClickTime = System.currentTimeMillis();
            Log.d("I AM IN A TOUCH LISTENER", "TOUCH LISTENER");
            view.performClick();
            return true;
        }
    };
11-26 15:01:21.747: D/I AM IN A TOUCH LISTENER(16231): TOUCH LISTENER
11-26 15:01:21.817: D/I AM IN A FALSE(16231): FALSE
11-26 15:01:21.837: D/I AM IN A FALSE(16231): FALSE
11-26 15:01:36.747: D/I AM IN A TOUCH LISTENER(16231): TOUCH LISTENER
11-26 15:01:36.837: D/I AM IN A FALSE(16231): FALSE
11-26 15:01:36.847: D/I AM IN A FALSE(16231): FALSE
11-26 15:01:39.387: E/NMSP(16231): [bz] Session Idle for too long, longer than [50] (initiated from XMode.parseBcp)
11-26 15:01:39.597: E/NMSP(16231): [bz] XMode.parseXModeMsg() unknown protocol: 0
11-26 15:01:39.597: I/SpeechKit(16231): Disconnected
11-26 15:01:42.367: D/I AM IN A TOUCH LISTENER(16231): TOUCH LISTENER
11-26 15:01:42.407: D/I AM IN A FALSE(16231): FALSE
11-26 15:01:42.457: D/I AM IN A FALSE(16231): FALSE
11-26 15:01:42.537: D/I AM IN A FALSE(16231): FALSE
11-26 15:01:49.407: D/I AM IN A TOUCH LISTENER(16231): TOUCH LISTENER
11-26 15:01:49.457: D/I AM IN A FALSE(16231): FALSE
11-26 15:01:49.527: D/I AM IN A FALSE(16231): FALSE
11-26 15:01:49.597: D/I AM IN A FALSE(16231): FALSE