我在不同的fragment1中有2个按钮,在片段2中有另一个webview。每当我点击按钮时,我想在webview中打开2个不同的网站。请建议
答案 0 :(得分:2)
请试试这个 -
public class MainHomeActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_first, btn_second;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_home);
btn_first= (Button)findViewById(R.id.btn_first);
btn_second= (Button)findViewById(R.id.btn_second);
btn_second.setOnClickListener(this);
btn_first.setOnClickListener(this);
}
private void loadFragment(String url) {
WebViewFragment webViewFragment = new WebViewFragment();
Bundle b = new Bundle();
b.putString("url", url);
webViewFragment.setArguments(b);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_layout, webViewFragment);
fragmentTransaction.commit();
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_first:
loadFragment("http://www.google.com");
break;
case R.id.btn_second:
loadFragment("http://www.apple.com");
break;
}
}
}
/ activity_main_home.xml /
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/topPanel"
android:orientation="horizontal">
<Button
android:id="@+id/btn_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="first"/>
<Button
android:id="@+id/btn_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="second"/>
</LinearLayout>
<FrameLayout
android:id="@+id/fragment_layout"
android:layout_below="@+id/topPanel"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</RelativeLayout>
// WebViewFragment.java
public class WebViewFragment extends Fragment {
private View view;
private WebView webview;
ProgressBar webViewPbar;
private String url ;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
url = bundle.getString("url");
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_main, container, false);
initUI();
return view;
}
private void initUI() {
webViewPbar = (ProgressBar) view.findViewById(R.id.webViewPbar);
webview = (WebView) view.findViewById(R.id.webview);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webview.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
webViewPbar.setProgress(newProgress);
if (newProgress == 100) {
webViewPbar.setVisibility(View.GONE);
}
}
});
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
if (webViewPbar != null) {
webViewPbar.setVisibility(View.GONE);
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
if (webViewPbar != null) {
webViewPbar.setVisibility(View.GONE);
}
}
});
// webview.loadUrl("http://www.google.com");
webview.loadUrl(url);
}
}
// activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ProgressBar
android:id="@+id/webViewPbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
style="@android:style/Widget.Holo.Light.ProgressBar.Horizontal" />
<WebView android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
/ androidmanifest文件 /
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shajib.capturemirror">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
>
<activity android:name=".MainHomeActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>