当用户点击back时, BottomSheetBehaviour被取消



我在我的布局中有一个BottomSheetBehaviour,它工作得非常好。问题是,每当用户点击后退按钮时,它就会变得不可见(或消失)。我怎样才能防止它成为不可解雇的?

为你的页底创建一个布局:

<androidx.core.widget.NestedScrollView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="16dp"
app:behavior_peekHeight="260dp"//change according to your need
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
//your design here       
</LinearLayout>
</androidx.core.widget.NestedScrollView>

在你的主布局文件中包含这个布局

<include layout="@layout/layout_name" />

现在你可以在activity中使用bottom sheet

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

View bottomSheet = findViewById(R.id.bottomSheet);
//add behaviour as
BottomSheetBehavior<View> bottomSheetBehavior =BottomSheetBehavior.from(bottomSheet);
//access your views inside bottomSheet as

TextView textView = bottomSheet.findViewById(R.id.textViewId);

}

直接覆盖onCancel()或onDismiss()。

@Override
public void onCancel(DialogInterface dialog) {
//   super.onCancel(dialog); // comment this out
Toast.makeText(MainApp.get(), "CANCEL REQUESTED", Toast.LENGTH_SHORT).show();
}

或者在创建对话框

时在onCreateDialog函数中重写onBackPressed
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new Dialog(getActivity(), getTheme()){
@Override
public void onBackPressed() {
Toast.makeText(MainApp.get(), "CANCEL REQUESTED", Toast.LENGTH_SHORT).show();
}
};
}

相关内容

  • 没有找到相关文章

最新更新