gpt4 book ai didi

java - 如何从底部 fragment 获取数据到主要 Activity ?

转载 作者:行者123 更新时间:2023-12-01 18:13:05 24 4
gpt4 key购买 nike

我已经创建了一个底部 fragment ,现在当在底部 fragment 中单击按钮时,我需要来自主 Activity 中底部 fragment 的一些数据。我通过创建界面看到了很少的答案,但对这个概念不是很清楚。

代码:

使用此函数调用 fragment 的主要 Activity :

public void showSchedule() {


BottomSheetFrag bottomSheetFragment = new BottomSheetFrag();


Bundle bundle = new Bundle();
bundle.putString("UserId", Parlour_BeauticianID);

bottomSheetFragment.setArguments(bundle);
bottomSheetFragment.show(getSupportFragmentManager(), bottomSheetFragment.getTag());


}

fragment :

public class BottomSheetFrag extends BottomSheetDialogFragment {
public static BottomSheetFrag newInstance() {
return new BottomSheetFrag();
}

public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {

final View view = inflater.inflate(R.layout.activity_appointment, container, false);

Bundle bundle = getArguments();
userID = bundle.getString("UserId");


ImageView imageViewClose = (ImageView) view.findViewById(R.id.imageClose);
final Fragment f = getActivity().getFragmentManager().findFragmentById(R.id.Linear_layout);


imageViewClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

dismiss();

}
});

button9AM1 = (Button) view.findViewById(R.id.button9AM1);

/* when i click the button9AM1 i want to send the data as as 9AM to main activity */

}}

最佳答案

interface OnclickDataLisnter{
void onClick(String data);
}

 public void showSchedule() {


BottomSheetFrag bottomSheetFragment = new BottomSheetFrag();

bottomSheetFragment.setClickListner(new OnclickDataLisnter(){
@Overrid
public void onClick(String data){
//You listner here your data
}
}
Bundle bundle = new Bundle();
bundle.putString("UserId", Parlour_BeauticianID);

bottomSheetFragment.setArguments(bundle);
bottomSheetFragment.show(getSupportFragmentManager(),
bottomSheetFragment.getTag());

}

     public class BottomSheetFrag extends 
BottomSheetDialogFragment {
public OnclickDataLisnter clickListner;
public static BottomSheetFrag newInstance() {
return new BottomSheetFrag();
}
public void setClickListner(OnclickDataLisnter clickListner){//add this
this.clickListner = clickListner; // add this

}
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {

final View view = inflater.inflate(R.layout.activity_appointment, container, false);

Bundle bundle = getArguments();
userID = bundle.getString("UserId");


ImageView imageViewClose = (ImageView) view.findViewById(R.id.imageClose);
final Fragment f = getActivity().getFragmentManager().findFragmentById(R.id.Linear_layout);


imageViewClose.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {

dismiss();

}
});

button9AM1 = (Button) view.findViewById(R.id.button9AM1);

/* when i click the button9AM1 i want to send the data as as 9AM to main activity */
clickListner.onClick(data); //add this

}}

关于java - 如何从底部 fragment 获取数据到主要 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60427808/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com