自定义android Dialog

1.自定义Dialog:

 1 import android.app.AlertDialog;

 2 import android.app.Dialog;

 3 import android.content.Context;

 4 import android.view.KeyEvent;

 5 import android.view.Window;

 6 

 7 public class HintDialog {

 8 

 9     Dialog mDialog = null;

10     private Context mContext;

11     private IHintDialog mDialogInstance = null;

12     

13     /**

14      * 构造函数

15      * @param context

16      */

17     public HintDialog(Context context) {

18         this.mContext =context;

19         mDialog = new AlertDialog(mContext){

20             public boolean onKeyDown(int keyCode, KeyEvent event) {

21                 if(keyCode == KeyEvent.KEYCODE_BACK && mDialogInstance !=null){

22                     mDialogInstance.onKeyDown(keyCode, event);

23                     return true;

24                 }

25                 return super.onKeyDown(keyCode, event);

26             };

27         };

28         mDialog.setCancelable(false);

29         mDialog.setCanceledOnTouchOutside(false);

30     }

31     

32     

33     /**

34      * 构造函数

35      * @param iLayoutResId 此Dialog采用的自定义布局文件

36      *  @param interfaceInstance 此Dialog需要实现的一些接口回调事件

37      */

38     public void showDialog(int iLayoutResId,IHintDialog interfaceInstance){

39         if(mDialog == null || iLayoutResId == 0){

40             return;

41         }

42         mDialogInstance = interfaceInstance;

43         mDialog.show();

44         mDialog.setContentView(iLayoutResId);

45         Window window = mDialog.getWindow();

46         if(mDialogInstance!=null){

47             mDialogInstance.showWindowDetail(window);

48         }

49     }

50     

51     /**

52      * 销毁Dialog

53      */

54     public void dissmissDialog(){

55         if(mDialog!=null && mDialog.isShowing()){

56             mDialog.dismiss();

57         }

58     }

59     

60     /**

61      * 判断Dialog是否显示

62      * @return

63      */

64     public boolean isShowing(){

65         if(mDialog!=null && mDialog.isShowing()){

66             return mDialog.isShowing();

67         }

68         return false;

69     }

70     

71     

72     /**

73      * 事件回调接口

74      *

75      */

76     public interface IHintDialog{

77         public void onKeyDown(int keyCode,KeyEvent event);

78         public void showWindowDetail(Window window);

79     }

80 }
View Code

