仿写从iOS8开始支持的UIAlertController:BGAAlertController-Android

工作以来公司UI设计师出的Android效果图都是iOS风格的UIAlertView和UIActionSheet,新项目还是用原来那一套,不想重复造轮子,所以仿写了从iOS8开始支持的UIAlertController,统一UIAlertView和UIActionSheet的用法

目前还不支持添加EditText,后续会支持

效果图

Demo

基本使用

1.添加Gradle依赖

dependencies {
    compile 'cn.bingoogolapple:bga-alertcontroller:latestVersion@aar' }

2.在java代码中使用BGAAlertController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public void showAlertView(View v) {
    BGAAlertController alertController = new BGAAlertController(this, "我是标题", "我是很长很长很长很长很长很长很长很长很长很长很长很长的消息", BGAAlertController.AlertControllerStyle.Alert);
    // 不管添加顺序怎样,AlertActionStyle.Cancel始终是在最底部的,AlertActionStyle.Default和AlertActionStyle.Destructive按添加的先后顺序显示
    alertController.addAction(new BGAAlertAction("取消", BGAAlertAction.AlertActionStyle.Cancel, new BGAAlertAction.Delegate() {
        @Override
        public void onClick() {
            showToast("点击了取消");
        }
    }));
    alertController.addAction(new BGAAlertAction("其他1", BGAAlertAction.AlertActionStyle.Default, new BGAAlertAction.Delegate() {
        @Override
        public void onClick() {
            showToast("点击了其他1");
        }
    }));
    alertController.addAction(new BGAAlertAction("其他2", BGAAlertAction.AlertActionStyle.Default, new BGAAlertAction.Delegate() {
        @Override
        public void onClick() {
            showToast("点击了其他2");
        }
    }));
    alertController.addAction(new BGAAlertAction("确定", BGAAlertAction.AlertActionStyle.Destructive, new BGAAlertAction.Delegate() {
        @Override
        public void onClick() {
            showToast("点击了确定");
        }
    }));
    alertController.show();
}
 
public void showActionSheet(View v) {
    BGAAlertController alertController = new BGAAlertController(this, "我是标题", "我是很长很长很长很长很长很长很长很长很长很长很长很长的消息", BGAAlertController.AlertControllerStyle.ActionSheet);
    // 不管添加顺序怎样,AlertActionStyle.Cancel始终是在最底部的,AlertActionStyle.Default和AlertActionStyle.Destructive按添加的先后顺序显示
    alertController.addAction(new BGAAlertAction("取消", BGAAlertAction.AlertActionStyle.Cancel, new BGAAlertAction.Delegate() {
        @Override
        public void onClick() {
            showToast("点击了取消");
        }
    }));
    alertController.addAction(new BGAAlertAction("其他1", BGAAlertAction.AlertActionStyle.Default, new BGAAlertAction.Delegate() {
        @Override
        public void onClick() {
            showToast("点击了其他1");
        }
    }));
    alertController.addAction(new BGAAlertAction("其他2", BGAAlertAction.AlertActionStyle.Default, new BGAAlertAction.Delegate() {
        @Override
        public void onClick() {
            showToast("点击了其他2");
        }
    }));
    alertController.addAction(new BGAAlertAction("确定", BGAAlertAction.AlertActionStyle.Destructive, new BGAAlertAction.Delegate() {
        @Override
        public void onClick() {
            showToast("点击了确定");
        }
    }));
    alertController.show();
}

 

高级用法

1.如果您不满意默认的颜色,在自己项目的colors.xml中定义以下相应地颜色即可(不用全部定义,对不满意的值重新定义即可)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<resources>
    <color name="ac_bg_translucent">#80808080</color>
    <color name="ac_bg_content">#F9F9F9</color>
 
    <color name="ac_item_text_default">#007AFF</color>
    <color name="ac_item_text_destructive">#FF3B30</color>
    <color name="ac_item_bg_pressed">#EBEBEB</color>
 
    <color name="ac_alert_title">#000000</color>
    <color name="ac_alert_message">#000000</color>
 
    <color name="ac_action_sheet_title">#929292</color>
    <color name="ac_action_sheet_message">#929292</color>
</resources>


2.如果您不满意默认的间距和字体大小,在自己项目的colors.xml中定义以下相应的dimen(不用全部定义,对不满意的值重新定义即可)

1
2
3
4
5
6
7
8
9
10
11
<resources>
    <dimen name="ac_radius">10dp</dimen>
    <dimen name="ac_gap">10dp</dimen>
    <dimen name="ac_line_height">1dp</dimen>
    <dimen name="ac_item_text_size">18sp</dimen>
    <dimen name="ac_action_sheet_text_size_title">14sp</dimen>
    <dimen name="ac_action_sheet_text_size_message">14sp</dimen>
 
    <dimen name="ac_alert_text_size_title">18sp</dimen>
    <dimen name="ac_alert_text_size_message">14sp</dimen>
</resources>


https://github.com/bingoogolapple/BGAAlertController-Android

posted @   brave-sailor  阅读(452)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
历史上的今天:
2015-06-26 Android中连接蓝牙设备时遇到createRfcommSocketToServiceRecord的UUID问题和BluetoothSocket的connect失败
2015-06-26 android4.0蓝牙使能的详细解析 (转载)
2015-06-26 蓝牙介绍
2014-06-26 android自定义viewgroup实现等分格子布局
点击右上角即可分享
微信分享提示