Activity和fragment的数据交互(Activity向fragment传数据)

Activity向fragment传数据

先上图
Activity和fragment的数据交互(Activity向fragment传数据)_第1张图片
activity与fragment之间进行数据传递:在Activity中将要传递的数据封装在Bundle中,使用setArgument(Bundel bundel)方法传递数据,在要传递到的Fragment中 使用getArgment(),得到传递到的Bundle。
我这放了两个fragment,只需要看懂一个就行了。
具体代码如下:
MainActivity.java

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText editText=findViewById(R.id.main_editText);
        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle=new Bundle();
                bundle.putString("arg1",editText.getText().toString());
                Fragment1 fragment1=new Fragment1();
                fragment1.setArguments(bundle);
                FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.frameLayout,fragment1);
                fragmentTransaction.commit();
            }
        });

        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle=new Bundle();
                bundle.putString("arg2",editText.getText().toString());
                Fragment2 fragment2=new Fragment2();
                fragment2.setArguments(bundle);
                FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.frameLayout,fragment2);
                fragmentTransaction.commit();
            }
        });
    }
}

activity_main.xml




   
    
    
        

Fragmen1.java

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class Fragment1 extends Fragment {
    private static final String TAG = "Fragment1";
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment1,null);
        final TextView textView1=view.findViewById(R.id.textView_1);
        view.findViewById(R.id.receive1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {          //在这里接收数据,更新TextView
                Bundle bundle=getArguments();
                String s=bundle.getString("arg1");
                textView1.setText(s);
            }
        });
        return view;
    }
}

fragmen1.xml



fragment2.java

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class Fragment2 extends Fragment {
    private static final String TAG = "Fragment2";
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment2,null);
        final TextView textView=view.findViewById(R.id.textView2);
        view.findViewById(R.id.receive2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {          //在这里接收数据,更新TextView
                Bundle bundle=getArguments();
                String s=bundle.getString("arg2");
                textView.setText(s);
            }
        });
        return view;
    }
}

fragment2.xml



你可能感兴趣的:(android入门笔记)