我正在使用a DialogFragment,虽然我已成功设置图像以在关闭时关闭(即关闭)对话框,但当用户点击对话框外的任何地方时,我很难找到解除对话框的方法,就像它一样正常的对话.我以为会有某种
dialogFragment.setCanceledOnTouchOutside(true);
Run Code Online (Sandbox Code Playgroud)
打电话,但我没有在文档中看到.
这有可能DialogFragment吗?或者我在错误的地方寻找?我尝试拦截"父母"活动中的触摸事件,但除了没有得到任何触摸事件外,它对我来说似乎不对.
我如何听取最后解雇BottomSheetDialogFragment?我想在最终解雇时保存用户更改...
我试过以下:
方法1
如果通过向下滑动对话框解除对话(不在背面按压或外面触摸),则仅触发此操作
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
Dialog d = super.onCreateDialog(savedInstanceState);
d.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
BottomSheetDialog d = (BottomSheetDialog) dialog;
FrameLayout bottomSheet = (FrameLayout) dialog.findViewById(android.support.design.R.id.design_bottom_sheet);
BottomSheetBehavior behaviour = BottomSheetBehavior.from(bottomSheet);
behaviour.setState(BottomSheetBehavior.STATE_EXPANDED);
behaviour.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_HIDDEN)
{
// Bottom Sheet was dismissed by user! But this is only fired, if dialog is swiped down! Not if touch outside dismissed the …Run Code Online (Sandbox Code Playgroud) android dialog android-dialogfragment dialogfragment bottom-sheet
我在我的活动中使用BottomSheetDialogFragment,对话框在纵向模式下显示全高,但在切换到横向模式时不显示.
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomBottomSheetDialog customBottomSheetDialog = new CustomBottomSheetDialog();
customBottomSheetDialog.show(getSupportFragmentManager(),customBottomSheetDialog.getTag());
}
}
Run Code Online (Sandbox Code Playgroud)
CustomBottomSheetDialog
public class CustomBottomSheetDialog extends BottomSheetDialogFragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return View.inflate(getContext(), R.layout.view_config, null);
}
}
Run Code Online (Sandbox Code Playgroud)
CustomBottomSheetDialog布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#fdf107"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="196dp"
android:gravity="center"
android:textColor="@color/colorAccent"
android:text="BottomSheetDialogFragment"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
在横向模式下,我必须拖动BottomSheetDialogFragment才能看到整个内容.