我的应用中有一个webview。我希望webview链接(点击时)加载到另一个webview中。 这是我的代码的一部分.webview1中的链接在设计浏览器中加载:
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);
答案 0 :(得分:1)
您的代码是正确的。
只是为了防止在设备默认浏览器中加载链接,您应该将webview客户端设置为:
webView2.setWebViewClient(new WebViewClient());
答案 1 :(得分:0)
如果要在其他活动中打开网址,请使用以下命令: 在activity1.java上添加这个。 (我使用按钮从活动转移到活动)
<强> Activity1.java 强>
声明:
Button button;
在oncrete下面:
button = (Button) findViewById(R.id.button1);
在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);
}
Activity1.java的XML部分
<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();
}
}
}
Activity2.java的XML:
<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>
对于自定义进度条: 将其添加到名为custom_progress.xml
的drawable上<?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>
我希望这会对你有所帮助。这将为您的webview带来惊人的外观更多此类访问http://www.techmobs.in