Webview无法与HTML select元素一起使用

时间:2019-03-02 08:19:13

标签: java android html webview

因此我的Web视图无法与select元素一起使用。换句话说,当我单击任意页面上的选择元素时,它不会显示菜单。

html选择元素示例:

        NestedWebView nwv = new NestedWebView(activity.getApplicationContext());
    nwv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    registerForContextMenu(nwv);
    nwv.setWebViewClient(new webViewClient());
    nwv.setWebChromeClient(new webChromeClient(nwv));
    nwv.addJavascriptInterface(new JSInterface(), "drconsole");
    nwv.getSettings().setJavaScriptEnabled(true);
    nwv.getSettings().setLoadWithOverviewMode(true);
    nwv.getSettings().setUseWideViewPort(true);
    nwv.getSettings().setSupportZoom(true);
    nwv.getSettings().setBuiltInZoomControls(true);
    nwv.getSettings().setDisplayZoomControls(false);
    nwv.getSettings().setAllowFileAccess(true);
    nwv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    nwv.getSettings().setSupportMultipleWindows(true);
    nwv.getSettings().setGeolocationEnabled(true);
    nwv.setDrawingCacheEnabled(true);
    nwv.getSettings().setPluginState(WebSettings.PluginState.ON);
    nwv.loadUrl(url);

    nwv.setDownloadListener(new DownloadListener(){
        @Override
        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength){
            try{
                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

                String filename = new URL(url).getFile();

                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename.substring(filename.lastIndexOf('/')+1));
                DownloadManager dm = (DownloadManager) activity.getSystemService(DOWNLOAD_SERVICE);
                dm.enqueue(request);

                Toast toast = new Toast(activity.getApplicationContext());
                View iview = activity.getLayoutInflater().inflate(R.layout.activity_toast, (ViewGroup) activity.findViewById(R.id.toast_root));

                TextView text = (TextView) iview.findViewById(R.id.text);
                text.setText("Downloading File");
                toast.setDuration(Toast.LENGTH_SHORT);
                toast.setView(iview);
                toast.show();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    });

java创建我的网络视图:

android {
compileSdkVersion 27
buildToolsVersion '28.0.3'
defaultConfig {
    applicationId "deeproot.theanarch.org.deeproot"
    minSdkVersion 16
    targetSdkVersion 27
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    vectorDrawables.useSupportLibrary = true
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

我的build.gradle

'options' => extension_loaded('pdo_mysql') ? array_filter([
    PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],

我几乎还拥有使用webviewclient和webchromeclient的所有可能。在对我的问题投票之前,请先发表评论。

2 个答案:

答案 0 :(得分:0)

您必须扩展WebViewClient,这里有详细的介绍https://stackoverflow.com/a/49350716/4159854

答案 1 :(得分:0)

要解决此错误,请务必使用活动而不是应用程序上下文来初始化Webview。

此行:

NestedWebView nwv = new NestedWebView(activity.getApplicationContext());

必须是:

NestedWebView nwv = new NestedWebView(activity);