Android中在Activity中添加底部弹窗(BottomSheetDialog)

//先在想要底部弹窗的Activity里面创建BottomSheetDialog类
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(LoginActivity.this);

//获取你想要弹出来的界面
View bottomSheetView = getLayoutInflater().inflate(R.layout.activity_login_dialog,null);

//将弹窗的界面放入到BottomSheetDialog类中
bottomSheetDialog.setContentView(bottomSheetView);

//让弹窗显示
//bottomSheetDialog.show();

//弹窗界面组件的点击事件(我这里是按钮)
bottomSheetView.findViewById(R.id.button3).setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) {
                        
    //Activity之间的跳转
    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
    startActivity(intent);
    }
});
bottomSheetView.findViewById(R.id.button4).setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) {
    //退出弹窗(让弹窗消失)
    bottomSheetDialog.dismiss();
    }
});

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Activity实现一个 BottomSheetDialog 底部弹窗,可以按照以下步骤进行: 1. 创建一个继承自 BottomSheetDialogFragment 的 Fragment 类,用于显示底部弹窗内容。在该 Fragment 重写 onCreateDialog 方法,返回一个 BottomSheetDialog 对象。 例如: ``` public class MyBottomSheetDialogFragment extends BottomSheetDialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { BottomSheetDialog dialog = new BottomSheetDialog(getActivity()); dialog.setContentView(R.layout.bottom_sheet_dialog); return dialog; } } ``` 2. 在 Activity 创建一个实例变量,用于保存 BottomSheetDialogFragment 的实例。 例如: ``` private MyBottomSheetDialogFragment mBottomSheetDialogFragment; ``` 3. 在需要显示底部弹窗的地方,调用以下代码来显示 BottomSheetDialogFragment: ``` mBottomSheetDialogFragment = new MyBottomSheetDialogFragment(); mBottomSheetDialogFragment.show(getSupportFragmentManager(), "MyBottomSheetDialogFragment"); ``` 4. 要在 Activity 获取 BottomSheetDialogFragment 所在的实例,可以在 onCreateDialog 方法BottomSheetDialog 的实例传递给 Fragment,并在 Fragment 保存该实例。 例如,在 MyBottomSheetDialogFragment 类添加以下代码: ``` private BottomSheetDialog mBottomSheetDialog; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mBottomSheetDialog = (BottomSheetDialog) getDialog(); } public BottomSheetDialog getBottomSheetDialog() { return mBottomSheetDialog; } ``` 然后,在 Activity 可以通过以下代码获取 BottomSheetDialogFragment 所在的 Fragment 实例: ``` MyBottomSheetDialogFragment fragment = (MyBottomSheetDialogFragment) getSupportFragmentManager().findFragmentByTag("MyBottomSheetDialogFragment"); BottomSheetDialog dialog = fragment.getBottomSheetDialog(); ``` 这样就可以在 Activity 获取 BottomSheetDialog 所在的 Fragment 实例了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值