gpt4 book ai didi

java - 通过单击另一个 Activity 上的每个按钮来更改单个 WebView Activity 的 URL?

转载 作者:行者123 更新时间:2023-11-30 00:31:41 25 4
gpt4 key购买 nike

I'm beginner in android development, i need help. I have two activities, MainActivity contain 3 Buttons(Google, Facebook, Twitter), WebViewActivity contain webview, how to change URL in WebViewActivity on each button click?

For example if i click on Google Button it will move to WebViewActivity and the url "goole.com" should be open in same WebViewActivity

if i click on Facebook Button it will move to same WebViewActivity and the url "facebook.com" should be open in same WebViewActivity

if i click on Twitter Button it will move to same WebViewActivity and the url "twitter.com" should be open in same WebViewActivity.

请根据我的代码告诉我,我的代码是:

MainActivity.java:

public class MainActivity extends AppCompatActivity {

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

public void LoadThisUrl(View view) {
if(view==findViewById(R.id.Google)){
Intent i = new Intent(this, WebViewActivity.class);
startActivity(i);
}
if(view==findViewById(R.id.Facebook)){
Intent i = new Intent(this, WebViewActivity.class);
startActivity(i);
}
if(view==findViewById(R.id.Twitter)){
Intent i = new Intent(this, WebViewActivity.class);
startActivity(i);
}
}}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.myapplication.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Google"
android:id="@+id/Google"
android:onClick="LoadThisUrl"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Facebook"
android:id="@+id/Facebook"
android:onClick="LoadThisUrl"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Twitter"
android:id="@+id/Twitter"
android:onClick="LoadThisUrl"/>
</LinearLayout>

WebViewActivity.java

public class WebViewActivity extends AppCompatActivity {
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http:\\www.atifsoftwares.blogspot.com");
}
}

activity_webview.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.myapplication.WebViewActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView">
</WebView>
</LinearLayout>

最佳答案

我自己解决了这个问题,为每个按钮添加了 Bundle。

Now in MainActivity.java

public void LoadThisUrl(View view) {
if(view==findViewById(R.id.Google)){
Intent i = new Intent(this, WebViewActivity.class);
i.putExtra("Url", "http://google.com");
startActivity(i);
}
if(view==findViewById(R.id.Facebook)){
Intent i = new Intent(this, WebViewActivity.class);
i.putExtra("Url", "http://facebook.com");
startActivity(i);
}
if(view==findViewById(R.id.Twitter)){
Intent i = new Intent(this, WebViewActivity.class);
i.putExtra("Url", "http://twitter.com");
startActivity(i);
}
}

WebViewActivity.java

webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
//webView.loadUrl("http:\\www.atifsoftwares.blogspot.com");
Bundle b = getIntent().getExtras();
String url = b.getCharSequence("Url").toString();
webView.loadUrl(url);

关于java - 通过单击另一个 Activity 上的每个按钮来更改单个 WebView Activity 的 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44224537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com