获取按钮以在webview中打开不同的URL

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取按钮以在webview中打开不同的URL相关的知识,希望对你有一定的参考价值。

我有一个大约十五个按钮的屏幕。每个按钮应该在另一个屏幕(设置为活动)的webview中打开特定URL。如何根据单击的按钮使webview显示正确的URL?

这是我的按钮代码:

package com.adanawtn.FunnMagViewerWindow;

import com.adanawtn.funnmagviewerwindow.R;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class mainactivity extends Activity implements OnClickListener
{
Button logobtn;
Button twitterbtn;
Button instagrambtn;
Button topeyebtn;
Button bestbtn;
Button gearsbtn;
Button artsbtn;
Button travelbtn;
Button eventsbtn;
Button dailybtn;
Button magbtn;
Button bttmeyebtn;
Button tvbtn;
Button mallbtn;

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start);


        Button twitterbtn = (Button) findViewById(R.id.twitterbtn);
        Button instagrambtn = (Button) findViewById(R.id.instagrambtn);
        Button topeyebtn = (Button) findViewById(R.id.topeyebtn);
        Button bestbtn = (Button) findViewById(R.id.bestbtn);
        Button gearsbtn = (Button) findViewById(R.id.gearsbtn);
        Button artsbtn = (Button) findViewById(R.id.artsbtn);
        Button travelbtn = (Button) findViewById(R.id.travelbtn);
        Button eventsbtn = (Button) findViewById(R.id.eventsbtn);
        Button dailybtn = (Button) findViewById(R.id.dailybtn);
        Button magbtn = (Button) findViewById(R.id.magbtn);
        Button bttmeyebtn = (Button) findViewById(R.id.bttmeyebtn);
        Button tvbtn = (Button) findViewById(R.id.tvbtn);
        Button mallbtn = (Button) findViewById(R.id.mallbtn);


 // logo            
      logobtn.setOnClickListener(new View.OnClickListener() {

          @Override
                public void onClick(View arg0) {
                    Intent logoClick = new Intent(Intent.ACTION_VIEW, Uri.parse("http://funnmag.com"));
                startActivity(logoClick);      
 }});


  // twitter
                twitterbtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View arg1) {

                        //Starting a new Intent

                        Intent twitClick = new Intent(Intent.ACTION_VIEW, Uri.parse("http://twitter.com/funnmagazine"));
                        // starting new activity

                        startActivity(twitClick);      
     }});
// instagram                    
                    instagrambtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View arg2) {

                            //Starting a new Intent

                            Intent instClick = new Intent(Intent.ACTION_VIEW, Uri.parse("http://instagram.com/funnmagazine"));

                            // starting new activity

                            startActivity(instClick);      
         }});

// topeye
                        topeyebtn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View arg3) {

                                //Starting a new Intent

                                Intent topClick = new Intent(Intent.ACTION_VIEW, Uri.parse("http://funnmag.com"));

                                // starting new activity

                                startActivity(topClick);      
             }});

    // best
                            bestbtn.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View arg4) {

                                    //Starting a new Intent

                                    Intent bestClick = new Intent(Intent.ACTION_VIEW, Uri.parse("http://funnmag.com/la"));

                                    // starting new activity

                                    startActivity(bestClick);      
                 }});
        // gears
                                gearsbtn.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View arg5) {

                                        //Starting a new Intent

                                        Intent gearsClick = new Intent(Intent.ACTION_VIEW, Uri.parse("http://funnmag.com/gear"));

                                        // starting new activity

                                        startActivity(gearsClick);      
                     }});
// arts
                                    artsbtn.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View arg6) {

                                            //Starting a new Intent

                                            Intent artsClick = new Intent(Intent.ACTION_VIEW, Uri.parse("http://funnmag.com/art"));


                                            // starting new activity

                                            startActivity(artsClick);      
                         }});

// travel
                                        travelbtn.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View arg7) {

                                                //Starting a new Intent

                                                Intent travelClick = new Intent(Intent.ACTION_VIEW, Uri.parse("http://funnmag.com/travel"));

                                                // starting new activity

                                                startActivity(travelClick);      
                             }});
 //events
                                            eventsbtn.setOnClickListener(new View.OnClickListener() {
                                                @Override
                                                public void onClick(View arg8) {

                                                    //Starting a new Intent

                                                    Intent eventsClick = new Intent(Intent.ACTION_VIEW, Uri.parse("http://funnmag.com/events"));


                                                    // starting new activity

                                                    startActivity(eventsClick);      
                                 }});
