xml文件(Activity):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.administrator.dashixun2_fragmentvsactivity.MainActivity"> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="我是Activity" android:textSize="20dp" /> <Button android:id="@+id/bt2" android:text="点击接收Fragment的传值" android:layout_width="match_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@+id/fl" android:layout_width="match_parent" android:layout_height="500dp"></FrameLayout> </LinearLayout>
xml(Fragment):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#ff00" tools:context="com.example.administrator.dashixun2_fragmentvsactivity.mFragment"> <TextView android:id="@+id/fragment" android:text="我是fragment" android:layout_gravity="center" android:textSize="30dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:textColor="#ffffff" android:id="@+id/text" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:text="等待Activity发送消息" /> <Button android:layout_marginTop="100dp" android:id="@+id/button" android:layout_gravity="center" android:text="点击接收Activity消息" android:layout_centerInParent="true" android:textSize="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
MainActivity:
public class MainActivity extends AppCompatActivity { TextView text; private FragmentTransaction transaction; private FragmentManager manager; private Button button; private mFragment fragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text = findViewById(R.id.text); button = findViewById(R.id.bt2); // 获取管理 manager = getSupportFragmentManager(); // 开启事务 transaction = manager.beginTransaction(); //获取fragment fragment = new mFragment(); //获取bundele Bundle bundle = new Bundle(); // 添加数据 bundle.putString("message", "厉害了我的哥"); // 传到fragment中 fragment.setArguments(bundle); //添加fragment transaction.add(R.id.fl, fragment).commit(); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { fragment.sendMessage(new mFragment.OnfragmentListener() { @Override public void fragmentListener(String s) { text.setText(s); } }); } }); } }
Fragment:
Button button; TextView text; Bundle bundle; String message; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.activity_m_fragment, container, false); // 设置布局文件 button = (Button) view.findViewById(R.id.button); text = (TextView) view.findViewById(R.id.text); // 通过getArgments()获取从Activity传过来的全部值 bundle = getArguments(); //根据key获取键 message = this.bundle.getString("message"); //设置按钮,将设置的值显示出来 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 显示传递过来的值 text.setText(message); } }); return view; } //接口回调 public interface OnfragmentListener{ void fragmentListener(String s); } //提供一个供外部访问的方法 public void sendMessage(OnfragmentListener onfragmentListener){ onfragmentListener.fragmentListener("来自fragment的消息"); } }