如何在单个webview中使用多个URL

时间:2016-03-15 05:47:40

标签: android android-webview

我是webView的新手,这里我点击了一个onclick事件,当我点击一个文本并在webview中打开时,当我点击文本2打开时,在同一个webview中任何一个请如何在同一个webview中放置两个onclick,每个textview字符串来自服务器 我试过这种方式,但没有使用任何ne请帮助我,我搜索谷歌,但他们是没有用的

下面是我的代码

Activity.java

 //webview onclick and get bundle
    webviewurl=NewsMainFregmant_List.listData.get(pos).getNewsSourceUrl();

    webviewurl2=NewsMainFregmant_List.listData.get(pos).getNewsSourceUrl2();

    news_site_link_one=(TextView)findViewById(R.id.news_SourceLink_text_one_t_webview);
    news_site_like_two=(TextView)findViewById(R.id.news_SourceLink_text_two_t_webview);

  news_site_link_one.setOnClickListener(new View.OnClickListener() {
      @Override
       public void onClick(View v) {
        Intent webviewintent = new Intent(getApplicationContext(), News_WebView.class);
       webviewintent.putExtra("webviewurl", webviewurl);
         startActivity(webviewintent);

    }
   });


    news_site_like_two.setOnClickListener(new View.OnClickListener() {
     @Override
      public void onClick(View v) {
        Intent webviewintent = new Intent(getApplicationContext(), News_WebView.class);
          webviewintent.putExtra("webviewurl2", webviewurl2);
      startActivity(webviewintent);


     }
   });

这是我的webview代码

 String SourceURL;
WebView webview;
final Activity activity = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_news__web_view);


    getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    setContentView(R.layout.activity_news__web_view);

    Intent intent =this.getIntent();

    if(intent!=null)


        SourceURL =  intent.getStringExtra("webviewurl");
    if(SourceURL.equals("webviewurl")) {

        webview = (WebView) findViewById(R.id.webView);
        webview.getSettings().setJavaScriptEnabled(true);
    }

    if(SourceURL.equals("webviewurl2")) {

        webview = (WebView) findViewById(R.id.webView);
        webview.getSettings().setJavaScriptEnabled(true);
    }





    webview.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress)
        {
            activity.setTitle("Loading...");
            activity.setProgress(progress * 100);

            if(progress == 100)
                activity.setTitle(R.string.app_name);
        }
    });

    webview.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            // Handle the error
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });

    webview.loadUrl(SourceURL);
}

2 个答案:

答案 0 :(得分:2)

使用containsKey

Bundle data =getIntent().getExtras();

if(data.containsKey("webviewurl"))
{
    SourceURL =data.getString("webviewurl");
}
else if(data.containsKey("webviewurl2"))
{
                SourceURL =data.getString("webviewurl2");
    }

答案 1 :(得分:0)

以下是我为多个网址尝试的代码段

Myactivity.java

public void openWeb() {

    impressum = (TextView) findViewById(R.id.web_link_1);
    konkact = (TextView) findViewById(R.id.web_link_2);
    uber = (TextView) findViewById(R.id.web_link_3);

    impressum.setOnClickListener(this);
    konkact.setOnClickListener(this);
    uber.setOnClickListener(this);
}

@Override
public void onClick(View v) {

    String disclaimer = "http://lucidspace.de/imprint.php";
    String contact = "http://lucidspace.de/contact.php";
    String about = "http://lucidspace.de/about-us.php";

    switch(v.getId()) {
        case R.id.web_link_1:
            Intent imprintIntent = new Intent(MainActivity.this, WebViewOpen.class);
            imprintIntent.putExtra("webivewImprint",disclaimer);
            this.startActivity(imprintIntent);
            break;

        case R.id.web_link_2:
            Intent contactIntent = new Intent(MainActivity.this, WebViewOpen.class);
            contactIntent.putExtra("webivewContact",contact);
            this.startActivity(contactIntent);
            break;

        case R.id.web_link_3:
            Intent aboutIntent = new Intent(MainActivity.this, WebViewOpen.class);
            aboutIntent.putExtra("webivewAbout",about);
            this.startActivity(aboutIntent);
            break;
    }

WebViewAcitivty

public class WebViewOpen extends AppCompatActivity {

    String url;
    WebView webView;
    final Activity webViewActivity = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        /*webView.setWebChromeClient(new WebChromeClient() {

            public void onProgressChanged(WebView wView, int progress) {
                webViewActivity.setTitle("Loading...");
                webViewActivity.requestWindowFeature(progress * 100);

                if(progress == 100) {
                    webViewActivity.setTitle(R.string.app_name);
                }
            }
        });*/

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_webview);

        Intent intent =this.getIntent();

        if(intent!= null) {

            Bundle data = getIntent().getExtras();

            if(data.containsKey("webivewImprint")) {
                url = data.getString("webivewImprint");
                webView = (WebView) findViewById(R.id.web_link);
                webView.clearCache(true);
                webView.clearHistory();
                webView.getSettings().setJavaScriptEnabled(true);
            }

            if(data.containsKey("webivewContact")) {
                url = data.getString("webivewContact");
                webView = (WebView) findViewById(R.id.web_link);
                webView.clearCache(true);
                webView.clearHistory();
                webView.getSettings().setJavaScriptEnabled(true);
            }

            if(data.containsKey("webivewAbout")) {
                url = data.getString("webivewAbout");
                webView = (WebView) findViewById(R.id.web_link);
                webView.clearCache(true);
                webView.clearHistory();
                webView.getSettings().setJavaScriptEnabled(true);
            }

            webView.loadUrl(url);

WebView.xml

<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/web_link"
android:layout_width="match_parent"
android:layout_height="match_parent" />

这对我有用