如何在Android Studio中的单个Web视图中打开不同的URL。

huangapple go评论76阅读模式
英文:

How to open different url in single web view android studio

问题

以下是翻译好的内容:

如何在不同的按钮中打开不同的URL链接。我只是在创建一个包含网页视图的单独活动。

例如,一个按钮包含Google的链接。另一个按钮包含Facebook的链接,还有一个按钮是Gmail的链接。

现在,我不想为每次点击创建单独的活动 - 我只想创建一个包含网页视图的活动,当有人点击按钮时,他们会进入这个活动,并且所提供的链接将在自定义加载视图的情况下打开。

帮助将不胜感激。

英文:

How do I open different different URL stores in different different buttons. I'm just creating a separate activity that contains web view .

For example, one button contains links of Google. Another is containing links of Facebook, and another is Gmail.

Now I don't want to create separate activity for each click - I just want to create a activity that contains web view and when someone clicks the button they get inside this activity and the provided link will open with custom loading view

Help would be appreciated.

答案1

得分: 1

在需要显示 WebView 的活动中,只需在活动中创建一个方法,该方法接受一个字符串输入,并将此字符串用作 WebView 的 URL。在每个按钮的 onClick 中调用此函数,并将所需网站的 URL 作为参数传递,为所有按钮执行此操作。

英文:

just create the method in the activity in which u have to show webView, which takes a string as a input and use this string in place of URL of webView,
call this function in the onClick of each button and pass the URL of desired website as parameter, do this for all the buttons

答案2

得分: 0

你可以使用意图(Intent)将网站的URL发送到WebView活动,并相应地进行接收!在每次按钮点击时添加以下内容:

button1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent = new Intent(this, webview.class);
        intent.putExtra("URL", "your url");
        startActivity(intent);
    }
});

button2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent = new Intent(this, webview.class);
        intent.putExtra("URL", "your url2");
        startActivity(intent);
    }
});

在你的WebView活动中,接收这些URL并启动WebView,如下所示:

String url = getIntent().getExtras().getString("URL");
英文:

You can send the url of the website to the webview activity using intent and receive them accordingly!on each button click add you

    button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(this, webview.class);
        intent.putExtra("URL","your url");   
        startActivity(intent);
            }
        });
 button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(this, webview.class);
        intent.putExtra("URL","your url2");   
        startActivity(intent);
            }
        });

on your weview activity recive these urls and fire your webview as follows

  String url=getIntent().getExtras().getString("URL");

huangapple
  • 本文由 发表于 2020年9月23日 04:32:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64017281.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定