2017-09-13 2 views
0

Ich habe ein Webview in meiner App. Ich möchte die Webview-Links (wenn angeklickt) in einem anderen Webview geladen werden. hier ist ein Teil meines code.links in webview1 in devise Browser geladen werden:Load WebView Links in einem anderen Webview

 webView1.getSettings().setJavaScriptEnabled(true); 
     webView2.getSettings().setJavaScriptEnabled(true); 
     WebViewClient webViewClient = new WebViewClient() { 


      @Override 
      public boolean shouldOverrideUrlLoading(WebView view, String url) { 
       webView2.loadUrl(url); 
       //webView2.requestFocus(); 
       return true; 
      } 
     }; 
     webView1.setWebViewClient(webViewClient); 

Antwort

1

Ihr Code ist korrekt.
Um zu verhindern, dass Links im Standardbrowser des Geräts geladen werden, sollten Sie den Webview-Client wie folgt einstellen:

webView2.setWebViewClient(new WebViewClient()); 
0

Wenn Sie diese URL in einer anderen Tätigkeit Verwendung öffnen möchten: Auf activity1.java fügen Sie diese. (Bin mit einem Knopf von acticity zu Aktivität bewegen)

Activity1.java

Declare:

Button button; 

Unten oncrete:

button = (Button) findViewById(R.id.button1); 

Nach onCreate:

public void onClick1(View view) { 
     Intent intent = new Intent(Activity1.this,Activity2.class); 
     //Passing a URL to activity2 
     intent.putExtra("WebAddress","http://www.techmobs.in"); 
     Activity1.this.startActivity(intent); 
    } 

XML Teil Activity1.java

<Button 
       android:layout_width="100dp" 
       android:layout_height="40dp" 
       android:id="@+id/button1" 
       android:onClick="onClick1" 
       android:text="Blogging" /> 

Activity2.java

import android.content.Intent; 
import android.net.Uri; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.webkit.WebChromeClient; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.FrameLayout; 
import android.widget.ProgressBar; 
import android.widget.Toolbar; 
import com.google.android.gms.ads.AdRequest; 
import com.google.android.gms.ads.AdView; 

public class Website extends AppCompatActivity { 
    WebView webView; 
    private ProgressBar progressBar; 
    private FrameLayout frameLayout; 
    String urlopen; 
    private Toolbar mActionBar; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity2); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

     //Getting the URL from activity1.java 
     final String WebAddress = getIntent().getStringExtra("WebAddress"); 

     frameLayout = (FrameLayout) findViewById(R.id.frameprogress); 
     progressBar = (ProgressBar) findViewById(R.id.progress); 
     progressBar.setMax(100); 
     webView = (WebView) findViewById(R.id.web1); 
     webView.setWebViewClient(new HelpClient()); 
     webView.setWebChromeClient(new WebChromeClient(){ 
      public void onProgressChanged(WebView view, int progress){ 
       frameLayout.setVisibility(View.VISIBLE); 
       progressBar.setProgress(progress); 

       setTitle("Loading..."); 

       if(progress == 100){ 
        frameLayout.setVisibility(View.GONE); 
        setTitle(view.getTitle()); 
        urlopen = WebAddress; 

       } 
       super.onProgressChanged(view, progress); 
      } 
     }); 
     webView.getSettings().setJavaScriptEnabled(true); 
     webView.setVerticalScrollBarEnabled(false); 
     webView.loadUrl(WebAddress); 
     progressBar.setProgress(0); 

    } 
    //Help client class definition 
    class HelpClient extends WebViewClient { 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url)  { 
      view.loadUrl(url); 
      frameLayout.setVisibility(View.VISIBLE); 
      return true; 
     } 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     if (item.getItemId() == android.R.id.home) { 
      finish(); 
     } 
     return true; 
    } 

    @Override 
    public void onBackPressed() { 
     if (webView.canGoBack()){ 
      webView.goBack(); 
     } 
     else { 
     super.onBackPressed(); 
     } 
    } 
} 

XML für Activity2.java:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="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.yourpackage.Activity2"> 
    <FrameLayout 
     android:id="@+id/frameprogress" 
     android:layout_width="match_parent" 
     android:background="@android:color/transparent" 
     android:layout_height="3dp"> 
     <ProgressBar 
      android:id="@+id/progress" 
      android:layout_width="match_parent" 
      android:layout_height="8dp" 
      android:background="@android:color/transparent" 
      style="?android:attr/progressBarStyleHorizontal" 
      android:layout_gravity="top" 
      android:layout_marginTop="-3dp" 
      android:progressDrawable="@drawable/custom_progress" 
      android:progress="30"/> 
    </FrameLayout> 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <WebView 
     android:id="@+id/web1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    </WebView> 
</RelativeLayout> 
</LinearLayout> 

Für kundenspezifische Fortschrittsbalken: hinzufügen auf Drawables auf den Namen custom_progress.xml

<?xml version="1.0" encoding="utf-8"?> 
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
     <item android:id="@android:id/secondaryProgress"> 
      <clip> 
       <shape> 
        <corners android:radius="3dip" /> 
        <solid android:color="#4FC3F7" /> 
       </shape> 
      </clip> 
     </item> 
     <item android:id="@android:id/progress"> 
      <clip> 
       <shape> 
        <corners android:radius="3dip" /> 
        <solid android:color="#4FC3F7" /> 
       </shape> 
      </clip> 
     </item> 
    </layer-list> 

Ich hoffe, das Ihnen helfen würde. das würde ein erstaunliches Aussehen für Ihr webview geben Für mehr solcher Besuch http://www.techmobs.in