您可以使用接口传递数据.
分段:
public class Fragment2 extends Fragment { public interface onSomeEventListener { public voID someEvent(String s); } onSomeEventListener someEventListener; @OverrIDe public voID onAttach(Activity activity) { super.onAttach(activity); try { someEventListener = (onSomeEventListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement onSomeEventListener"); } } final String LOG_TAG = "myLogs"; public VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState) { VIEw v = inflater.inflate(R.layout.fragment2,null); button button = (button) v.findVIEwByID(R.ID.button); button.setonClickListener(new OnClickListener() { public voID onClick(VIEw v) { someEventListener.someEvent("Test text to Fragment1"); } }); return v; }}
活动:
public class MainActivity extends Activity implements onSomeEventListener{ @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); Fragment frag2 = new Fragment2(); FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.add(R.ID.fragment2,frag2); ft.commit(); } @OverrIDe public voID someEvent(String s) { Fragment frag1 = getFragmentManager().findFragmentByID(R.ID.fragment1); ((TextVIEw)frag1.getVIEw().findVIEwByID(R.ID.textVIEw)).setText("Text from Fragment 2:" + s); }}总结
以上是内存溢出为你收集整理的android – 将片段之间的数据传递到活动全部内容,希望文章能够帮你解决android – 将片段之间的数据传递到活动所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)