Android漂亮的對話框項目sweet-alert-dialog

項目地址: https://github.com/pedant/sweet-alert-dialog

android原生的dialog太生硬了,之前看到了這個效果非常不錯但是沒有用過,今天給別人推薦使用,他遇到了問題,導入後錯誤非常多,也沒有庫工程。於是自己認真看了一下,這是個AndroidStudio的工程,並且裏面還依賴於materialish-progress工程,也是個AS的工程。於是打算弄一個eclipse的版本並且將這兩個工程融合在一起作爲一個庫工程XAlertDialogLibrary。使用時將其作爲庫導入項目中即可。

效果如下

使用起來非常簡單,測試代碼如下:

MainActivity.java

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
public class MainActivity extends Activity implements View.OnClickListener {
 
    private int i = -1;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.basic_test).setOnClickListener(this);
        findViewById(R.id.under_text_test).setOnClickListener(this);
        findViewById(R.id.error_text_test).setOnClickListener(this);
        findViewById(R.id.success_text_test).setOnClickListener(this);
        findViewById(R.id.warning_confirm_test).setOnClickListener(this);
        findViewById(R.id.warning_cancel_test).setOnClickListener(this);
        findViewById(R.id.custom_img_test).setOnClickListener(this);
        findViewById(R.id.progress_dialog).setOnClickListener(this);
    }
 
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.basic_test:
                // default title "Here's a message!"
                SweetAlertDialog sd = new SweetAlertDialog(this);
                sd.setCancelable(true);
                sd.setCanceledOnTouchOutside(true);
                sd.show();
                break;
            case R.id.under_text_test:
                new SweetAlertDialog(this)
                        .setContentText("It's pretty, isn't it?")
                        .show();
                break;
            case R.id.error_text_test:
                new SweetAlertDialog(this, SweetAlertDialog.ERROR_TYPE)
                        .setTitleText("Oops...")
                        .setContentText("Something went wrong!")
                        .show();
                break;
            case R.id.success_text_test:
                new SweetAlertDialog(this, SweetAlertDialog.SUCCESS_TYPE)
                        .setTitleText("Good job!")
                        .setContentText("You clicked the button!")
                        .show();
                break;
            case R.id.warning_confirm_test:
                new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE)
                        .setTitleText("Are you sure?")
                        .setContentText("Won't be able to recover this file!")
                        .setConfirmText("Yes,delete it!")
                        .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                        @Override
                        public void onClick(SweetAlertDialog sDialog) {
                            // reuse previous dialog instance
                            sDialog.setTitleText("Deleted!")
                                    .setContentText("Your imaginary file has been deleted!")
                                    .setConfirmText("OK")
                                    .setConfirmClickListener(null)
                                    .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
                        }
                        })
                        .show();
                break;
            case R.id.warning_cancel_test:
                new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE)
                        .setTitleText("Are you sure?")
                        .setContentText("Won't be able to recover this file!")
                        .setCancelText("No,cancel plx!")
                        .setConfirmText("Yes,delete it!")
                        .showCancelButton(true)
                        .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                            @Override
                            public void onClick(SweetAlertDialog sDialog) {
                                // reuse previous dialog instance, keep widget user state, reset them if you need
                                sDialog.setTitleText("Cancelled!")
                                        .setContentText("Your imaginary file is safe :)")
                                        .setConfirmText("OK")
                                        .showCancelButton(false)
                                        .setCancelClickListener(null)
                                        .setConfirmClickListener(null)
                                        .changeAlertType(SweetAlertDialog.ERROR_TYPE);
 
                                // or you can new a SweetAlertDialog to show
                               /* sDialog.dismiss();
                                new SweetAlertDialog(SampleActivity.this, SweetAlertDialog.ERROR_TYPE)
                                        .setTitleText("Cancelled!")
                                        .setContentText("Your imaginary file is safe :)")
                                        .setConfirmText("OK")
                                        .show();*/
                            }
                        })
                        .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                            @Override
                            public void onClick(SweetAlertDialog sDialog) {
                                sDialog.setTitleText("Deleted!")
                                        .setContentText("Your imaginary file has been deleted!")
                                        .setConfirmText("OK")
                                        .showCancelButton(false)
                                        .setCancelClickListener(null)
                                        .setConfirmClickListener(null)
                                        .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
                            }
                        })
                        .show();
                break;
            case R.id.custom_img_test:
                new SweetAlertDialog(this, SweetAlertDialog.CUSTOM_IMAGE_TYPE)
                        .setTitleText("Sweet!")
                        .setContentText("Here's a custom image.")
                        .setCustomImage(R.drawable.custom_img)
                        .show();
                break;
            case R.id.progress_dialog:
                final SweetAlertDialog pDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE)
                        .setTitleText("Loading");
                pDialog.show();
                pDialog.setCancelable(false);
                new CountDownTimer(800 7800) {
                    public void onTick(long millisUntilFinished) {
                        // you can change the progress bar color by ProgressHelper every 800 millis
                        i++;
                        switch (i){
                            case 0:
                                pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.blue_btn_bg_color));
                                break;
                            case 1:
                                pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.material_deep_teal_50));
                                break;
                            case 2:
                                pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.success_stroke_color));
                                break;
                            case 3:
                                pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.material_deep_teal_20));
                                break;
                            case 4:
                                pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.material_blue_grey_80));
                                break;
                            case 5:
                                pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.warning_stroke_color));
                                break;
                            case 6:
                                pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.success_stroke_color));
                                break;
                        }
                    }
 
                    public void onFinish() {
                        i = -1;
                        pDialog.setTitleText("Success!")
                                .setConfirmText("OK")
                                .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
                    }
                }.start();
                break;
        }
    }
}

