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

fragment向Activity传数据

先上图
Activity和fragment的数据交互(fragment向Activity传数据)_第1张图片
Fragment向activity中传值

1.在Fragment中写一个回调接口

2.在activity中实现这个回调接口

3.在Fragment中onAttach 方法中得到activity中实现好的 实例化接口对象

4.用接口的对象 进行传值
具体代码如下:
MainActivity.java

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

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

public class MainActivity extends AppCompatActivity implements FragmentTest.SendValue, MyFragment.MySendValue {
    private TextView textView;
    Button btn_my;
    Button btn_test;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=findViewById(R.id.textView);
        btn_my=findViewById(R.id.changeToMy);
        btn_test=findViewById(R.id.changeToTest);
        btn_my.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.layout_container_fragment,new MyFragment());
                fragmentTransaction.commit();
            }
        });
        btn_test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.layout_container_fragment,new FragmentTest());
                fragmentTransaction.commit();
            }
        });
    }

    @Override
    public void send(String s) {
        textView.setText(s);
    }

    @Override
    public void mySend(String s) {
        textView.setText(s);
    }
}

activity_main.xml


    
        

    

    

MyFragment.java

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import androidx.fragment.app.Fragment;

public class MyFragment extends Fragment {
    MySendValue mMySendValue;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mMySendValue= (MySendValue) getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.layout_fragment,null);
        final EditText editText=view.findViewById(R.id.editText_my);
        Button button=view.findViewById(R.id.button_my);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s=editText.getText().toString();
                mMySendValue.mySend(s);
            }
        });
        return view;
    }
    public interface MySendValue{
        void mySend(String s);
    }
}

layout_fragment.xml




    

    

FragmenTest.java

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import androidx.fragment.app.Fragment;

public class FragmentTest extends Fragment {
    SendValue mSendValue;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mSendValue= (SendValue) getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.test_fragment,null);
        final EditText editText=view.findViewById(R.id.EditText_test);
        Button button=view.findViewById(R.id.btn_test);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s=editText.getText().toString();
                mSendValue.send(s);
            }
        });
        return view;
    }
    public interface SendValue{
        void send(String s);
    }
}

test_fragment.xml



    
    

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