我在Chintan Khetiya的帮助下创建了一个自定义的底部Android对话:如何在Android中创建自定义对话框?
我想从底部模拟自己的活动中定义的按钮中解散对话。
。这是我的呼叫活动中的代码,通过单击按钮,我创建了自定义bottomSheet_liab实例:
openBottomDialogeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
**//Creating the BottomDialogue Instance**`Bottomsheet_liab dialog;
dialog=new Bottomsheet_liab(getActivity());
dialog.getWindow().setBackgroundDrawable(newColorDrawable(Color.TRANSPARENT));`
}
});
这是我的对话活动中的代码:
public class Bottomsheet_liab extends BottomSheetDialog{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cashflow_bottomsheet);
Button btn=(Button)findViewByID(R.id.btnx);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
**//I want to dismiss this BottomSheetDialogue from here.How can I do this>**
}
});
}
尝试一下您需要调用 dismiss();
类似于下面的代码
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});
示例代码
public class Bottomsheet_liab extends BottomSheetDialog{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cashflow_bottomsheet);
Button btn=(Button)findViewByID(R.id.btnx);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});
}
尝试以下代码
private Button startbtn, okaybtn, cancelbtn;
BottomSheetDialog mBottomSheetDialog;
View sheetView;
mBottomSheetDialog = new BottomSheetDialog(HomeActivity.this);
sheetView = getLayoutInflater().inflate(R.layout.botomdialouge,null);
mBottomSheetDialog.setContentView(sheetView);
okaybtn = sheetView.findViewById(R.id.okaybtn);
cancelbtn = sheetView.findViewById(R.id.cancelbtn);
okaybtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
cancelbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mBottomSheetDialog.dismiss();
}
});
布局代码如下
<LinearLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:id="@+id/bottomsheet"
android:clipToPadding="true"
android:background="@color/colorwhite"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<ImageView
android:layout_gravity="center"
android:src="@drawable/logowhite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:layout_gravity="center"
android:textSize="@dimen/textsizenormal"
android:textColor="@color/colortheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/exit"
android:padding="16dp"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/cancelbtn"
android:text="Cancel"
android:textColor="@color/colorwhite"
android:layout_margin="10dp"
android:textSize="@dimen/textsizenormal"
android:background="@drawable/buttonclick"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/okaybtn"
android:text="Okay"
android:textColor="@color/colorwhite"
android:layout_margin="10dp"
android:textSize="@dimen/textsizenormal"
android:background="@drawable/buttonclick"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>