android自定义对话框

网友投稿 238 2022-11-21

android自定义对话框

自定义 对话框类: public class AlertDialogUI { Context context; android.app.AlertDialog ad; TextView titleView; TextView messageView; TextView btn_sure, btn_cancle; public AlertDialogUI(Context context) { // TODO Auto-generated constructor stub try { this.context = context; LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); if (ad == null) { ad = new android.app.AlertDialog.Builder(context).create(); } if (!ad.isShowing()) { ad.show(); } // 关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局 // Window window = ad.getWindow(); // window.setContentView(R.layout.alertdialog); View layout = inflater.inflate(R.layout.alertdialog, null); Window dialogWindow = ad.getWindow(); DisplayMetrics metrics = new DisplayMetrics(); ((Activity) context).getWindowManager().getDefaultDisplay() .getMetrics(metrics); int width = (int) ((metrics.widthPixels) * 0.8); WindowManager.LayoutParams layoutParams = ad.getWindow() .getAttributes(); layoutParams.width = width; dialogWindow.setAttributes(layoutParams); ad.setContentView(layout, new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); titleView = (TextView) layout.findViewById(R.id.title); messageView = (TextView) layout.findViewById(R.id.message); btn_sure = (TextView) layout.findViewById(R.id.btn_sure); btn_cancle = (TextView) layout.findViewById(R.id.btn_cancle); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void showDialog() { if (null != ad && !ad.isShowing()) { ad.show(); } } public void setDisMissListener() { } public void setTitle(int resId) { titleView.setVisibility(View.VISIBLE); titleView.setText(resId); } public void setTitle(String title) { titleView.setVisibility(View.VISIBLE); titleView.setText(title); } public void setMessage(int resId) { messageView.setText(resId); } public void setMessage(String message) { messageView.setText(message); } public void setCanceledOnTouchOutside(boolean flag) { ad.setCanceledOnTouchOutside(flag); } public void setCancleAble(boolean flag) { ad.setCancelable(flag); } /** * 设置按钮 * * @param text * @param listener */ public void setPositiveButton(String text, final View.OnClickListener listener) { btn_sure.setText(text); btn_sure.setOnClickListener(listener); } /** * 设置按钮 * * @param text * @param listener */ public void setNegativeButton(String text, final View.OnClickListener listener) { btn_cancle.setText(text); btn_cancle.setVisibility(View.VISIBLE); btn_cancle.setOnClickListener(listener); } /** * 关闭对话框 */ public void dismiss() { try { ad.dismiss(); } catch (Exception e) { // TODO: handle exception } } } 布局文件Layout: alertdialog  资源文件ui_new_dialog_common_normal  ui_new_dialog_common_pressed                                                                                                                                                                 -END

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:android之监听设备电量变化
下一篇:CDMA2000 空中接口技术的演进
相关文章

 发表评论

暂时没有评论,来抢沙发吧~