android fragment activity 数据传递
来源:互联网 发布:淘宝弓箭 编辑:程序博客网 时间:2024/04/30 01:41
下面贴出代码
MainActivity4
package com.example.android_fragment;import com.example.android_fragment.MyFragment5.MyListener;import android.app.Activity;import android.app.Fragment;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class MainActivity4 extends Activity implements MyListener {private EditText editext;private Button send;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.main4);editext = (EditText) findViewById(R.id.editText);send = (Button) findViewById(R.id.send);send.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubString text = editext.getText().toString();MyFragment5 fragment5 = new MyFragment5();Bundle bundle = new Bundle();bundle.putString("name", text);fragment5.setArguments(bundle);FragmentManager fragmentManager = getFragmentManager();FragmentTransaction beginTransaction = fragmentManager.beginTransaction();beginTransaction.add(R.id.layout, fragment5, "fragment5");beginTransaction.commit();Toast.makeText(MainActivity4.this, "向Fragment发送数据" + text,Toast.LENGTH_SHORT).show();}});FragmentManager fragmentManager = getFragmentManager();Fragment findFragmentById = fragmentManager.findFragmentById(R.id.frag); MyFragment frag=(MyFragment) findFragmentById; frag.setAaa("fragment静态传值");}@Overridepublic void thank(String code) {// TODO Auto-generated method stubToast.makeText(MainActivity4.this, "已成功接收到" + code + ",客气了!",Toast.LENGTH_SHORT).show();}}
MyFragment5
package com.example.android_fragment;import android.app.Activity;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;import android.widget.Toast;public class MyFragment5 extends Fragment{private String code="Thank you,Activity!";public MyListener listener;public interface MyListener{public void thank(String code);}@Overridepublic void onAttach(Activity activity) {// TODO Auto-generated method stublistener=(MyListener) activity;super.onAttach(activity);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubView view = inflater.inflate(R.layout.fragment2, container, false);TextView tv = (TextView) view.findViewById(R.id.text);String text=getArguments().get("name")+""; tv.setText(text);Toast.makeText(getActivity(), "已成功接收到"+text, Toast.LENGTH_SHORT).show();Toast.makeText(getActivity(), "向Activity发送"+code, Toast.LENGTH_SHORT).show();listener.thank(code);return view;}}
0 0
- android fragment activity 数据传递
- Android点滴-Activity和Fragment 传递数据
- Android-Activity与Fragment之间传递数据
- Android activity Bundle和fragment数据传递
- Android Activity/Fragment间的数据传递
- Android中传递数据(Activity、Fragment)
- android数据传递(三)之Activity传递到Fragment
- fragment向activity传递数据
- activity给fragment传递数据
- Activity向Fragment传递数据
- Fragment传递数据到Activity
- Fragment与Activity传递数据
- FragMent传递数据到Activity
- Activity向Fragment传递数据
- Activity向Fragment传递数据
- Android 笔记-Fragment 与 Activity之间传递数据
- Android笔记Fragment与Activity之间传递数据
- Android笔记Fragment与Activity之间传递数据
- windows下如何安装Python环境和运行python程序
- iPhone界面跳转及页面传值
- 用转换器完整的将PPT转换成PDF的方法
- 日常用的的js
- 如何选择一个适合自己的开源项目来阅读
- android fragment activity 数据传递
- 安装openssh的版本不一致问题解决
- PL/SQL Developer自动补全SQL技巧
- 了解CSS的查找匹配原理 让CSS更简洁、高效
- 经纬创投笔试题算法-php实现
- Android 底层知识拾零
- TCP/IP协议栈的基本工作原理
- 研究僧杂谈
- C++___的虚拟继承的一些总结