通过单击另一个 activity 上的每个按钮更改单个 WebViewActivity 的 URL?
Change URL of single WebViewActivity by clicking each button on another activity?
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.
请根据我的code.My代码告诉我:
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);
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.
请根据我的code.My代码告诉我:
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);