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
原创粉丝点击