【问题标题】:Clicks Does Not Working in WebView点击在 WebView 中不起作用
【发布时间】:2017-05-03 14:05:15
【问题描述】:

我的 android 应用中有一个 webview。
我已经加载了网址:https://imsnsit.org/imsnsit/notifications.php
有各种通知的链接。当我点击它们时,我的 WebView 什么也不做。 它适用于其他一切 - Chrome (Android)、Chrome(Desktop)。链接很好。我注意到通知的一件事是带有 PHP 文件的 href。仅导航到该链接是行不通的。我得到无效的操作。加上链接不是静态的,每次刷新页面时它们都会改变(只有plum_url.php的参数)。
我已经在使用这些功能了。没有任何帮助。
webSettings.setJavaScriptEnabled(true); webSettings.setJavaScriptCanOpenWindowsAutomatically(true); webSettings.setDomStorageEnabled(true);

【问题讨论】:

    标签: javascript php android webview


    【解决方案1】:

    我检查了您的链接,所有链接都打开了一个 android 网络视图不支持的 pdf 文件。您可以在 google docs 或您设备中的某些应用程序中打开此类链接。 实现 shouldOverrideUrlLoading 类似的东西。

     @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if ( urlIsPDF(url)){
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse(url), "application/pdf");
            try{
                view.getContext().startActivity(intent);
            } catch (ActivityNotFoundException e) {
                //user does not have a pdf viewer installed
            }
        } else {
            webview.loadUrl(url);
        }
        return true;
    }
    

    【讨论】:

    • 您已尝试过该网站,但您是否尝试过此代码,因为它也不起作用。
    【解决方案2】:

    你可以像这样在 webview 中打开新窗口

    WebView webviewContact=(WebView)findViewById(R.id.webviewcontact);  
    
    webviewContact.loadUrl("https://imsnsit.org/imsnsit/notifications.php");
    
    webviewContact.clearCache(true);
    
    webviewContact.clearHistory();
    //webviewContact.getSettings().setDomStorageEnabled(true);
    //webviewContact.getSettings().setSupportMultipleWindows(true);
    webviewContact.getSettings().setJavaScriptEnabled(true);        webviewContact.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webviewContact.setWebViewClient(new WebViewClient(){
    /*For open new window in webview*/
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {                                    Toast.makeText(getApplicationContext(),url,Toast.LENGTH_SHORT).show();
                    view.loadUrl(url);
                    return false;
                }  
            });
    

    【讨论】:

    • 你遇到了什么问题?
    • 当我点击链接时它什么也没做,这就是问题
    【解决方案3】:

    https://www.mkyong.com/android/android-webview-example/
    创建两个 Android 布局文件——“res/layout/main.xml”和“res/layout/webview.xml”。

    文件:res/layout/main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <Button
            android:id="@+id/buttonUrl"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Go to http://www.google.com" />
    
    </LinearLayout>
    

    文件:res/layout/main.xml – WebView 示例

    <?xml version="1.0" encoding="utf-8"?>
    <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    />
    

    两个活动类,一个活动显示一个按钮,另一个活动显示带有预定义URL的WebView。

    文件:MainActivity.java

    package com.mkyong.android;
    
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class MainActivity extends Activity {
    
        private Button button;
    
        public void onCreate(Bundle savedInstanceState) {
            final Context context = this;
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            button = (Button) findViewById(R.id.buttonUrl);
    
            button.setOnClickListener(new OnClickListener() {
    
              @Override
              public void onClick(View arg0) {
                Intent intent = new Intent(context, WebViewActivity.class);
                startActivity(intent);
              }
    
            });
    
        }
    
    }
    

    文件:WebViewActivity.java

    package com.mkyong.android;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.webkit.WebView;
    
    public class WebViewActivity extends Activity {
    
        private WebView webView;
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.webview);
    
            webView = (WebView) findViewById(R.id.webView1);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.loadUrl("http://www.google.com");
    
        }
    
    }
    

    WebView 需要 INTERNET 权限,将下面添加到 AndroidManifest.xml 中。

    <uses-permission android:name="android.permission.INTERNET" />
    

    文件:AndroidManifest.xml - 查看完整示例。

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.mkyong.android"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk android:minSdkVersion="10" />
    
        <uses-permission android:name="android.permission.INTERNET" />
    
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
            <activity
                android:name=".WebViewActivity"
                android:theme="@android:style/Theme.NoTitleBar" />
    
            <activity
                android:label="@string/app_name"
                android:name=".MainActivity" >
                <intent-filter >
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    

    它会帮助你。

    https://www.mkyong.com/android/android-webview-example/

    【讨论】:

      【解决方案4】:

      我建议您开始使用更快更好的自定义标签。详细实现在https://developer.chrome.com/multidevice/android/customtabs

      // Use a CustomTabsIntent.Builder to configure CustomTabsIntent.
      // Once ready, call CustomTabsIntent.Builder.build() to create a CustomTabsIntent
      // and launch the desired Url with CustomTabsIntent.launchUrl()
      
      String url = ¨https://paul.kinlan.me/¨;
      CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
      CustomTabsIntent customTabsIntent = builder.build();
      customTabsIntent.launchUrl(this, Uri.parse(url));
      

      如果您不打算从 Web 视图重定向到应用程序,自定义选项卡将是一个很好的解决方案。 webview 的问题非常具体到您的实现,所以不能在这里建议一个解决方案。请粘贴您如何打开 Web 视图的实现以及您尝试打开的 php 内容或链接是什么。

      【讨论】:

      • 实际上我的应用中有一个通知列表。我想在用户单击通知时打开通知。问题是我想用 Javascript 打开第一个通知(比如说),所以自定义选项卡不是选项
      猜你喜欢
      • 1970-01-01
      • 2021-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多