我有很多链接可以打开特定的网页。目前他们在浏览器中打开。他们在许多不同的类中,我想要做的是有一个Web View我可以充气或运行,它将响应我正在运行的活动。即所以我可以打开downloads.class网页,tutorials.class网页。所有来自一个网络视图。而不是每个类的Web视图。我想我正确地解释了自己,但我不确定如何开始这样做我自己。希望你们能帮忙谢谢
这是我目前使用的一些代码。但因为它的片段我不能做一个公共构造函数。我希望能够使用String url init从另一个类
更改Urlpublic class WebViewFragment extends Fragment {
private String curURL;
public void init(String url) {
curURL = url;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater
.inflate(R.layout.webviewlayout, container, false);
init("http://www.mediafire.com/download/ezbkyava2qz44b5/AllCast.apk");
if (curURL != null) {
WebView webview = (WebView) view.findViewById(R.id.DownloadWebNav);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new webClient());
webview.loadUrl(curURL);
webview.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
}
return view;
}
private class webClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
}
}
答案 0 :(得分:0)
你可以通过简单的java enum技术实现这一点。假设你有三个网页视图,你希望一次一个地从同一个活动中触发。
public enum MenuType {
ABOUTUS, FAQS, TERMSANDCONDITION
}
具有三个启动webview的按钮的活动/片段
// in OnCreate
Button mClickButton1 = (Button)findViewById(R.id.clickButton1);
mClickButton1.setOnClickListener(this);
Button mClickButton2 = (Button)findViewById(R.id.clickButton2);
mClickButton2.setOnClickListener(this);
Button mClickButton3 = (Button)findViewById(R.id.clickButton3);
mClickButton3.setOnClickListener(this);
// somewhere else in your code
public void onClick(View v) {
switch (v.getId()) {
case R.id.clickButton1: {
// launch ABOUT US webview.
startWebViewActivity(MenuType.ABOUTUS);
break;
}
case R.id.clickButton2: {
// launch FAQS webview.
startWebViewActivity(MenuType.FAQS);
break;
}
case R.id.clickButton3: {
// launch TERMSANDCONDITION webview.
startWebViewActivity(MenuType.TERMSANDCONDITION);
break;
}
default:
break;
}
// method triggered when button clicked
private void startWebViewActivity(MenuType menuType) {
Intent intent = new Intent(this, WebViewActivity.class);
intent.putExtra(WebViewActivity.INTENT_MENUTYPE, menuType);
startActivity(intent);
}
WebViewActivity.java
上课
public class WebViewActivity extends AppCompactActivity {
public static final String INTENT_MENUTYPE = "intent_menu_type";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* webview layout with <Framelayout> as a child having id =container
that gets replaced by a fragment at run time.
[make your own layout here]
*/
setContentView(R.layout.activity_webview);
Bundle bundle = getIntent().getExtras();
if (bundle != null && bundle.containsKey(INTENT_MENUTYPE)) {
MenuType menuType = (MenuType) bundle.getSerializable(INTENT_MENUTYPE);
openFragment(WebViewFragment.newInstance(menuType));
}
}
public void openFragment(Fragment fragment) {
FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
ft.replace(R.id.container,
fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commitAllowingStateLoss();
}
}
WebViewFragment.java
public class WebViewFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
private static final String TAG = WebViewFragment.class.getSimpleName();
WebView mWebView;
private MenuType mMenuType;
private String mUrl;
SwipeRefreshLayout mPullToLoad;
public static WebViewFragment newInstance(MenuType menuType) {
WebViewFragment fragment = new WebViewFragment();
fragment.setMenuType(menuType);
return fragment;
}
public void setMenuType(MenuType menuType) {
this.mMenuType = menuType;
}
public WebViewFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (mMenuType == MenuType.ABOUTUS) {
mUrl = " valid about us url";
} else if (mMenuType == MenuType.FAQS) {
mUrl = " valid FAQS url";
} else if (mMenuType == MenuType.TERMSANDCONDITION) {
mUrl = "valid terms and conditions url";
}
View view = inflater.inflate(R.layout.fragment_webview,
container, false);
// initialize views here.
int progressColor1 = ContextCompat.getColor(mContext, R.color.primary_color);
int progressColor2 = ContextCompat.getColor(mContext, R.color.primary_color_dark);
int progressColor3 = ContextCompat.getColor(mContext, R.color.dark_blue);
int progressColor4 = ContextCompat.getColor(mContext, R.color.light_orange);
mPullToLoad.setColorSchemeColors(progressColor2,progressColor3,progressColor4,progressColor1);
mPullToLoad.setOnRefreshListener(this);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDisplayZoomControls(true);
webSettings.setSupportZoom(true);
webSettings.setUseWideViewPort(true);
webSettings.setBuiltInZoomControls(true);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
showRefreshDialog();
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
dismissRefreshDialog();
}
});
mWebView.loadUrl(mUrl);
}
@Override
public void onRefresh() {
if(mWebView !=null) {
mWebView.loadUrl(mUrl);
}
}
public void showRefreshDialog() {
mPullToLoad.post(new Runnable() {
@Override
public void run() {
if(mPullToLoad != null)
mPullToLoad.setRefreshing(true);
}
});
}
public void dismissRefreshDialog() {
if(mPullToLoad!=null && mPullToLoad.isShown() )
mPullToLoad.setRefreshing(false);
}
}
fragment_webview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/pull_to_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
希望这会有所帮助!!