Activity传数据给fragment

xiaoxiao2021-02-28  91

1、

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/content_frame"     android:layout_width="match_parent"     android:layout_height="match_parent"      android:fitsSystemWindows="true"     />

import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.widget.FrameLayout; public class MainActivity extends FragmentActivity { FrameLayout content_frame; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); content_frame = (FrameLayout) findViewById(R.id.content_frame); getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, new FrameLayoutFragment("传参数")) .commit(); } }

2、

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical" >     <TextView         android:id="@+id/frag_text"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="这是fragment" /> </LinearLayout>

import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class FrameLayoutFragment extends Fragment{ View view; TextView  frag_text; String aaaa; public FrameLayoutFragment(String string) { aaaa  = string; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.frame_layout, container, false); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { initView(); super.onActivityCreated(savedInstanceState); } private void initView() { frag_text = (TextView) view.findViewById(R.id.frag_text); frag_text.setText(aaaa + "aaa"); } }

转载请注明原文地址: https://www.6miu.com/read-24807.html

最新回复(0)