android - 从 Bottom Sheet 对话框 fragment 中获取值(value)

标签 android fragment bottom-sheet dialogfragment

我从 fragment A 开始 bottomSheetDialogFragment。 我想从 bottomSheetDialogFragment 中选择日期,然后将其设置在 fragment A 中。

选择日期已经完成了,我只想在 fragment A中获取它以在某些字段中进行设置。

如何获取值? 有什么建议吗?

最佳答案

像这样创建一个接口(interface)类

public interface CustomInterface {

    public void callbackMethod(String date);
}

在您的ActivityFragment 中实现此接口(interface)。并创建此接口(interface)的对象。

private CustomInterface callback;

onCreateonCreateView中初始化

callback=this;

现在,当您调用它时,将此回调传递到您的 BottomSheetDialogFragment 构造函数中。

yourBottomSheetObject = new YourBottomSheet(callback);
yourBottomSheetObject.show(getSupportFragmentManager()," string");

现在在 BottomSheetFragment 的构造函数中

私有(private)自定义接口(interface)回调;

public SelectStartTimeSheet(CustomInterface callback){

this.callback=callback;

}

最后使用这个回调对象来设置你的日期

callback.callbackMethod("your date");

您将在您的 fragment 或您在 callbackMethod 函数中的 Activity 中收到此日期。

关于android - 从 Bottom Sheet 对话框 fragment 中获取值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49794739/

相关文章:

Android:Google Fit 找不到 DataType.TYPE_LOCATION_SAMPLE 的数据源

android - fragment 是否意味着我应该编写单 Activity 应用程序?

android - 在外部点击时隐藏 Fragment 中的 Android 软键盘

android - BottomSheetBehavior : The view is not associated with BottomSheetBehavior

android - colorButtonNormal没有效果?

java - 比较 Hashmap 字符串值和重复项的重复键

android - 全屏 fragment

android - 无法解析 fragment 中的 findViewById

android - 高程不适用于协调器布局中的 BottomSheet

android - Jetpack Compose BottomSheetScaffold sheetGestures 已禁用,但当子组件可滚动时手势仍然有效