Android 使用WebView点击左上角返回键和物理返回键回到上一个H5页面或者退出当前页面的处理

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qinwendou/article/details/87937409

1.监听系统返回键,如果有上个html则返回,否则退出这个页面:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
        mWebView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

2.如果自定义了页面左上角返回的按钮,也要添加下面代码:

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.iv_back:
            if (mWebView.canGoBack()) {
                mWebView.goBack();//返回上个页面
                return;
            } else {
                finish();
            }

            break;
    }
}
展开阅读全文

点击返回键webview无效了。

04-21

具体情况是这样的:一个网页里面需要调用安卓的拍照功能和到图库中选择图片,所以用到了webview,点击网页按钮后,弹出安卓的对话框,“拍照” 或者“进入图库”,之后选择当前照片或者选定图库中的图片后,返回给网页。rnrn正常情况下,一路选择到图库,并且也选择了图片,这都没问题。但是如果在弹出选择对话框后,敲击对话框之外的任何地方,或者直接点击返回键,取消掉对话框,这个时候,再次点击网页按钮,就没有任何反应了。 此时的webview就只能看,上面的任何操作都变的无效了,包括网页链接也是如此! rnrn这是怎么回事啊。rnrn附上源码:rnrnwebView.setWebChromeClient(new MyWebChromeClient() rn rn //****************************** 实现文件上传 ************************rn // For Android 3.0+rn public void openFileChooser(ValueCallback uploadMsg, String acceptType) rn Log.d("myu", "For Android 3.0+");rn if (mUploadMessage != null) rn return;rn Log.d("myu", uploadMsg.toString() + " " + acceptType);rn rn mUploadMessage = uploadMsg; rn selectImage();rn rn // For Android < 3.0rn public void openFileChooser(ValueCallback uploadMsg) rn openFileChooser( uploadMsg, "" );rn rn // For Android > 4.1.1rn public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) rn Log.d("myu", "For Android 4.1.1");rn openFileChooser(uploadMsg, acceptType);rn rnrnrn protected final void selectImage() rnrn boolean flag = Environment.getExternalStorageState().equals(rn Environment.MEDIA_MOUNTED);rn if (!flag) rn Toast.makeText(this, "请插入手机存储卡再使用本功能", Toast.LENGTH_SHORT).show();rn return;rn rnrn Log.d("myu", "selectImage");rnrn String[] selectPicTypeStr = "拍照", "从相册中选择" ;rn AlertDialog.Builder builder = new AlertDialog.Builder(this);rn builder.setTitle("选择图片");rn // .setCancelable(false) 让返回键失效rn //builder.setOnKeyListener(onKeyListener);rnrn builder.setItems(selectPicTypeStr,rn new DialogInterface.OnClickListener() rn @Overridern public void onClick(DialogInterface dialog, int which) rn switch (which) rn case 0:rn // 相机拍摄rn openCarcme();rn break;rn case 1:rn // 手机相册rn chosePic();rn break;rn default:rn Log.d("myu", "nonono");rn break;rn rn compressPath = Environmentrn .getExternalStorageDirectory().getPath()rn + "/sspset/temp";rn new File(compressPath).mkdirs();rn compressPath = compressPath + File.separatorrn + "compress.jpg";rn rn );rn AlertDialog dialog = builder.create(); rn dialog.show();rn rnrn@Overridern protected void onActivityResult(int requestCode, int resultCode,rn Intent intent) rnrn Log.d("myu", String.valueOf(resultCode));rn rn super.onActivityResult(requestCode, resultCode, intent);rnrn Log.d("myu", "onActivityResult2");rn rn if (null == mUploadMessage)rn return;rn Uri uri = null;rn // 判断是何种返回rn if (resultCode == RESULT_OK) rn if (requestCode == REQ_CAMERA) rn afterOpenCamera();rn uri = cameraUri;rn else if (requestCode == REQ_CHOOSE) rn uri = afterChosePic(intent);rn rn mUploadMessage.onReceiveValue(uri);rn mUploadMessage = null;rnrn else if (resultCode == RESULT_CANCELED) rn // 取消rn Log.d("myu", "RESULT_CANCELED");rnrn rn 论坛

没有更多推荐了,返回首页