activity_main.xml

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#FFF"
      xmlns:android="http://schemas.android.com/apk/res/android">
 
      <RelativeLayout android:layout_width="match_parent"
           android:paddingBottom="10dp"
           android:layout_height="wrap_content">
 
           <ImageView
                 android:id="@+id/logo_img"
                 android:layout_width="180dp"
                 android:layout_height="wrap_content"
                 android:src="@drawable/logo_big"
                 android:layout_marginTop="10dp"
                 android:layout_marginBottom="15dp"
                 android:layout_centerHorizontal="true"
                 android:contentDescription="@string/app_name"/>
 
                 <TextView
                        android:id="@+id/txt_0"
                        android:layout_alignLeft="@id/logo_img"
                        android:layout_below="@id/logo_img"
                        android:layout_marginLeft="15dp"
                        android:text="show material progress"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="14sp"
                        android:textColor="#797979"/>
 
                 <Button
                       android:layout_centerHorizontal="true"
                       android:layout_below="@id/txt_0"
                       android:id="@+id/progress_dialog"
                       style="@style/dialog_blue_button"
                       android:layout_margin="10dp"
                       android:text="Try me!"/>
 
                <TextView
                       android:id="@+id/txt_1"
                       android:layout_alignLeft="@id/logo_img"
                       android:layout_below="@id/progress_dialog"
                       android:layout_marginLeft="15dp"
                       android:text="A basic message"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:textSize="14sp"
                       android:textColor="#797979"/>
 
               <Button
                       android:layout_centerHorizontal="true"
                       android:layout_below="@id/txt_1"
                       android:id="@+id/basic_test"
                       style="@style/dialog_blue_button"
                       android:layout_margin="10dp"
                       android:text="Try me!"/>
 
              <TextView
                      android:id="@+id/txt_2"
                      android:layout_alignLeft="@id/logo_img"
                      android:layout_below="@id/basic_test"
                      android:layout_marginLeft="15dp"
                      android:text="A title with a text under"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:textSize="14sp"
                      android:layout_marginTop="15dp"
                      android:textColor="#797979"/>
 
              <Button
                     android:layout_centerHorizontal="true"
                     android:layout_below="@id/txt_2"
                     android:id="@+id/under_text_test"
                     style="@style/dialog_blue_button"
                     android:layout_margin="10dp"
                     android:text="Try me!"/>
 
             <TextView
                     android:id="@+id/txt_3"
                     android:layout_alignLeft="@id/logo_img"
                     android:layout_below="@id/under_text_test"
                     android:layout_marginLeft="15dp"
                     android:text="show error message"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:textSize="14sp"
                     android:layout_marginTop="15dp"
                     android:textColor="#797979"/>
 
             <Button
                    android:layout_centerHorizontal="true"
                    android:layout_below="@id/txt_3"
                    android:id="@+id/error_text_test"
                    style="@style/dialog_blue_button"
                    android:layout_margin="10dp"
                    android:text="Try me!"/>
 
             <TextView
                    android:id="@+id/txt_4"
                    android:layout_alignLeft="@id/logo_img"
                    android:layout_below="@id/error_text_test"
                    android:layout_marginLeft="15dp"
                    android:text="A success message"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="14sp"
                    android:layout_marginTop="15dp"
                    android:textColor="#797979"/>
 
             <Button
                    android:layout_centerHorizontal="true"
                    android:layout_below="@id/txt_4"
                    android:id="@+id/success_text_test"
                    style="@style/dialog_blue_button"
                    android:layout_margin="10dp"
                    android:text="Try me!"/>
 
             <TextView
                    android:id="@+id/txt_5"
                    android:layout_alignLeft="@id/logo_img"
                    android:layout_below="@id/success_text_test"
                    android:layout_marginLeft="15dp"
                    android:text="A warning message, with a listener bind to the Confirm-button..."
                    android:layout_width="200dp"
                    android:layout_height="wrap_content"
                    android:textSize="14sp"
                    android:layout_marginTop="15dp"
                    android:textColor="#797979"/>
 
            <Button
                   android:layout_centerHorizontal="true"
                   android:layout_below="@id/txt_5"
                   android:id="@+id/warning_confirm_test"
                   style="@style/dialog_blue_button"
                   android:layout_margin="10dp"
                   android:text="Try me!"/>
 
            <TextView
                   android:id="@+id/txt_6"
                   android:layout_alignLeft="@id/logo_img"
                   android:layout_below="@id/warning_confirm_test"
                   android:layout_marginLeft="15dp"
                   android:text="A warning message, with listeners bind to Cancel and Confirm button..."
                   android:layout_width="200dp"
                   android:layout_height="wrap_content"
                   android:textSize="14sp"
                   android:layout_marginTop="15dp"
                   android:textColor="#797979"/>
 
           <Button
                  android:layout_centerHorizontal="true"
                  android:layout_below="@id/txt_6"
                  android:id="@+id/warning_cancel_test"
                  style="@style/dialog_blue_button"
                  android:layout_margin="10dp"
                  android:text="Try me!"/>
 
           <TextView
                  android:id="@+id/txt_7"
                  android:layout_alignLeft="@id/logo_img"
                  android:layout_below="@id/warning_cancel_test"
                  android:layout_marginLeft="15dp"
                  android:text="A message with a custom icon"
                  android:layout_width="200dp"
                  android:layout_height="wrap_content"
                  android:textSize="14sp"
                  android:layout_marginTop="15dp"
                  android:textColor="#797979"/>
 
          <Button
                 android:layout_centerHorizontal="true"
                 android:layout_below="@id/txt_7"
                 android:id="@+id/custom_img_test"
                 style="@style/dialog_blue_button"
                 android:layout_margin="10dp"
                 android:text="Try me!"/>
 
      </RelativeLayout>
</ScrollView>

XAlertDialogLibrary(eclipse):點此下載

發佈了7 篇原創文章 · 獲贊 3 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章