2017-05-28 2 views
0

Ich bin Anfänger in Android-Entwicklung, ich brauche Hilfe. Ich habe zwei Aktivitäten, MainActivity enthalten 3 Buttons (Google, Facebook, Twitter), WebViewActivity enthalten Webview, wie URL in WebViewActivity bei jedem Klick auf die Schaltfläche ändern?Ändern Sie die URL einzelner WebViewActivity, indem Sie auf jede Schaltfläche einer anderen Aktivität klicken.

Zum Beispiel, wenn ich auf Google-Knopf klicken, wird es in derselben offen zu WebViewActivity und die URL „goole.com“ WebViewActivity

wenn ich auf Facebook Button klicken, wird es zu bewegen, um gleiche WebViewActivity bewegen sollte und die uRL „facebook.com“ sollte in derselben WebViewActivity

offen sein, wenn ich auf Twitter-Knopf klicken, wird es in derselben WebViewActivity offen gleichen WebViewActivity und die uRL „twitter.com“ sein bewegen sollte.

Bitte sagen Sie mir nach meiner code.My-Code ist:

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> 

Antwort

0

Ich löste es mein Selbst, das Hinzufügen Bundle jeder Taste.

Jetzt 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); 
Verwandte Themen