gpt4 book ai didi

java - 无需使用按钮即可自动打开 Bottom Sheet

转载 作者:行者123 更新时间:2023-12-02 08:43:01 25 4
gpt4 key购买 nike

我正在做这个项目,我想启动一个应用程序(主 Activity ),并使用 Bottom Sheet 作为说明来了解如何使用某些功能,而主 Activity 在后台打开。

我已经知道如何在 Bottom Sheet 之间转换到另一个 Bottom Sheet ,但我的主要问题是第一个 Bottom Sheet 需要一个按钮本身才能激活,所以我的问题是它可以在应用程序启动时自动完成,而不需要单击 Bottom Sheet 内的按钮后,按钮会消失?

这是我的 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();

}

});

最佳答案

在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();
}

关于java - 无需使用按钮即可自动打开 Bottom Sheet ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61256035/

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