如何在单击WebView下拉菜单中的链接时更改片段或转到另一个活动。 目前我正在使用它下面的代码工作。但我需要转到另一个片段而不是Activity。
WebView webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
String url = "file:///android_asset/index.html";
webView.loadUrl(url);
if (url.equals("file:///android_asset/index5.html")) {
Intent intent = new Intent(MainActivity.this, NextActivity.class);
startActivity(intent);
return;
}
}
我试过了
WebView webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
String url = "file:///android_asset/index.html";
webView.loadUrl(url);
if (url.equals("file:///android_asset/index5.html")) {
// Fragment fr = new FragmentOne();
// fm = getFragmentManager();
// fragmentTransaction = fm.beginTransaction();
// fragmentTransaction.replace(R.layout.my_account, fr);
// fragmentTransaction.commit();
Fragment newFragment = new FragmentOne();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.layout.activity_main, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
}
FragmentOne.java
public class FragmentOne extends Fragment implements OnClickListener{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_account, container, false);
return view;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
但它没有替换片段
谢谢,
答案 0 :(得分:0)
这应该适合你:
mWebView.setWebViewClient(new MyWebViewClient());
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equals("file:///android_asset/index5.html")) {
//Launch new activity here
return true;
}
return false;
}
}