Android 自定义AlertDialog对话框

来源:互联网 发布:阿里云邮件服务器地址 编辑:程序博客网 时间:2024/04/27 21:39

原文链接地址:http://www.pocketdigi.com/20120703/885.html

系统默认的AlertDialog,与项目的UI不统一,所以,改了一下,定义了一样式,最终效果如下图:

另外,为了尽量少改原来的代码,该类类名及相关方法名都与android.app.AlertDialog相同,使用时,原代码只需要修改导入的包名,在按钮的Listener上加上一句关闭对话框的方法即可.
先是对话框的XML布局文件:
test.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6.  
  7. <!-- 顶部椭园边缘 -->
  8. <ImageView
  9. android:layout_width="400dp"
  10. android:layout_height="22dp"
  11. android:src="@drawable/dialog_top" >
  12. </ImageView>
  13. <!-- 中间白色背景,两个TextView,标题和内容,留一个LinearLayout,在代码中根据调用动态加上按钮 -->
  14. <LinearLayout
  15. android:layout_width="400dp"
  16. android:layout_height="wrap_content"
  17. android:background="@drawable/dialog_center"
  18. android:orientation="vertical" >
  19.  
  20. <TextView android:textColor="#000000"
  21. android:textSize="35dp"
  22. android:gravity="center"
  23. android:id="@+id/title"
  24. android:layout_width="fill_parent"
  25. android:layout_height="wrap_content" />
  26.  
  27. <TextView android:layout_marginLeft="20dp"
  28. android:layout_marginRight="10dp"
  29. android:id="@+id/message"
  30. android:layout_marginBottom="10dp"
  31. android:layout_marginTop="20dp"
  32. android:textColor="#000000"
  33. android:textSize="25dp"
  34. android:layout_width="fill_parent"
  35. android:layout_height="wrap_content" />
  36. <!-- 在LinearLayout中加按钮 -->
  37. <LinearLayout
  38. android:id="@+id/buttonLayout"
  39. android:layout_width="fill_parent"
  40. android:layout_height="fill_parent"
  41. android:layout_gravity="center"
  42. android:gravity="center"
  43. android:orientation="horizontal" >
  44. </LinearLayout>
  45. </LinearLayout>
  46. <!-- 底部椭园边缘 -->
  47. <ImageView
  48. android:layout_marginTop="-2dp"
  49. android:layout_width="400dp"
  50. android:layout_height="22dp"
  51. android:src="@drawable/dialog_bottom" >
  52. </ImageView>
  53.  
  54. </LinearLayout>

按钮背景,不同状态不同,可参考本博相关文章:
alertdialog_button.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <item android:state_pressed="true" android:drawable="@drawable/dialog_button_pressed" />
  4. <item android:state_focused="true" android:drawable="@drawable/dialog_button_pressed" />
  5. <item android:drawable="@drawable/dialog_button" />
  6. </selector>

下面就是自定义的AlertDialog类的代码:

  1. public class AlertDialog {
  2. Context context;
  3. android.app.AlertDialog ad;
  4. TextView titleView;
  5. TextView messageView;
  6. LinearLayout buttonLayout;
  7. public AlertDialog(Context context) {
  8. // TODO Auto-generated constructor stub
  9. this.context=context;
  10. ad=new android.app.AlertDialog.Builder(context).create();
  11. ad.show();
  12. //关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
  13. Window window = ad.getWindow();
  14. window.setContentView(R.layout.alertdialog);
  15. titleView=(TextView)window.findViewById(R.id.title);
  16. messageView=(TextView)window.findViewById(R.id.message);
  17. buttonLayout=(LinearLayout)window.findViewById(R.id.buttonLayout);
  18. }
  19. public void setTitle(int resId)
  20. {
  21. titleView.setText(resId);
  22. }
  23. public void setTitle(String title) {
  24. titleView.setText(title);
  25. }
  26. public void setMessage(int resId) {
  27. messageView.setText(resId);
  28. }
  29.  
  30. public void setMessage(String message)
  31. {
  32. messageView.setText(message);
  33. }
  34. /**
  35. * 设置按钮
  36. * @param text
  37. * @param listener
  38. */
  39. public void setPositiveButton(String text,final View.OnClickListener listener)
  40. {
  41. Button button=new Button(context);
  42. LinearLayout.LayoutParams params=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  43. button.setLayoutParams(params);
  44. button.setBackgroundResource(R.drawable.alertdialog_button);
  45. button.setText(text);
  46. button.setTextColor(Color.WHITE);
  47. button.setTextSize(20);
  48. button.setOnClickListener(listener);
  49. buttonLayout.addView(button);
  50. }
  51. /**
  52. * 设置按钮
  53. * @param text
  54. * @param listener
  55. */
  56. public void setNegativeButton(String text,final View.OnClickListener listener)
  57. {
  58. Button button=new Button(context);
  59. LinearLayout.LayoutParams params=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  60. button.setLayoutParams(params);
  61. button.setBackgroundResource(R.drawable.alertdialog_button);
  62. button.setText(text);
  63. button.setTextColor(Color.WHITE);
  64. button.setTextSize(20);
  65. button.setOnClickListener(listener);
  66. if(buttonLayout.getChildCount()>0)
  67. {
  68. params.setMargins(20, 0, 0, 0);
  69. button.setLayoutParams(params);
  70. buttonLayout.addView(button, 1);
  71. }else{
  72. button.setLayoutParams(params);
  73. buttonLayout.addView(button);
  74. }
  75. }
  76. /**
  77. * 关闭对话框
  78. */
  79. public void dismiss() {
  80. ad.dismiss();
  81. }
  82. }

使用方法:

  1. final AlertDialog ad=new AlertDialog(Test.this);
  2. ad.setTitle("标题");
  3. ad.setMessage("内容sdfsafdasf内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容");
  4. ad.setPositiveButton("确定", new OnClickListener() {
  5. @Override
  6. public void onClick(View v) {
  7. // TODO Auto-generated method stub
  8. ad.dismiss();
  9. Toast.makeText(Test.this, "被点到确定", Toast.LENGTH_LONG).show();
  10. }
  11. });
  12. ad.setNegativeButton("取消", new OnClickListener() {
  13. @Override
  14. public void onClick(View v) {
  15. // TODO Auto-generated method stub
  16. ad.dismiss();
  17. Toast.makeText(Test.this, "被点到取消", Toast.LENGTH_LONG).show();
  18. }
  19. });

© 2012, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接! 应用开发笔记

0 0