如何在webView上将不同的URL与Button链接

时间:2019-04-18 10:59:13

标签: android android-webview

我是android的新手。我想制作一个Web View App。我在主要活动上有10个按钮。我想用每个按钮打开不同的URL。像Google的一个按钮和Twitter的一个按钮。如何计划这个应用程式?

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    facebook = findViewById(R.id.Layout_facebook);
    instagram = findViewById(R.id.layout_instagram);
    twitter = findViewById(R.id.layout_twitter);

    instagram.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, Instagram.class);
            startActivity(intent);
        }
    });

    facebook.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, Facebook.class);
            startActivity(intent);
        }
    });

    twitter.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, Twitter.class);
            startActivity(intent);
        }
    });

2 个答案:

答案 0 :(得分:0)

您的操作,然后单击按钮:-

           setContentView(theWebPage);
           WebView theWebPage = new WebView(this);
           theWebPage.getSettings().setJavaScriptEnabled(true);
           theWebPage.getSettings().setPluginState(PluginState.ON); 



 instagram.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            theWebPage.loadUrl("your instagram url");
        }
    });

    facebook.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            theWebPage.loadUrl("your facebook url");
        }
    });
    twitter.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            theWebPage.loadUrl("your twitter url");
        }
    });

答案 1 :(得分:0)

您可以使用一个DetailActivity类使用意图显示从MainActivity接收的任何URL。

String instagram = "https://instagram.com";
String twitter = "https://twitter.com";
String facebook = "https://facebook.com";

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    facebook = findViewById(R.id.Layout_facebook);
    instagram = findViewById(R.id.layout_instagram);
    twitter = findViewById(R.id.layout_twitter);

    instagram.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent instagram = new Intent(MainActivity.this, DetailActivity.class);
            instagram.putExtra("ig",instagram);
            startActivity(instagram);
        }
    });

    facebook.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent fb = new Intent(MainActivity.this, DetailActivity.class);
            fb.putExtra("fb",facebook);
            startActivity(intent);
        }
    });

    twitter.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent twitter = new Intent(MainActivity.this, DetailActivity.class);
            twitter.putExtra("twitter",twitter);
            startActivity(intent);
        }
    });

对于DetailActivity类

public class DetailActivity extends AppCompatActivity {

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail_activity);


        mWebView.clearCache(true);
        mWebView.clearHistory();
        mWebView.getSettings().setJavaScriptEnabled(true);

        Intent intent = this.getIntent();
        if (intent != null) {
            Bundle data = getIntent().getExtras();
            if (data.containsKey("ig")) {
                url = data.getString("ig");
            }

            if (data.containsKey("fb")) {
                url = data.getString("fb");
            }

            if (data.containsKey("twitter")) {
                url = data.getString("twitter");
            }

            mWebView.loadUrl(url);
        }
    }
}

我希望它能帮助您实现目标。