2.采用系统Dialog,各种样式:

  1 import android.app.AlertDialog;

  2 import android.app.Dialog;

  3 import android.content.Context;

  4 import android.content.DialogInterface.OnClickListener;

  5 import android.content.DialogInterface.OnMultiChoiceClickListener;

  6 import android.view.View;

  7 

  8 /**

  9  * 对话框封装类

 10  * 

 11  * @author Jack_Lu

 12  * 

 13  */

 14 public class DialogTool {

 15 

 16     public static final int NO_ICON = -1; // 无图标

 17 

 18     /**

 19      * 创建消息对话框

 20      * 

 21      * @param context

 22      *            上下文 必填

 23      * @param iconId

 24      *            图标,如:R.drawable.icon 或 DialogTool.NO_ICON 必填

 25      * @param title

 26      *            标题 必填

 27      * @param message

 28      *            显示内容 必填

 29      * @param btnName

 30      *            按钮名称 必填

 31      * @param listener

 32      *            监听器,需实现android.content.DialogInterface.OnClickListener接口 必填

 33      * @return

 34      */

 35     public static Dialog createMessageDialog(Context context, String title,

 36             String message, String btnName, OnClickListener listener, int iconId) {

 37         Dialog dialog = null;

 38         AlertDialog.Builder builder = new AlertDialog.Builder(context);

 39 

 40         if (iconId != NO_ICON) {

 41             // 设置对话框图标

 42             builder.setIcon(iconId);

 43         }

 44         // 设置对话框标题

 45         builder.setTitle(title);

 46         // 设置对话框消息

 47         builder.setMessage(message);

 48         // 设置按钮

 49         builder.setPositiveButton(btnName, listener);

 50         // 创建一个消息对话框

 51         dialog = builder.create();

 52 

 53         return dialog;

 54     }

 55 

 56     /**

 57      * 创建警示(确认、取消)对话框

 58      * 

 59      * @param context

 60      *            上下文 必填

 61      * @param iconId

 62      *            图标,如:R.drawable.icon 或 DialogTool.NO_ICON 必填

 63      * @param title

 64      *            标题 必填

 65      * @param message

 66      *            显示内容 必填

 67      * @param positiveBtnName

 68      *            确定按钮名称 必填

 69      * @param negativeBtnName

 70      *            取消按钮名称 必填

 71      * @param positiveBtnListener

 72      *            监听器,需实现android.content.DialogInterface.OnClickListener接口 必填

 73      * @param negativeBtnListener

 74      *            监听器,需实现android.content.DialogInterface.OnClickListener接口 必填

 75      * @return

 76      */

 77     public static Dialog createConfirmDialog(Context context, String title,

 78             String message, String positiveBtnName, String negativeBtnName,

 79             OnClickListener positiveBtnListener,

 80             OnClickListener negativeBtnListener, int iconId) {

 81         Dialog dialog = null;

 82         AlertDialog.Builder builder = new AlertDialog.Builder(context);

 83 

 84         if (iconId != NO_ICON) {

 85             // 设置对话框图标

 86             builder.setIcon(iconId);

 87         }

 88         // 设置对话框标题

 89         builder.setTitle(title);

 90         // 设置对话框消息

 91         builder.setMessage(message);

 92         // 设置确定按钮

 93         builder.setPositiveButton(positiveBtnName, positiveBtnListener);

 94         // 设置取消按钮

 95         builder.setNegativeButton(negativeBtnName, negativeBtnListener);

 96         // 创建一个消息对话框

 97         dialog = builder.create();

 98 

 99         return dialog;

100     }

101 

102     /**

103      * 创建单选对话框

104      * 

105      * @param context

106      *            上下文 必填

107      * @param iconId

108      *            图标,如:R.drawable.icon 或 DialogTool.NO_ICON 必填

109      * @param title

110      *            标题 必填

111      * @param itemsString

112      *            选择项 必填

113      * @param positiveBtnName

114      *            确定按钮名称 必填

115      * @param negativeBtnName

116      *            取消按钮名称 必填

117      * @param positiveBtnListener

118      *            监听器,需实现android.content.DialogInterface.OnClickListener接口 必填

119      * @param negativeBtnListener

120      *            监听器,需实现android.content.DialogInterface.OnClickListener接口 必填

121      * @param itemClickListener

122      *            监听器,需实现android.content.DialogInterface.OnClickListener接口 必填

123      * @return

124      */

125     public static Dialog createSingleChoiceDialog(Context context,

126             String title, String[] itemsString, String positiveBtnName,

127             String negativeBtnName, OnClickListener positiveBtnListener,

128             OnClickListener negativeBtnListener,

129             OnClickListener itemClickListener, int iconId) {

130         Dialog dialog = null;

131         AlertDialog.Builder builder = new AlertDialog.Builder(context);

132 

133         if (iconId != NO_ICON) {

134             // 设置对话框图标

135             builder.setIcon(iconId);

136         }

137         // 设置对话框标题

138         builder.setTitle(title);

139         // 设置单选选项, 参数0: 默认第一个单选按钮被选中

140         builder.setSingleChoiceItems(itemsString, 0, itemClickListener);

141         // 设置确定按钮

142         builder.setPositiveButton(positiveBtnName, positiveBtnListener);

143         // 设置确定按钮

144         builder.setNegativeButton(negativeBtnName, negativeBtnListener);

145         // 创建一个消息对话框

146         dialog = builder.create();

147 

148         return dialog;

149     }

150 

151     /**

152      * 创建复选对话框

153      * 

154      * @param context

155      *            上下文 必填

156      * @param iconId

157      *            图标,如:R.drawable.icon 或 DialogTool.NO_ICON 必填

158      * @param title

159      *            标题 必填

160      * @param itemsString

161      *            选择项 必填

162      * @param positiveBtnName

163      *            确定按钮名称 必填

164      * @param negativeBtnName

165      *            取消按钮名称 必填

166      * @param positiveBtnListener

167      *            监听器,需实现android.content.DialogInterface.OnClickListener接口 必填

168      * @param negativeBtnListener

169      *            监听器,需实现android.content.DialogInterface.OnClickListener接口 必填

170      * @param itemClickListener

171      *            监听器,需实现android.content.DialogInterface.

172      *            OnMultiChoiceClickListener;接口 必填

173      * @return

174      */

175     public static Dialog createMultiChoiceDialog(Context context, String title,

176             String[] itemsString, String positiveBtnName,

177             String negativeBtnName, OnClickListener positiveBtnListener,

178             OnClickListener negativeBtnListener,

179             OnMultiChoiceClickListener itemClickListener, int iconId) {

180         Dialog dialog = null;

181         AlertDialog.Builder builder = new AlertDialog.Builder(context);

182 

183         if (iconId != NO_ICON) {

184             // 设置对话框图标

185             builder.setIcon(iconId);

186         }

187         // 设置对话框标题

188         builder.setTitle(title);

189         // 设置选项

190         builder.setMultiChoiceItems(itemsString, null, itemClickListener);

191         // 设置确定按钮

192         builder.setPositiveButton(positiveBtnName, positiveBtnListener);

193         // 设置确定按钮

194         builder.setNegativeButton(negativeBtnName, negativeBtnListener);

195         // 创建一个消息对话框

196         dialog = builder.create();

197 

198         return dialog;

199     }

200 

201     /**

202      * 创建列表对话框

203      * 

204      * @param context

205      *            上下文 必填

206      * @param iconId

207      *            图标,如:R.drawable.icon 或 DialogTool.NO_ICON 必填

208      * @param title

209      *            标题 必填

210      * @param itemsString

211      *            列表项 必填

212      * @param negativeBtnName

213      *            取消按钮名称 必填

214      * @param negativeBtnListener

215      *            监听器,需实现android.content.DialogInterface.OnClickListener接口 必填

216      * @return

217      */

218     public static Dialog createListDialog(Context context, String title,

219             String[] itemsString, String negativeBtnName,

220             OnClickListener negativeBtnListener,

221             OnClickListener itemClickListener, int iconId) {

222         Dialog dialog = null;

223         AlertDialog.Builder builder = new AlertDialog.Builder(context);

224 

225         if (iconId != NO_ICON) {

226             // 设置对话框图标

227             builder.setIcon(iconId);

228         }

229         // 设置对话框标题

230         builder.setTitle(title);

231         // 设置列表选项

232         builder.setItems(itemsString, itemClickListener);

233         // 设置确定按钮

234         builder.setNegativeButton(negativeBtnName, negativeBtnListener);

235         // 创建一个消息对话框

236         dialog = builder.create();

237 

238         return dialog;

239     }

240 

241     /**

242      * 创建自定义(含确认、取消)对话框

243      * 

244      * @param context

245      *            上下文 必填

246      * @param iconId

247      *            图标,如:R.drawable.icon 或 DialogTool.NO_ICON 必填

248      * @param title

249      *            标题 必填

250      * @param positiveBtnName

251      *            确定按钮名称 必填

252      * @param negativeBtnName

253      *            取消按钮名称 必填

254      * @param positiveBtnListener

255      *            监听器,需实现android.content.DialogInterface.OnClickListener接口 必填

256      * @param negativeBtnListener

257      *            监听器,需实现android.content.DialogInterface.OnClickListener接口 必填

258      * @param view

259      *            对话框中自定义视图 必填

260      * @return

261      */

262     public static Dialog createRandomDialog(Context context, String title,

263             String positiveBtnName, String negativeBtnName,

264             OnClickListener positiveBtnListener,

265             OnClickListener negativeBtnListener, View view, int iconId) {

266         Dialog dialog = null;

267         AlertDialog.Builder builder = new AlertDialog.Builder(context);

268 

269         if (iconId != NO_ICON) {

270             // 设置对话框图标

271             builder.setIcon(iconId);

272         }

273         // 设置对话框标题

274         builder.setTitle(title);

275         builder.setView(view);

276         // 设置确定按钮

277         builder.setPositiveButton(positiveBtnName, positiveBtnListener);

278         // 设置确定按钮

279         builder.setNegativeButton(negativeBtnName, negativeBtnListener);

280         // 创建一个消息对话框

281         dialog = builder.create();

282 

283         return dialog;

284     }

285 

286 }
View Code

 

你可能感兴趣的:(android)