从片段调用带有javascript函数的活动

时间:2014-06-10 05:50:43

标签: javascript android android-fragments webview fragment

我正在尝试在加载片段后加载另一个活动。当我点击主页webview上的任何图标时,Toast命令正在工作,但意图不起作用。点击主页图标后,有没有办法加载其他活动。滑动菜单工作正常。我把这段代码放在HOME java类文件中。 Index类文件的位置不同。我尝试了两种方式,但它不起作用。你能帮帮我吗?

我的代码是:

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    String url = "file:///android_asset/www/index.htm";
    webView = (WebView) getView().findViewById(R.id.webviewhome);
    webView.setWebViewClient(new MyBrowser());
    webView.getSettings().setJavaScriptEnabled(true);
    webView.addJavascriptInterface(new WebAppInterface(getActivity()), "Android");
    webView.loadUrl(url);
}


public class WebAppInterface {

    Context mContext;
    WebAppInterface(Context c) {
        mContext = c;
    }
    @JavascriptInterface
    public void LoadAboutGames() {
        Toast.makeText(mContext, "This is About Games Activity", Toast.LENGTH_SHORT).show();
    }

    @JavascriptInterface
    public void LoadShowInfo() {
        Intent intent = new Intent(getActivity(), ShowInfo.class);
        startActivity(intent);
    }

    @JavascriptInterface
    public void LoadAppPhotos() {
        Toast.makeText(mContext, "Hello", Toast.LENGTH_SHORT).show();
    }

    @JavascriptInterface
    public void LoadAppVideos() {
        Intent intent = new Intent(getActivity(), Videos.class);
        startActivity(intent);
    }

    @JavascriptInterface
    public void LoadExhibitors() {
        Intent intent = new Intent(getActivity(), Exhibitor.class);
        startActivity(intent);
    }

    @JavascriptInterface
    public void LoadMessages() {
        Intent intent = new Intent(getActivity(), AppMessages.class);
        startActivity(intent);
    }

    @JavascriptInterface
    public void LoadTickets() {
        Intent intent = new Intent(getActivity(), Tickets.class);
        startActivity(intent);
    }

    @JavascriptInterface
    public void LoadContact() {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("file:///android_asset/www/contacts.htm"));
        getActivity().startActivity(intent);

    }

}

1 个答案:

答案 0 :(得分:0)

经过长时间的努力,我解决了我的问题,我只需要使用这段代码,现在它的工作正常,如我所料      FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction()。replace(R.id.frame_container,new ShowInfo())。commit();