Fragment 的通信,flutter 通知推送
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragrement_one, null);
et_input_text = view.findViewById(R.id.et_input_text);
btn_set_text = view.findViewById(R.id.btn_set_text);
//方法一:第一步:获取 TwoFragrement 对象,可以使用该对象的方法了
TwoFragrement two = (TwoFragrement) getActivity().getFragmentManager().findFragmentByTag("two");
//第三步:调用对象公开的方法
btn_set_text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
two.setText(et_input_text.getText().toString());
}
});
return view;
}
}
twoFragment 的 java 代码
package com.wust.twofragrement;
import android.app.Fragment;
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;
/**
ClassName: OneFragrement <br/>
Description: <br/>
date: 2021/5/27 16:26<br/>
@author yiqi<br />
@email:1820762465@qq.com
@QQ:1820762465
@since JDK 1.8
*/
public class TwoFragrement extends Fragment {
private TextView tv_show_text;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragrement_two, null);
tv_show_text = view.findViewById(R.id.tv_show_text);
return view;
}
//第二步:在第二个 fragment 里公开一个修改自身 TextView 的方法
public void setText(String str){
tv_show_text.setText(str);
}
}
MainActivity 的 java 代码
package com.wust.twofragrement;
import androidx.fragment.app.FragmentActivity;
import android.os.Bundle;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//第一步,获取 Fragrement
OneFragrement oneFragrement = new OneFragrement();
TwoFragrement twoFragrement = new TwoFragrement();
//第二步,占坑
getFragmentManager().beginTransaction().replace(R.id.fl_one,oneFragrement,"one").commit();
getFragmentManager().beginTransaction().replace(R.id.fl_two,twoFragrement,"two").commit();
}
}
方法二:使用回调
oneFragment 的 java 代码
package com.wust.twofragrement;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.app.Fragment;
import android.widget.Button;
import android.widget.EditText;
/**
ClassName: OneFragrement <br/>
Description: <br/>
date: 2021/5/27 16:26<br/>
@author yiqi<br />
@email:1820762465@qq.com
@QQ:1820762465
@since JDK 1.8
*/
public class OneFragrement extends Fragment {
private Button btn_set_text;
private EditText et_input_text;
private onReceiveData mOnReceiveData;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragrement_one, null);
et_input_text = view.findViewById(R.id.et_input_text);
btn_set_text = view.findViewById(R.id.btn_set_text);
btn_set_text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//第三步:调用
mOnReceiveData.receiveData(et_input_text.getText().toString());
}
});
return view;
}
//第一步:定义接口
public interface onReceiveData{
void receiveData(String s
);
}
//第二步:暴露接口
public void setOnReceiveDataListener(onReceiveData onReceiveData){
this.mOnReceiveData = onReceiveData;
}
}
twoFragment 的 java 代码
package com.wust.twofragrement;
import android.app.Activity;
import android.app.Fragment;
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;
评论