//daily
                                                dailybtn.setOnClickListener(new View.OnClickListener() {
                                                    @Override
                                                    public void onClick(View arg9) {

                                                        //Starting a new Intent

                                                        Intent dailyClick = new Intent(Intent.ACTION_VIEW, Uri.parse("http://funnmag.com/daily"));

                                                        // starting new activity

                                                        startActivity(dailyClick);      
                                     }});
    // mag
                                                    magbtn.setOnClickListener(new View.OnClickListener() {
                                                        @Override
                                                        public void onClick(View arg10) {

                                                            //Starting a new Intent

                                                            Intent magClick = new Intent(Intent.ACTION_VIEW, Uri.parse("http://funnmag.com/blog"));


                                                            // starting new activity

                                                            startActivity(magClick);      
                                         }});
// bttmeye
                                                        bttmeyebtn.setOnClickListener(new View.OnClickListener() {
                                                            @Override
                                                            public void onClick(View arg11) {

                                                                //Starting a new Intent

                                                                Intent bttmClick = new Intent(getApplicationContext(), Class1.class);

                                                                // starting new activity

                                                                startActivity(bttmClick);      
                                             }});
 // tv
                                                            tvbtn.setOnClickListener(new View.OnClickListener() {
                                                                @Override
                                                                public void onClick(View arg12) {

                                                                    //Starting a new Intent

                                                                    Intent tvClick = new Intent(Intent.ACTION_VIEW, Uri.parse("http://funnmag.com/funn-tv"));

                                                                    // starting new activity

                                                                    startActivity(tvClick);      
                                                 }});

    // mall
                                                                mallbtn.setOnClickListener(new View.OnClickListener() {
                                                                    @Override
                                                                    public void onClick(View arg13) {

                                                                        //Starting a new Intent

                                                                        Intent mallClick = new Intent(Intent.ACTION_VIEW, Uri.parse("http://funnmag.com/mall"));

                                                                        // starting new activity

                                                                        startActivity(mallClick);      
                                                     }});



 }

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub

}

}

以下是包含webview的活动的代码:

package com.adanawtn.FunnMagViewerWindow;

import com.adanawtn.funnmagviewerwindow.R;


import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.webkit.WebView;

public class Class1 extends Activity {

 @Override

    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_class1);


 WebView webview1 = (WebView) findViewById(R.id.webView1);


Intent logoClick = getIntent();
    String uri = logoClick.getStringExtra("uri");
    setContentView(webview1);
    webview1.loadUrl(uri);

Intent twitClick = getIntent();
    String twituri = twitClick.getStringExtra("uri");
    setContentView(webview1);
    webview1.loadUrl(twituri);   


Intent instClick = getIntent();
    String insturi = instClick.getStringExtra("uri");
    setContentView(webview1);
    webview1.loadUrl(insturi);  

    Intent bestClick = getIntent();
    String besturi = bestClick.getStringExtra("uri");
    setContentView(webview1);
    webview1.loadUrl(besturi);  

    Intent gearsClick = getIntent();
    String gearsuri = gearsClick.getStringExtra("uri");
    setContentView(webview1);
    webview1.loadUrl(gearsuri);     

 Intent artsClick = getIntent();
    String artsuri = artsClick.getStringExtra("uri");
    setContentView(webview1);
    webview1.loadUrl(artsuri);  

    Intent travelClick = getIntent();
    String traveluri = travelClick.getStringExtra("uri");
    setContentView(webview1);
    webview1.loadUrl(traveluri);    

 Intent eventsClick = getIntent();
    String eventsuri = eventsClick.getStringExtra("uri");
    setContentView(webview1);
    webview1.loadUrl(eventsuri); 

 Intent dailyClick = getIntent();
    String dailyuri = dailyClick.getStringExtra("uri");
    setContentView(webview1);
    webview1.loadUrl(dailyuri);     

 Intent magClick = getIntent();
    String maguri = magClick.getStringExtra("uri");
    setContentView(webview1);
    webview1.loadUrl(maguri);   

    Intent tvClick = getIntent();
    String tvuri = tvClick.getStringExtra("uri");
    setContentView(webview1);
    webview1.loadUrl(tvuri);    


Intent mallClick = getIntent();
    String malluri = mallClick.getStringExtra("uri");
    setContentView(webview1);
    webview1.loadUrl(malluri);  


}


}
答案

你应该像这个例子一样构建你的Intents

Intent instClick = new Intent(this, Class1.class);
instClick.putExtra("uri", "http://instagram.com/funnmagazine");
startActivity(instClick);

然后,您在webview活动中的代码应该可以工作,但是您只需要用于设置webview而不是全部15个代码的一小部分代码。

以上是关于获取按钮以在webview中打开不同的URL的主要内容,如果未能解决你的问题,请参考以下文章

带有 2 个按钮的 Android 主要活动可在 webview 中打开第二个活动

从按钮到 WebView XCODE 4.5 的 URL 列表

Android Webview问题:如何在单个活动中打开具有不同按钮的不同页面

如何基于按钮单击在新屏幕上打开webview中的URL

PhoneGap:在 Safari 中打开外部 URL

根据按钮链接在webview中显示特定的url