无需使用按钮即可自动打开底页

问题描述 投票:0回答:1

我正在做这个项目,我想在底端打开一个带有底页的应用程序(Main活动),以了解如何使用某些功能,而main活动在后台打开。

我已经知道如何在一个底页和另一个底页之间进行转换,但是我的主要问题是,第一个底页需要一个按钮才能自我激活,所以我的问题是,当应用程序启动时,它可以自动完成,而无需单击底部工作表内的按钮后,是否关闭了一个按钮?

这是我的Java代码:

public class MainActivity extends AppCompatActivity {

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


    Button buttonShow = findViewById(R.id.button_start);
    buttonShow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


             final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(
                    MainActivity.this, R.style.BottomSheetDesign
            );
             View bottomSheetView = LayoutInflater.from(getApplicationContext())
                    .inflate(
                            R.layout.layout_bottom_sheet,
                            (LinearLayout)findViewById(R.id.BottomSheetContainer)
                    );
            bottomSheetView.findViewById(R.id.ButtonNext).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    final BottomSheetDialog bottomSheetDialog1 = new BottomSheetDialog(
                            MainActivity.this, R.style.BottomSheetDesign
                    );

                    View bottomSheetView1 = LayoutInflater.from(getApplicationContext())
                            .inflate(
                                    R.layout.layout_bottom_sheet1,
                                    (LinearLayout)findViewById(R.id.BottomSheetContainer1)

                            );

                    bottomSheetView1.findViewById(R.id.ButtonDone).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            bottomSheetDialog1.dismiss();

                        }

                    });

                    bottomSheetDialog.dismiss();
                    bottomSheetDialog1.setContentView(bottomSheetView1);
                    bottomSheetDialog1.show();

                }
            });

            bottomSheetDialog.setContentView(bottomSheetView);
            bottomSheetDialog.show();

        }

    });
java android android-layout bottom-sheet android-bottomsheetdialog
1个回答
0
投票

直接在oncreate方法中调用底层表

尝试此代码:

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

private void openBottomSheet() {

    final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(
            MainActivity.this, R.style.BottomSheetDesign
    );
    View bottomSheetView = LayoutInflater.from(getApplicationContext())
            .inflate(
                    R.layout.layout_bottom_sheet,
                    (LinearLayout) findViewById(R.id.BottomSheetContainer)
            );
    bottomSheetView.findViewById(R.id.ButtonNext).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final BottomSheetDialog bottomSheetDialog1 = new BottomSheetDialog(
                    MainActivity.this, R.style.BottomSheetDesign
            );

            View bottomSheetView1 = LayoutInflater.from(getApplicationContext())
                    .inflate(
                            R.layout.layout_bottom_sheet1,
                            (LinearLayout) findViewById(R.id.BottomSheetContainer1)

                    );

            bottomSheetView1.findViewById(R.id.ButtonDone).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    bottomSheetDialog1.dismiss();

                }

            });

            bottomSheetDialog.dismiss();
            bottomSheetDialog1.setContentView(bottomSheetView1);
            bottomSheetDialog1.show();

        }
    });

    bottomSheetDialog.setContentView(bottomSheetView);
    bottomSheetDialog.show();
}
© www.soinside.com 2019 - 2024. All rights reserved.