为了解决在android种,fragment 数据传递带给大家的困扰,今天特别写了一个Demo,这个demo 能够实现 Activity 向Fragment,和Fragment向Activity传递数据,Fragment 向Fragment 之间传递数据!,下面我就贴出 这个demo 的源代码,第一次写blog ,可能写的不好,希望大家提出宝贵的意见!
首先上MainActivity布局
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:text="此DEMO由小峰提供,QQ:184480354"
android:textColor="#030303"
android:textSize="18sp" />
android:layout_height="fill_parent"
android:layout_below="@id/tvMsg"
android:orientation="vertical" >
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
layout="@layout/fragment_layout" />
android:name="com.example.androidfragment.AFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
android:name="com.example.androidfragment.BFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
接下来上Fragment 加载的布局:
android:layout_height="fill_parent" >
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="1" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginTop="2dp"
android:textColor="#CD2626"
android:textSize="15sp" />
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/myActivity"
android:layout_marginTop="5dp" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:layout_marginLeft="5dp" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_marginRight="5dp" />
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/relactivityshow"
android:layout_marginTop="5dp" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:layout_marginLeft="25dp" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_marginRight="25dp" />
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/relactivityget" >
android:layout_width="fill_parent"
android:layout_height="35dp"
android:layout_toLeftOf="@id/btn_one"
android:hint="内容..." />
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/relactivitysendXML" >
android:layout_width="fill_parent"
android:layout_height="35dp"
android:layout_toLeftOf="@id/btn_two"
android:hint="内容..." />
MainActivity类:
package com.example.androidfragment;
import com.example.androidfragment.AFragment.setActivityData;
import com.example.androidfragment.AFragment.setDataToBfragment;
import com.example.androidfragment.BFragment.setResultToActivity;
import com.example.androidfragment.BFragment.setResultToBFragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends FragmentActivity implements setActivityData,
setResultToActivity, setResultToBFragment, setDataToBfragment {
private RelativeLayout layout_activity;
private AFragment aFragment;
private BFragment bFragment;
private Button btnSendOne;
private Button btnSendTwo;
private EditText etOne;
private EditText etTwo;
private TextView tvOne;
private TextView tvTwo;
private TextView title;
private TextView tvTitleOne;
private TextView tvTitleTwo;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
layout_activity = (RelativeLayout) findViewById(R.id.myActivity);
aFragment = (AFragment) getSupportFragmentManager().findFragmentById(
R.id.aFragment);
bFragment = (BFragment) getSupportFragmentManager().findFragmentById(
R.id.bFragment);
btnSendOne = (Button) layout_activity.findViewById(R.id.btn_one);
btnSendTwo = (Button) layout_activity.findViewById(R.id.btn_two);
etOne = (EditText) layout_activity.findViewById(R.id.et_one);
etTwo = (EditText) layout_activity.findViewById(R.id.et_two);
tvOne = (TextView) layout_activity.findViewById(R.id.tv_one);
tvTwo = (TextView) layout_activity.findViewById(R.id.tv_two);
title = (TextView) layout_activity.findViewById(R.id.title);
tvTitleOne = (TextView) layout_activity.findViewById(R.id.tvTitle_one);
tvTitleTwo = (TextView) layout_activity.findViewById(R.id.tvTitle_two);
tvTitleOne.setText("AFragment信息");
tvTitleTwo.setText("BFragment信息");
btnSendOne.setText("发送AFragment");
btnSendTwo.setText("发送BFragment");
title.setText("Activity");
tvOne.setText("...");
tvTwo.setText("...");
btnSendOne.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String et = etOne.getText().toString().trim();
if (!"".equals(etOne.getText().toString().trim())) {
aFragment
.setAfragmentMsg(etOne.getText().toString().trim());
} else {
Toast.makeText(MainActivity.this, "输入框不能为null",
Toast.LENGTH_LONG).show();
}
}
});
btnSendTwo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String et = etTwo.getText().toString().trim();
if (!"".equals(etTwo.getText().toString().trim())) {
bFragment
.setAfragmentMsg(etTwo.getText().toString().trim());
} else {
Toast.makeText(MainActivity.this, "输入框不能为null",
Toast.LENGTH_LONG).show();
}
}
});
}
@Override
public void setActivity(String msg) {
// TODO Auto-generated method stub
tvOne.setText(msg);
}
@Override
public void setResultToactivity(String msg) {
// TODO Auto-generated method stub
tvTwo.setText(msg);
}
@Override
public void setResultToBfragment(String msg) {
// TODO Auto-generated method stub
aFragment.getDataForBfragment(msg);
}
@Override
public void setDataToBfragment(String msg) {
// TODO Auto-generated method stub
bFragment.getAfragmentMsg(msg);
}
}
AFragment类:
package com.example.androidfragment;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class AFragment extends Fragment {
private Button btnSendOne;
private Button btnSendTwo;
private EditText etOne;
private EditText etTwo;
private TextView tvOne;
private TextView tvTwo;
private TextView title;
private TextView tvTitleOne;
private TextView tvTitleTwo;
private View view;
setActivityData setData;
setDataToBfragment setDataToBfragment;
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
if (!(activity instanceof setActivityData)
&& !(activity instanceof setDataToBfragment)) {
throw new IllegalStateException("error");
}
setData = (setActivityData) activity;
setDataToBfragment = (setDataToBfragment) activity;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.e("我被执行了.....", "我被执行了.....");
view = inflater.inflate(R.layout.fragment_layout, container, false);
btnSendOne = (Button) view.findViewById(R.id.btn_one);
btnSendTwo = (Button) view.findViewById(R.id.btn_two);
etOne = (EditText) view.findViewById(R.id.et_one);
etTwo = (EditText) view.findViewById(R.id.et_two);
tvOne = (TextView) view.findViewById(R.id.tv_one);
tvTwo = (TextView) view.findViewById(R.id.tv_two);
title = (TextView) view.findViewById(R.id.title);
tvTitleOne = (TextView) view.findViewById(R.id.tvTitle_one);
tvTitleTwo = (TextView) view.findViewById(R.id.tvTitle_two);
tvTitleOne.setText("Activity信息");
tvTitleTwo.setText("BFragment信息");
btnSendOne.setText("发送到Activity");
btnSendTwo.setText("发送BFragment");
title.setText("AFragment");
tvOne.setText("...");
tvTwo.setText("...");
btnSendOne.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!"".equals(etOne.getText().toString().trim())) {
setData.setActivity(etOne.getText().toString().trim());
} else {
Toast.makeText(getActivity(), "输入框不能为null",
Toast.LENGTH_LONG).show();
}
}
});
btnSendTwo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!"".equals(etTwo.getText().toString().trim())) {
setDataToBfragment.setDataToBfragment(etTwo.getText()
.toString().trim());
} else {
Toast.makeText(getActivity(), "输入框不能为null",
Toast.LENGTH_LONG).show();
}
}
});
return view;
}
public void setAfragmentMsg(String msg) {
tvOne.setText(msg);
}
public interface setActivityData {
public void setActivity(String msg);
}
public void getDataForBfragment(String msg) {
if (msg != null) {
tvTwo = (TextView) view.findViewById(R.id.tv_two);
tvTwo.setText(msg);
}
}
public interface setDataToBfragment {
public void setDataToBfragment(String msg);
}
}
BFragment类:
package com.example.androidfragment;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class BFragment extends Fragment {
private Button btnSendOne;
private Button btnSendTwo;
private EditText etOne;
private EditText etTwo;
private TextView tvOne;
private TextView tvTwo;
private TextView title;
private TextView tvTitleOne;
private TextView tvTitleTwo;
private View view;
private setResultToActivity callback;
private setResultToBFragment toBfragment;
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
if (!(activity instanceof setResultToActivity)
&& !(activity instanceof setResultToBFragment)) {
throw new IllegalStateException("异常...");
}
callback = (setResultToActivity) activity;
toBfragment = (setResultToBFragment) activity;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
view = inflater.inflate(R.layout.fragment_layout, container, false);
btnSendOne = (Button) view.findViewById(R.id.btn_one);
btnSendTwo = (Button) view.findViewById(R.id.btn_two);
etOne = (EditText) view.findViewById(R.id.et_one);
etTwo = (EditText) view.findViewById(R.id.et_two);
tvOne = (TextView) view.findViewById(R.id.tv_one);
tvTwo = (TextView) view.findViewById(R.id.tv_two);
title = (TextView) view.findViewById(R.id.title);
tvTitleOne = (TextView) view.findViewById(R.id.tvTitle_one);
tvTitleTwo = (TextView) view.findViewById(R.id.tvTitle_two);
tvTitleOne.setText("Activity信息");
tvTitleTwo.setText("AFragment信息");
btnSendOne.setText("发送到Activity");
btnSendTwo.setText("发送AFragment");
title.setText("BFragment");
tvOne.setText("...");
tvTwo.setText("...");
btnSendOne.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!"".equals(etOne.getText().toString().trim())) {
callback.setResultToactivity(etOne.getText().toString()
.trim());
} else {
Toast.makeText(getActivity(), "输入框不能为null",
Toast.LENGTH_LONG).show();
}
}
});
btnSendTwo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!"".equals(etTwo.getText().toString().trim())) {
toBfragment.setResultToBfragment(etTwo.getText().toString()
.trim());
} else {
Toast.makeText(getActivity(), "输入框不能为null",
Toast.LENGTH_LONG).show();
}
;
}
});
return view;
}
public void setAfragmentMsg(String msg) {
tvOne.setText(msg);
}
public void getAfragmentMsg(String msg) {
tvTwo.setText(msg);
}
public interface setResultToActivity {
public void setResultToactivity(String msg);
}
public interface setResultToBFragment {
public void setResultToBfragment(String msg);
}
}
Demo下载地址:
http://download.csdn.net/detail/u013014573/7604577
如果大家在实现的同时,遇到了问题,也可以加我QQ,Demo里面有我的QQ 号,同时大家也欢迎大家加入,IT创业群:313246800