gpt4 book ai didi

android - 如何禁用第二个 Bottom Sheet 对话框上的后退按钮?

转载 作者:行者123 更新时间:2023-12-04 23:52:02 35 4
gpt4 key购买 nike

我正在创建一个 MainActivity。此 Activity 有一个打开 fragment 的按钮, fragment 有一个打开 Bottom Sheet 对话框的按钮。

当我在 Fragment 上时,我可以按 Back 按钮返回 MainActivity。但是,当我已经打开底部对话框时,我想禁用后退按钮,以便用户在显示底部对话框时无法按下后退按钮。那么我该怎么做呢?谢谢你。

主要 Activity

public class MainActivity extends SdwBaseActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void startAction(View view){
LoadCashTutorialScreen loadCashTutorialScreen = LoadCashTutorialScreen.newInstance();
getSupportFragmentManager().beginTransaction().replace(R.id.container, loadCashTutorialScreen, "LoadCashTutorialScreen").addToBackStack("LoadCashTutorialScreen").commit();
}

@Override
public void onBackPressed(){
super.onBackPressed()
//do something so that the back button is disable when the Bottom Dialog is showing
}
}

分段:
public class LoadCashTutorialScreen extends Fragment{
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = initView(inflater, container, R.layout.load_cash_tutorial_screen);
Button startDialog = view.findViewById(R.id.dialogButton);
startDialog.setOnClickListener(view1 -> {
MyBottomDialog dialog = new MyBottomDialog();
dialog.show(activity.getSupportFragmentManager(), "BottomDialog");
});

return view;
}
}

对话:
public class MyBottomDialog extends BottomSheetDialogFragment {
@Override
public void setupDialog(@NonNull Dialog dialog, int style) {
super.setupDialog(dialog, style);
View contentView = View.inflate(getContext(), R.layout.load_cash_bottom_dialog, null);
dialog.setContentView(contentView);
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(false);
}
}

注意:我以前在 onBackPressed() 上写了一些方法来防止对话框消失,但是,当按下 Back 时对话框总是消失。调试后,我意识到显示底部对话框时未达到 onBackPressed() 。我不知道为什么。

最佳答案

由于您的 Bottom Sheet 是 Fragment ,您必须以单独的方式收听。用这个:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
// ...
setupBackPressListener()
}

private fun setupBackPressListener() {
this.view?.isFocusableInTouchMode = true
this.view?.requestFocus()
this.view?.setOnKeyListener { _, keyCode, _ ->
if (keyCode == KeyEvent.KEYCODE_BACK) {
// Do what you want to do on back press
true
} else
false
}
}

关于android - 如何禁用第二个 Bottom Sheet 对话框上的后退按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60067397/

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