单击另一个活动上的每个按钮更改单个WebViewActivity的URL?

时间:2017-05-28 06:43:43

标签: java android android-webview

  

我是android开发的初学者,我需要帮助。我有两个   活动,MainActivity包含3个按钮(Google,Facebook,Twitter),   WebViewActivity包含webview,如何在WebViewActivity中更改URL   在每个按钮上单击?

     

例如,如果我点击谷歌按钮,它将移动到   WebViewActivity和网址" goole.com"应该是开放的   WebViewActivity

     

如果我点击Facebook按钮,它将移动到相同的WebViewActivity和   网址" facebook.com"应该在同一个WebViewActivity中打开

     

如果我点击Twitter按钮,它将移动到相同的WebViewActivity和   网址" twitter.com"应该在同一个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>

1 个答案:

答案 0 :(得分:0)

我自己解决了,将Bundle添加到每个按钮。

  

现在在MainActivity.java

public class LogicalOpTable {

    public static void main(String[] args) {
        int p,q;

        System.out.println("P\t Q\t AND\t OR\t XOR\t NOT\n");

        p = 1;
        q = 1;

        System.out.println(p+ "\t " + q + "\t " + (p&q) + "\t " + (p|q) + "\t " + (p^q) + "\t " + !p );

        p = 1;
        q = 0;

        System.out.println(p + "\t " + q + "\t " + (p&q) + "\t " + (p|q) + "\t " + (p^q) + "\t " + !p);

        p = 0;
        q = 1;

        System.out.println(p + "\t " + q + "\t " + (p&q) + "\t " + (p|q) + "\t " + (p^q) + "\t " + !p);

        p = 0;
        q = 0;

        System.out.println(p + "\t " + q + "\t " + (p&q) + "\t " + (p|q) + "\t " + (p^q) + "\t " + !p);


    }

}
  

WebViewActivity.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);
        }
    }