美文网首页Android
自用BottomSheetDialog

自用BottomSheetDialog

作者: 明日未期 | 来源:发表于2020-02-03 11:41 被阅读0次

    设置style

        <style name="BottomSheetStyle" parent="Theme.Design.Light.BottomSheetDialog">
            <item name="android:windowIsFloating">false</item>
            <item name="android:statusBarColor">@android:color/transparent</item>
            <item name="android:windowSoftInputMode">adjustResize</item>
            <item name="bottomSheetStyle">@style/bottomSheetStyleWrapper</item>
        </style>
    
        <style name="bottomSheetStyleWrapper" parent="Widget.Design.BottomSheet.Modal">
            <item name="android:background">@android:color/transparent</item>
        </style>
        
    

    布局xml

    dialog_bsd.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="20dp">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:id="@+id/bsd_title"/>
    
        <EditText
            android:layout_width="match_parent"
            android:ems="10"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:textSize="14sp"
            android:singleLine="true"
            android:textColor="#000000"
            android:id="@+id/bsd_bjk"/>
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:layout_gravity="right"
            android:textColor="#ffffff"
            android:id="@+id/bsd_btn"/>
    
    </LinearLayout>
    

    shape类

    MyShape.java

    import android.graphics.Color;
    import android.graphics.PorterDuff;
    import android.graphics.drawable.Drawable;
    import android.graphics.drawable.GradientDrawable;
    import android.view.View;
    
    public class MyShape
    {
    
        public static void set(Drawable dw, String color){
            try{
                dw.setColorFilter(Color.parseColor(color), PorterDuff.Mode.SRC_ATOP);
            }
            catch(Exception e){}
    
        }
    
        //无边框
        public static void set(View view, int c1, int c2, int c3, int c4, String color){
            try{
                GradientDrawable gd = new GradientDrawable();
                gd.setShape(GradientDrawable.RECTANGLE);
                gd.setCornerRadii(new float[]{c1, c1, c2, c2, c3, c3, c4, c4});
                gd.setColor(Color.parseColor(color));
                gd.setStroke(0, Color.parseColor("#ffffff"));
                view.setBackgroundDrawable(gd);          
            }
            catch(Exception e){}
        }
    
        //有边框
        public static void set(View view, int c1, int c2, int c3, int c4, int stroke, String color, String strokeColor){
            try{
                GradientDrawable gd = new GradientDrawable();
                gd.setShape(GradientDrawable.RECTANGLE);
                gd.setCornerRadii(new float[]{c1, c1, c2, c2, c3, c3, c4, c4});
                gd.setColor(Color.parseColor(color));
                gd.setStroke(stroke, Color.parseColor(strokeColor));
                view.setBackgroundDrawable(gd);
            }
            catch(Exception e){}
        }
    }
    

    dialog类

    MyBottomSheetDialog.java

    import android.content.Context;
    import android.support.design.widget.BottomSheetDialog;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import com.jueze.ibeauty.util.MyShape; //此处导入具体看自己的shape类位置
    
    public class MyBottomSheetDialog {
        
        private Context mContext;
        private BottomSheetDialog bsd;
        private View mView;
        private TextView mTextview;
        private EditText mEditText;
        private Button mButton;
        private String mTitle;
        private String mBtnText;
        private String mHint;
        
        public MyBottomSheetDialog(Context context){
            this.mContext = context;
            init();
        }
        public MyBottomSheetDialog(Context context, String title, String btnText){
            this.mContext = context;
            this.mTitle = title;
            this.mBtnText = btnText;
            init();
        }
    
        public MyBottomSheetDialog(Context context, String title, String btnText, String hint){
            this.mContext = context;
            this.mTitle = title;
            this.mBtnText = btnText;
            this.mHint = hint;
            init();
        }
        
        protected void init(){
            mView = View.inflate(mContext,R.layout.dialog_bsd,null);
            mTextview = mView.findViewById(R.id.bsd_title);
            mEditText = mView.findViewById(R.id.bsd_bjk);
            mButton = mView.findViewById(R.id.bsd_btn);
            
            MyShape.set(mView,15,15,0,0,"#ffffff");
            MyShape.set(mButton.getBackground(),mContext.getString(R.color.colorPrimary));
            
            if(mTitle!=null){
                mTextview.setText(mTitle);
            }
            if(mBtnText!=null){
                mButton.setText(mBtnText);
            }
            if(mHint != null){
                mEditText.setHint(mHint);
            }
            bsd = new BottomSheetDialog(mContext, R.style.BottomSheetStyle);
            bsd.setContentView(mView);
            
        }
        
        public void show(){
            bsd.show();
        }
        
        public void dismiss(){
            bsd.dismiss();
        }
        
        public Button getBtn(){
            return this.mButton;
        }
        
        public String getText(){
            return this.mEditText.getText().toString();
        }
        
        public void setTitle(String title){
            this.mTextview.setText(title);
        }
        
        public void setBtnText(String btnText){
            this.mButton.setText(btnText);
        }
        
        public void setHint(String hint){
            this.mEditText.setHint(hint);
        }
    }
    
    

    使用

    MyBottomSheetDialog bsd = new MyBottomSheetDialog(ctx, "标题", "按钮", "提示");
    bsd.show();
    

    相关文章

      网友评论

        本文标题:自用BottomSheetDialog

        本文链接:https://www.haomeiwen.com/subject/ldhhxhtx.html