您的位置:首页 > 移动开发 > Android开发

fragment之间的参数传递

2014-10-24 23:49 260 查看
使用Fragment的时候可能需要在两个Fragment之间进行参数的传递,找到一个方法就能实现像Activity一样便捷的实现参数传递 程序中的一段代码

                Restart sf = new Restart(this.getActivity(),w,h);                

                Bundle bundle = new Bundle();

                bundle.putString("second",second+"" );  //second 为一个数字,将它变成string

                bundle.putString("count",count+"" );

                sf.setArguments(bundle);  

                FragmentTransaction transaction = getFragmentManager().beginTransaction();

                transaction.replace(R.id.container, sf);

                transaction.commit();

   可以使用bundle进行参数传递,这样在两个Fragment跳转的时候就可以带上参数了,在另外一个Fragment获取参数的方式只需要一个语句

         String second = getArguments().getString("second");

            String count = getArguments().getString("count");

            TextView tv=(TextView)view.findViewById(R.id.score);

            tv.setText(second+":"+(100-Integer.parseInt(count)))

           second和count是自己定义的一个标识,参数的形式只要bundle能传递都可以实现
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android fragment