Android 安卓自定义Dialog工具类封装与使用


Android 安卓Dialog工具类封装与使用

安卓自定义Dialog,分别用了Kotlin和Java两种语言列出!布局可以自定义!


效果图

布局是自定义的

在这里插入图片描述


配置

style.xml中配置

<style name="BoxDialog" parent="@android:style/Theme.Holo.Dialog">
    <!-- 是否有边框 -->
    <item name="android:windowFrame">@null</item>
    <!--是否在悬浮Activity之上  -->
    <item name="android:windowIsFloating">true</item>
    <!-- 标题 -->
    <item name="android:windowNoTitle">true</item>
    <!--阴影  -->
    <item name="android:windowIsTranslucent">true</item>
    <!--背景透明-->
    <item name="android:windowBackground">@android:color/transparent</item>
    <!--可加入动画-->
</style>

Java

/**
 * @author ThirdGoddess
 * @email ofmyhub@gmail.com
 * @Github https://github.com/ThirdGoddess
 * @date :2019-12-29 01:24
 */
public class BoxDialog extends Dialog {

    //Dialog View
    private View view;

    //Dialog弹出位置
    private LocationView locationView = LocationView.CENTER;

    /**
     * @param context 上下文
     * @param view    Dialog View
     */
    public BoxDialog(Context context, View view) {
        super(context, R.style.BoxDialog);
        this.view = view;
    }

    /**
     * @param context      上下文
     * @param view         Dialog View
     * @param locationView Dialog弹出位置
     */
    public BoxDialog(Context context, View view, LocationView locationView) {
        super(context, R.style.BoxDialog);
        this.view = view;
        this.locationView = locationView;
    }


    @SuppressLint("RtlHardcoded")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (null != view) {
            setContentView(view);
            setCancelable(isCancelable);//点击外部是否可以关闭Dialog
            setCanceledOnTouchOutside(isCanceledOnTouchOutside);//返回键是否可以关闭Dialog
            Window window = this.getWindow();
            assert window != null;
            switch (locationView) {
                case TOP:
                    window.setGravity(Gravity.TOP);
                    break;
                case BOTTOM:
                    window.setGravity(Gravity.BOTTOM);
                    break;
                case CENTER:
                    window.setGravity(Gravity.CENTER);
                    break;
            }
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = WindowManager.LayoutParams.MATCH_PARENT;
            params.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(params);
        }
    }

    public enum LocationView {
        CENTER, TOP, BOTTOM
    }
}


Kotlin

/**
 * @author ThirdGoddess
 * @email ofmyhub@gmail.com
 * @Github https://github.com/ThirdGoddess
 * @date :2019-12-29 01:24
 */
class BoxDialog : Dialog {

    //Dialog View
    private var view: View?

    //Dialog弹出位置
    private var locationView = LocationView.CENTER

    /**
     * @param context 上下文
     * @param view    Dialog View
     */
    constructor(context: Context?, view: View?) : super(context!!, R.style.MyDialog) {
        this.view = view
    }

    /**
     * @param context      上下文
     * @param view         Dialog View
     * @param locationView Dialog弹出位置
     */
    constructor(context: Context?, view: View?, locationView: LocationView) : super(context!!, R.style.MyDialog) {
        this.view = view
        this.locationView = locationView
    }

    @SuppressLint("RtlHardcoded")
    override fun onCreate(savedInstanceState: Bundle) {
        super.onCreate(savedInstanceState)
        if (null != view) {
            setContentView(view!!)
            setCancelable(isCancelable) //点击外部是否可以关闭Dialog
            setCanceledOnTouchOutside(isCanceledOnTouchOutside) //返回键是否可以关闭Dialog
            val window = this.window!!
            when (locationView) {
                LocationView.TOP -> window.setGravity(Gravity.TOP)
                LocationView.BOTTOM -> window.setGravity(Gravity.BOTTOM)
                LocationView.CENTER -> window.setGravity(Gravity.CENTER)
            }
            val params = window.attributes
            params.width = WindowManager.LayoutParams.MATCH_PARENT
            params.height = WindowManager.LayoutParams.WRAP_CONTENT
            window.attributes = params
        }
    }

    enum class LocationView {
        CENTER, TOP, BOTTOM
    }
}

使用方法

Java 和 Kotlin 的方法


Java

public class MainActivity extends AppCompatActivity {
    
    private BoxDialog boxDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        Button dialogButton = findViewById(R.id.dialog);
        dialogButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_view2, null, false);
                //在这里可以给布局中的按钮加事件,boxDialog.dismiss()可以关闭dialog

                boxDialog = new BoxDialog(MainActivity.this, inflate, BoxDialog.LocationView.CENTER);
                boxDialog.setCancelable(false);//是否可以点击DialogView外关闭Dialog
                boxDialog.setCanceledOnTouchOutside(false);//是否可以按返回按钮关闭Dialog
                boxDialog.show();
            }
        });
    }

}


Kotlin

class MainActivity : AppCompatActivity() {
    private var boxDialog: BoxDialog? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
    }

    private fun initView() {
        val dialogButton = findViewById<Button>(R.id.dialog)
        dialogButton.setOnClickListener {
            val inflate = LayoutInflater.from(this).inflate(R.layout.dialog_view2, null, false)
            //可以给布局中的按钮加事件,boxDialog.dismiss();关闭dialog
            
            boxDialog = BoxDialog(this, inflate, BoxDialog.LocationView.CENTER)
            boxDialog!!.setCancelable(false) //是否可以点击DialogView外关闭Dialog
            boxDialog!!.setCanceledOnTouchOutside(false) //是否可以按返回按钮关闭Dialog
            boxDialog!!.show()
        }
    }
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 博客之星2020 设计师:CY__0809 返回首页
实付 19.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值