通过Java的面向对象传递信息;
1、建立一个接口
package com.example.fragment;
public interface ITFragm {
void sendmsgToAct(String msg);
default String getmsgFromAct() {
return null;
}
}
2、写一个Fragment经行信息交流
package com.example.fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
/**
* A simple {@link Fragment} subclass.
* Use the factory method to
* create an instance of this fragment.
*/
public class BlankFragment2 extends Fragment {
private View root;
public TextView tv;
public Button btn1,btn;
private ITFragm itFragm1;
/**
* 该函数是一个公共的void类型函数,
* 名为setITFragm,接受一个参数itFragm2,
* 其类型为ITFragm。
* 函数的作用是将传入的itFragm2对象赋值给类中的itFragm1成员变量。
* 为匿名内部类做准备
* @param itFragm2
*/
public void setITFragm(ITFragm itFragm2){
itFragm1=itFragm2;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(root==null){
root=inflater.inflate((R.layout.fragment_blank1),container, false);
}
tv=root.findViewById(R.id.tv);
btn1=root.findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
itFragm1.sendmsgToAct("你好,我是Frag");
tv.setText("发送成功");//发送信息给Activity
}
});
btn=root.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
String msg= itFragm1.getmsgFromAct();
tv.setText(msg);//接收到来自Activity的信息
}
});
return root;
}
}
3、主界面
package com.example.fragment;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button btn2,btn3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn2=findViewById(R.id.btn2);
btn3=findViewById(R.id.btn3);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
}
//fragment与Activity之间通过Bundle传递数据
public void onClick(View v) {
switch (v.getId()){
case R.id.btn2:
Bundle bundle=new Bundle();
bundle.putString("name","张三");
BlankFragment1 f1=new BlankFragment1();
f1.setArguments(bundle);
replacefragment(f1);
break
;
//btn3通过面向对象的思想传递数据
case R.id.btn3:
BlankFragment2 blankFragment2=new BlankFragment2();
blankFragment2.setITFragm(new ITFragm(){
@Override
public void sendmsgToAct(String msg) {
Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();
}//接收来自Fragment的信息
@Override
public String getmsgFromAct() {
return "你好,我是Activity";
}//发送信息给Fragment
});
replacefragment(blankFragment2);
break;
}
}
/**
* 这个函数用于在Android开发中切换Fragment。
* 它通过获取FragmentManager和FragmentTransaction对象,
* 使用replace方法将当前的Fragment替换为新的BlankFragment1实例,并提交该事务。
* @param v
*/
public void replacefragment(Fragment v){
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment1,v);//替换
fragmentTransaction.addToBackStack(null);//添加到后退栈
fragmentTransaction.commit();//提交事务
}
}