【问题标题】:Dismiss a bottom sheet dialogue in android from its own Custom Activity从它自己的自定义活动中关闭android中的底部对话框
【发布时间】:2017-12-02 10:44:26
【问题描述】:

在 Chintan Khetiya:How to create a Custom Dialog box in android? 的这个答案的帮助下,我创建了一个自定义底页 android 对话。

我想从 BottomDialogue 自己的 Activity 中定义的按钮关闭对话。而不是来自 Calling 活动。

这是我在调用活动中的代码,在该活动中我通过单击按钮创建了我的自定义 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>**
        }
    });

}

【问题讨论】:

    标签: android android-layout android-fragments android-dialog


    【解决方案1】:

    试试这个你需要像下面的代码调用 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();
            }
        });
    
    }
    

    【讨论】:

    • @shubhamsaini 乐于提供帮助
    【解决方案2】:

    尝试以下代码

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-14
      • 1970-01-01
      • 2019-01-02
      • 2014-12-19
      • 2016-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多