仿寫從iOS8開始支持的UIAlertController:BGAAlertController-Android
工作以來公司UI設計師出的Android效果圖都是iOS風格的UIAlertView和UIActionSheet,新項目還是用原來那一套,不想重復造輪子,所以仿寫了從iOS8開始支持的UIAlertController,統一UIAlertView和UIActionSheet的用法
目前還不支持添加EditText,后續會支持
效果圖
基本使用
1.添加Gradle依賴
dependencies {
compile 'cn.bingoogolapple:bga-alertcontroller:latestVersion@aar' }2.在java代碼中使用BGAAlertController
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();
}</pre>
高級用法
1.如果您不滿意默認的顏色,在自己項目的colors.xml中定義以下相應地顏色即可(不用全部定義,對不滿意的值重新定義即可)
</blockquote>
<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></pre>
2.如果您不滿意默認的間距和字體大小,在自己項目的colors.xml中定義以下相應的dimen(不用全部定義,對不滿意的值重新定義即可)
<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></pre>
https://github.com/bingoogolapple/BGAAlertController-Android