Example usage for android.webkit WebView setOnKeyListener

List of usage examples for android.webkit WebView setOnKeyListener

Introduction

In this page you can find the example usage for android.webkit WebView setOnKeyListener.

Prototype

public void setOnKeyListener(OnKeyListener l) 

Source Link

Document

Register a callback to be invoked when a hardware key is pressed in this view.

Usage

From source file:com.jiandanbaoxian.fragment.DialogFragmentCreater.java

/**
 * Dialog ???/*from ww w  .ja v a 2s  .  c  om*/
 * @param mContext
 * @return
 */
private Dialog showLicenseChoiceDialog(final Context mContext) {
    View convertView = LayoutInflater.from(mContext).inflate(R.layout.dialog_license_choice, null);
    final Dialog dialog = new Dialog(mContext, R.style.mystyle);

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.layout_rule:
                if (isAgree) {
                    isAgree = false;
                    ImageView imageView = (ImageView) v.findViewById(R.id.iv_choose);
                    //                            TextView textView = (TextView)v.findViewById(R.id.tv_rule);
                    imageView.setImageResource(R.drawable.icon_choose);
                    //                            textView.setBackgroundColor(getResources().getColor(R.color.bg_gray_color_level_0));
                    //                            textView.setTextColor(getResources().getColor(R.color.tv_gray_color_level_3));
                } else {
                    isAgree = true;
                    ImageView imageView = (ImageView) v.findViewById(R.id.iv_choose);
                    //                            TextView textView = (TextView)v.findViewById(R.id.tv_rule);
                    imageView.setImageResource(R.drawable.icon_choose_selected);
                    //                            textView.setBackgroundResource(R.drawable.btn_select_base_shape_0);
                    //                            textView.setTextColor(getResources().getColor(R.color.white_color));
                }
                break;
            }
            if (onLicenseDialogClickListener != null) {
                onLicenseDialogClickListener.onClick(v, isAgree);
            }
        }
    };

    TitleBar titleBar;
    SwipeRefreshLayout swipeLayout;
    final WebView webView;
    ProgressBar progressBar;
    LinearLayout layoutRule;
    LinearLayout layoutConfirm;

    titleBar = (TitleBar) convertView.findViewById(R.id.title_bar);
    swipeLayout = (SwipeRefreshLayout) convertView.findViewById(R.id.swipe_layout);
    webView = (WebView) convertView.findViewById(R.id.webView);
    progressBar = (ProgressBar) convertView.findViewById(R.id.progress_bar);
    layoutRule = (LinearLayout) convertView.findViewById(R.id.layout_rule);
    layoutConfirm = (LinearLayout) convertView.findViewById(R.id.layout_confirm);

    WebChromeClient client = new AppChromeWebClient(titleBar, progressBar, swipeLayout);
    webView.setWebChromeClient(client);
    webView.setWebViewClient(new AppWebClient());
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl(webViewURL);
    webView.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
                    webView.goBack(); //?
                    return true; //?
                }
            }
            return false;
        }
    });

    titleBar.initTitleBarInfo("", R.drawable.arrow_left, -1, "", "");
    titleBar.setOnTitleBarClickListener(new TitleBar.OnTitleBarClickListener() {
        @Override
        public void onLeftButtonClick(View v) {
            if (webView.canGoBack()) {
                webView.goBack(); //?
                return; //?
            }

            if (onLicenseDialogClickListener != null) {
                onLicenseDialogClickListener.onClick(v, isAgree);
                dismiss();
            }
        }

        @Override
        public void onRightButtonClick(View v) {

        }
    });
    UIUtils.initSwipeRefreshLayout(swipeLayout);
    swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            webView.reload();
        }
    });

    layoutConfirm.setOnClickListener(listener);
    layoutRule.setOnClickListener(listener);

    dialog.setContentView(convertView);
    dialog.getWindow().setWindowAnimations(R.style.dialog_right_control_style);
    return dialog;
}