Java 不使用按钮自动打开底部板材
我正在做一个项目,我想启动一个应用程序(主活动),在主活动在后台打开的同时,用一个底部的工作表作为指导,以了解如何使用一些函数 我知道如何在一张底页和另一张底页之间转换,但我的主要问题是,第一张底页需要一个按钮来激活,所以我的问题是,当应用程序启动时,它是否可以自动完成,而不需要按钮,然后在点击底页内的按钮后,它会被取消 这是我的Java代码:Java 不使用按钮自动打开底部板材,java,android,android-layout,bottom-sheet,android-bottomsheetdialog,Java,Android,Android Layout,Bottom Sheet,Android Bottomsheetdialog,我正在做一个项目,我想启动一个应用程序(主活动),在主活动在后台打开的同时,用一个底部的工作表作为指导,以了解如何使用一些函数 我知道如何在一张底页和另一张底页之间转换,但我的主要问题是,第一张底页需要一个按钮来激活,所以我的问题是,当应用程序启动时,它是否可以自动完成,而不需要按钮,然后在点击底页内的按钮后,它会被取消 这是我的Java代码: public class MainActivity extends AppCompatActivity { @Override protected v
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();
}
});
在oncreate方法中直接调用bottomsheet 请尝试以下代码:
@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();
}
删除按钮click listener中的代码,将其添加到函数中,并在oncreate中调用该函数