android webview:网站控件无法正常工作

时间:2014-03-29 11:05:18

标签: android android-webview

我已经创建了android webview应用程序。

但网页上的链接或按钮无效。

我可以说它没有正确加载。

您可以在此处找到MainActivity.java

package com.tunghuynh.comparotel;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.res.Configuration;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends Activity {

    private WebView webView;
    private ProgressBar progress;
    private RelativeLayout splash;
    //private RelativeLayout imgSplash;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        splash = (RelativeLayout) findViewById(R.id.webview);
        //imgSplash = (RelativeLayout) findViewById(R.id.imgSplash);
        splash.setVisibility(View.INVISIBLE);

        webView = new WebView(this);
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                MainActivity.this.setValue(newProgress);
                super.onProgressChanged(view, newProgress);
                try {
                    if (newProgress >= 100) {
                        splash.setVisibility(View.VISIBLE);
                        splash.addView(webView);
                        progress.setVisibility(View.INVISIBLE);
                    }
                } catch (Exception e) {
                }
            }
        });
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

        progress = (ProgressBar) findViewById(R.id.progressBar);
        progress.setMax(100);
        String url = Global.urlComparotel;
        switch (Global.type) {
        case 0:
            url = Global.urlComparotel;
            break;
        case 1:
            url = Global.urlHotelcito;
            break;
        case 2:
            url = Global.urlHotelinha;
            break;
        case 3:
            url = Global.urlHotelomat;
            break;
        case 4:
            url = Global.urlOtelcik;
            break;
        default:
            url = Global.urlComparotel;
        }
        openWeb(url);

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        setContentView(R.layout.activity_main);
    }

    private void openWeb(String url) {
        webView.loadUrl(url);
        MainActivity.this.progress.setProgress(0);
    }

    public void setValue(int progress) {
        this.progress.setProgress(progress);
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;// super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub

        switch (item.getItemId()) {
        case R.id.item1:
            webView.reload();
            break;
        case R.id.item2:
            shareTextUrl();
            break;
        case R.id.item3:
            Intent int1 = new Intent(MainActivity.this, AboutUs.class);
            startActivity(int1);
            break;
        case R.id.item4:
            rateApp();
            break;
        case R.id.item5:
            finish();
            break;
        }
        //shareTextUrl();
        return false;// super.onOptionsItemSelected(item);
    }


    private void rateApp() {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("market://details?id=" + this.getPackageName()));
        if (!MyStartActivity(intent)) {
            intent.setData(Uri
                    .parse("https://play.google.com/store/apps/details?id="
                            + this.getPackageName()));
            if (!MyStartActivity(intent)) {
                Toast.makeText(
                        this,
                        "Could not open Android market, please install the market app.",
                        Toast.LENGTH_SHORT).show();
            }
        }
    }

    private boolean MyStartActivity(Intent aIntent) {
        try {
            startActivity(aIntent);
            return true;
        } catch (ActivityNotFoundException e) {
            return false;
        }
    }

    private void shareTextUrl() {
        Intent share = new Intent(android.content.Intent.ACTION_SEND);
        share.setType("text/plain");
        share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        share.putExtra(Intent.EXTRA_TEXT,
                "https://play.google.com/store/apps/details?id=" + this.getPackageName());
        startActivity(Intent.createChooser(share, "Share application!"));
    }
}

http://www.a2b4.net/MainActivity.java

我们如何解决问题。

1 个答案:

答案 0 :(得分:0)

由于您的网站需要javascript,因此您应该在网络视图中启用javascript执行:

 webview.getSettings().setJavaScriptEnabled(true);

另见documentation

更新:您可以通过添加以下内容来取消警告消息Warning: Using setJavaScriptEnabled can introduce XSS vulnerabilities into you application

 @SuppressLint("SetJavaScriptEnabled") // add this
 @Override
 protected void onCreate(Bundle savedInstanceState) {

在onCreate方法之上。

相关问题