免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3951 | 回复: 0
打印 上一主题 下一主题

android自定义控件或属性-日期时间选择框 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-11-17 16:05 |只看该作者 |倒序浏览
android自定义控件或属性-日期时间选择框





关于自定义控件或属性
请转此学习
看代码之前先看看效果图
时间选择

使用方法:配置为时间(dateTime:dateFormatStr="HH:mm:ss" dateTime:dateFormat="time"

Xml代码
  1. 1.<?xml version="1.0" encoding="UTF-8"?>  
  2. 2.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3. 3.        xmlns:dateTime="http://schemas.android.com/apk/res/com.app"/>  
  4. 4.  
  5. 5. <com.app.view.DatePickText  android:layout_marginLeft="7dp"   android:layout_width="230dp" android:layout_height="35dp"   
  6. 6.         android:id="@+id/v_birthday" dateTime:dateFormatStr="HH:mm:ss" dateTime:dateFormat="time"/>  
  7. <?xml version="1.0" encoding="UTF-8"?>
  8. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  9.         xmlns:dateTime="http://schemas.android.com/apk/res/com.app"/>

  10. <com.app.view.DatePickText  android:layout_marginLeft="7dp"   android:layout_width="230dp" android:layout_height="35dp"
  11.              android:id="@+id/v_birthday" dateTime:dateFormatStr="HH:mm:ss" dateTime:dateFormat="time"/>
复制代码
看代码之前先看看效果图

日期选择

使用方法:配置为日期(dateTime:dateFormatStr="yyyy-MM-dd" dateTime:dateFormat="date"

Xml代码
  1. 1.<?xml version="1.0" encoding="UTF-8"?>  
  2. 2.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3. 3.        xmlns:dateTime="http://schemas.android.com/apk/res/com.app"/>  
  4. 4.  
  5. 5. <com.app.view.DatePickText  android:layout_marginLeft="7dp"   android:layout_width="230dp" android:layout_height="35dp"   
  6. 6.         android:id="@+id/v_birthday" dateTime:dateFormatStr="yyyy-MM-dd" dateTime:dateFormat="date"/>  
  7. <?xml version="1.0" encoding="UTF-8"?>
  8. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  9.         xmlns:dateTime="http://schemas.android.com/apk/res/com.app"/>

  10. <com.app.view.DatePickText  android:layout_marginLeft="7dp"   android:layout_width="230dp" android:layout_height="35dp"
  11.              android:id="@+id/v_birthday" dateTime:dateFormatStr="yyyy-MM-dd" dateTime:dateFormat="date"/>

  12. res/values/attrs.xml
复制代码
Xml代码
  1. 1.<?xml version="1.0" encoding="utf-8"?>  
  2. 2.<resources>  
  3. 3.    <declare-styleable name="DatePickText">     
  4. 4.         
  5. 5.        <attr name="dateFormatStr" format="string"/>  
  6. 6.        <attr name="dateFormat" >   
  7. 7.              <!-- yyyy-MM-dd  -->   
  8. 8.             <enum name="date" value="0" />  
  9. 9.             <!-- HH:mm:ss -->  
  10. 10.             <enum name="time" value="1" />  
  11. 11.         </attr>  
  12. 12.         
  13. 13.    </declare-styleable>     
  14. 14.</resources>  
  15. <?xml version="1.0" encoding="utf-8"?>
  16. <resources>
  17.     <declare-styleable name="DatePickText">  
  18.       
  19.         <attr name="dateFormatStr" format="string"/>
  20.         <attr name="dateFormat" >
  21.               <!-- yyyy-MM-dd  -->
  22.                  <enum name="date" value="0" />
  23.                  <!-- HH:mm:ss -->
  24.                  <enum name="time" value="1" />
  25.              </attr>
  26.       
  27.     </declare-styleable>  
  28. </resources>
复制代码
实现类

Java代码
  1. 1.package com.app.view;   
  2. 2.  
  3. 3.import java.text.SimpleDateFormat;   
  4. 4.import java.util.Calendar;   
  5. 5.import java.util.Locale;   
  6. 6.  
  7. 7.import android.app.DatePickerDialog;   
  8. 8.import android.app.TimePickerDialog;   
  9. 9.import android.content.Context;   
  10. 10.import android.content.res.TypedArray;   
  11. 11.import android.util.AttributeSet;   
  12. 12.import android.view.LayoutInflater;   
  13. 13.import android.view.View;   
  14. 14.import android.widget.DatePicker;   
  15. 15.import android.widget.EditText;   
  16. 16.import android.widget.ImageButton;   
  17. 17.import android.widget.LinearLayout;   
  18. 18.import android.widget.TimePicker;   
  19. 19.  
  20. 20.import com.app.R;   
  21. 21.  
  22. 22.public class DatePickText extends LinearLayout {   
  23. 23.  
  24. 24.   private Integer dateFormat;   
  25. 25.   private String layout_height,layout_width;   
  26. 26.   private String dateFormatStr;   
  27. 27.   private EditText edit;   
  28. 28.   private ImageButton btn_date;   
  29. 29.   private LinearLayout layout;   
  30. 30.    public static final int TOP = 0;   
  31. 31.    public static final int BOTTOM = 1;   
  32. 32.    public static final int LEFT = 2;   
  33. 33.    public static final int RIGHT = 3;   
  34. 34.      
  35. 35.    public static final int DATE = 0;   
  36. 36.    public static final int TIME = 1;   
  37. 37.    private SimpleDateFormat df ;   
  38. 38.    private final Calendar cal = Calendar.getInstance(Locale.SIMPLIFIED_CHINESE);   
  39. 39.      
  40. 40.    public DatePickText(Context context) {   
  41. 41.        super(context);   
  42. 42.           
  43. 43.    }   
  44. 44.  
  45. 45.      
  46. 46.  
  47. 47.    public DatePickText(Context context, AttributeSet attrs) {   
  48. 48.        super(context, attrs);   
  49. 49.           
  50. 50.        TypedArray typeA =context.obtainStyledAttributes(attrs, R.styleable.DatePickText);   
  51. 51.           
  52. 52.        layout_height=typeA.getString(R.styleable.DatePickText_layout_height);   
  53. 53.        layout_width=typeA.getString(R.styleable.DatePickText_layout_width);   
  54. 54.         dateFormatStr=typeA.getString(R.styleable.DatePickText_dateFormatStr);   
  55. 55.         dateFormat=typeA.getInteger(R.styleable.DatePickText_dateFormat,DATE);   
  56. 56.        //typeA.g   
  57. 57.            
  58. 58.     LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);   
  59. 59.     layoutInflater.inflate(R.layout.date_pick_txt,this);   
  60. 60.     layout=(LinearLayout)findViewById(R.id.date_linear);   
  61. 61.     edit=(EditText)findViewById(R.id.date_txt);   
  62. 62.     btn_date=(ImageButton)findViewById(R.id.date_btn);   
  63. 63.        
  64. 64.     processUi(context);   
  65. 65.    }   
  66. 66.      
  67. 67.    private void processUi(final Context context){   
  68. 68.        //ViewGroup.LayoutParams params=new ViewGroup.LayoutParams(params);   
  69. 69.        //layout.setLayoutParams(params);   
  70. 70.           
  71. 71.        btn_date.setOnClickListener(new OnClickListener(){   
  72. 72.            
  73. 73.            @Override  
  74. 74.            public void onClick(View v) {   
  75. 75.                System.out.println("-------------click------------");   
  76. 76.                buildDateOrTimeDialog(context);   
  77. 77.                  
  78. 78.            }   
  79. 79.               
  80. 80.        });   
  81. 81.           
  82. 82.    }   
  83. 83.    private void buildDateOrTimeDialog(Context context){   
  84. 84.        df = new SimpleDateFormat(dateFormatStr);   
  85. 85.           
  86. 86.        switch(dateFormat)   
  87. 87.        {   
  88. 88.        case DATE:   
  89. 89.             date:   
  90. 90.                new DatePickerDialog( context,listener ,   
  91. 91.                    cal .get(Calendar. YEAR ),   
  92. 92.                        
  93. 93.                    cal .get(Calendar. MONTH ),   
  94. 94.                        
  95. 95.                    cal .get(Calendar. DAY_OF_MONTH )   
  96. 96.                        
  97. 97.                    ).show();   
  98. 98.            break;   
  99. 99.                        
  100. 100.        case TIME:   
  101. 101.            System.out.println("----------time---------------");   
  102. 102.             new TimePickerDialog(context,timeListen,cal.get(Calendar.HOUR_OF_DAY),cal.get(Calendar.MINUTE),true).show();   
  103. 103.             break;   
  104. 104.        default:   
  105. 105.            new DatePickerDialog( context,listener ,   
  106. 106.                    cal .get(Calendar. YEAR ),   
  107. 107.                        
  108. 108.                    cal .get(Calendar. MONTH ),   
  109. 109.                        
  110. 110.                    cal .get(Calendar. DAY_OF_MONTH )   
  111. 111.                        
  112. 112.                    ).show();   
  113. 113.               
  114. 114.        }   
  115. 115.      
  116. 116.}   
  117. 117.           
  118. 118.      
  119. 119.private DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener(){  //   
  120. 120.        
  121. 121.    @Override  
  122. 122.        
  123. 123.    public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {   
  124. 124.        
  125. 125.    cal .set(Calendar. YEAR , arg1);   
  126. 126.        
  127. 127.    cal .set(Calendar. MONTH , arg2);   
  128. 128.        
  129. 129.    cal .set(Calendar. DAY_OF_MONTH , arg3);   
  130. 130.        
  131. 131.    updateDate();   
  132. 132.        
  133. 133.    }   
  134. 134.        
  135. 135.    };   
  136. 136.      
  137. 137.    // 当 DatePickerDialog 关闭,更新日期显示   
  138. 138.        
  139. 139.    private void updateDate(){   
  140. 140.        
  141. 141.          edit.setText( df .format( cal .getTime()));   
  142. 142.        
  143. 143.    }   
  144. 144.      
  145. 145.    TimePickerDialog.OnTimeSetListener timeListen = new TimePickerDialog.OnTimeSetListener() {   
  146. 146.  
  147. 147.        //同DatePickerDialog控件   
  148. 148.  
  149. 149.        @Override  
  150. 150.        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {   
  151. 151.            cal.set(Calendar.HOUR_OF_DAY, hourOfDay);   
  152. 152.            cal.set(Calendar.MINUTE, minute);   
  153. 153.            cal.set(Calendar.SECOND, cal.get(Calendar.SECOND));   
  154. 154.          updateTimes();   
  155. 155.        }   
  156. 156.  
  157. 157.      
  158. 158.  
  159. 159.        };   
  160. 160.           
  161. 161.        //更新页面TextView的方法   
  162. 162.    private void updateTimes() {   
  163. 163.      
  164. 164.            edit.setText(df.format(cal.getTime()));   
  165. 165.    }   
  166. 166.}  
  167. package com.app.view;

  168. import java.text.SimpleDateFormat;
  169. import java.util.Calendar;
  170. import java.util.Locale;

  171. import android.app.DatePickerDialog;
  172. import android.app.TimePickerDialog;
  173. import android.content.Context;
  174. import android.content.res.TypedArray;
  175. import android.util.AttributeSet;
  176. import android.view.LayoutInflater;
  177. import android.view.View;
  178. import android.widget.DatePicker;
  179. import android.widget.EditText;
  180. import android.widget.ImageButton;
  181. import android.widget.LinearLayout;
  182. import android.widget.TimePicker;

  183. import com.app.R;

  184. public class DatePickText extends LinearLayout {

  185.    private Integer dateFormat;
  186.    private String layout_height,layout_width;
  187.    private String dateFormatStr;
  188.    private EditText edit;
  189.    private ImageButton btn_date;
  190.    private LinearLayout layout;
  191.     public static final int TOP = 0;
  192.         public static final int BOTTOM = 1;
  193.         public static final int LEFT = 2;
  194.         public static final int RIGHT = 3;
  195.        
  196.         public static final int DATE = 0;
  197.         public static final int TIME = 1;
  198.         private SimpleDateFormat df ;
  199.         private final Calendar cal = Calendar.getInstance(Locale.SIMPLIFIED_CHINESE);
  200.        
  201.         public DatePickText(Context context) {
  202.                 super(context);
  203.                
  204.         }

  205.        

  206.         public DatePickText(Context context, AttributeSet attrs) {
  207.                 super(context, attrs);
  208.                
  209.                 TypedArray typeA =context.obtainStyledAttributes(attrs, R.styleable.DatePickText);
  210.                
  211.                 layout_height=typeA.getString(R.styleable.DatePickText_layout_height);
  212.                 layout_width=typeA.getString(R.styleable.DatePickText_layout_width);
  213.                  dateFormatStr=typeA.getString(R.styleable.DatePickText_dateFormatStr);
  214.                  dateFormat=typeA.getInteger(R.styleable.DatePickText_dateFormat,DATE);
  215.                 //typeA.g
  216.                  
  217.          LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  218.          layoutInflater.inflate(R.layout.date_pick_txt,this);
  219.          layout=(LinearLayout)findViewById(R.id.date_linear);
  220.          edit=(EditText)findViewById(R.id.date_txt);
  221.          btn_date=(ImageButton)findViewById(R.id.date_btn);
  222.          
  223.          processUi(context);
  224.         }
  225.    
  226.         private void processUi(final Context context){
  227.                 //ViewGroup.LayoutParams params=new ViewGroup.LayoutParams(params);
  228.                 //layout.setLayoutParams(params);
  229.                
  230.                 btn_date.setOnClickListener(new OnClickListener(){
  231.          
  232.                         @Override
  233.                         public void onClick(View v) {
  234.                                 System.out.println("-------------click------------");
  235.                                 buildDateOrTimeDialog(context);
  236.                                
  237.                         }
  238.                        
  239.                 });
  240.                
  241.         }
  242.         private void buildDateOrTimeDialog(Context context){
  243.                 df = new SimpleDateFormat(dateFormatStr);
  244.                
  245.                 switch(dateFormat)
  246.                 {
  247.                 case DATE:
  248.                          date:
  249.                                 new DatePickerDialog( context,listener ,
  250.                                         cal .get(Calendar. YEAR ),
  251.                                          
  252.                                         cal .get(Calendar. MONTH ),
  253.                                          
  254.                                         cal .get(Calendar. DAY_OF_MONTH )
  255.                                          
  256.                                         ).show();
  257.                         break;
  258.                                          
  259.                 case TIME:
  260.                         System.out.println("----------time---------------");
  261.                          new TimePickerDialog(context,timeListen,cal.get(Calendar.HOUR_OF_DAY),cal.get(Calendar.MINUTE),true).show();
  262.                          break;
  263.                 default:
  264.                         new DatePickerDialog( context,listener ,
  265.                                         cal .get(Calendar. YEAR ),
  266.                                          
  267.                                         cal .get(Calendar. MONTH ),
  268.                                          
  269.                                         cal .get(Calendar. DAY_OF_MONTH )
  270.                                          
  271.                                         ).show();
  272.                        
  273.                 }
  274.        
  275. }
  276.                
  277.        
  278. private DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener(){  //
  279.          
  280.         @Override
  281.          
  282.         public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
  283.          
  284.         cal .set(Calendar. YEAR , arg1);
  285.          
  286.         cal .set(Calendar. MONTH , arg2);
  287.          
  288.         cal .set(Calendar. DAY_OF_MONTH , arg3);
  289.          
  290.         updateDate();
  291.          
  292.         }
  293.          
  294.         };
  295.        
  296.         // 当 DatePickerDialog 关闭,更新日期显示
  297.          
  298.         private void updateDate(){
  299.          
  300.                   edit.setText( df .format( cal .getTime()));
  301.          
  302.         }
  303.        
  304.         TimePickerDialog.OnTimeSetListener timeListen = new TimePickerDialog.OnTimeSetListener() {

  305.                 //同DatePickerDialog控件

  306.                 @Override
  307.                 public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
  308.                         cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
  309.                         cal.set(Calendar.MINUTE, minute);
  310.                         cal.set(Calendar.SECOND, cal.get(Calendar.SECOND));
  311.                   updateTimes();
  312.                 }

  313.        

  314.                 };
  315.                
  316.                 //更新页面TextView的方法
  317.         private void updateTimes() {
  318.        
  319.                         edit.setText(df.format(cal.getTime()));
  320.         }
  321. }
复制代码
实现类中用到的布局文件
date_pick_txt.xml

Xml代码
  1. 1.<?xml version="1.0" encoding="utf-8"?>  
  2. 2.<LinearLayout  
  3. 3.  xmlns:android="http://schemas.android.com/apk/res/android"  
  4. 4.  android:orientation="horizontal" android:id="@+id/date_linear"  
  5. 5.  android:layout_width="230dp"  
  6. 6.  android:layout_height="35dp">  
  7. 7.   <RelativeLayout android:id="@+id/date_relative" android:layout_height="fill_parent" android:layout_width="fill_parent">  
  8. 8.       <EditText  android:id="@+id/date_txt" android:editable="false" android:layout_height="fill_parent" android:layout_width="fill_parent"  
  9. 9.             android:includeFontPadding="false" android:hint="yyyy-mm-dd"/>  
  10. 10.             <ImageButton android:src="@drawable/date_pic" android:layout_width="28dp" android:layout_marginLeft="-33dp"   
  11. 11.            android:layout_alignBottom="@+id/date_txt"   android:layout_centerInParent="true" android:layout_centerHorizontal="true"  
  12. 12.          android:layout_height="26dp" android:layout_toRightOf="@+id/date_txt" android:id="@+id/date_btn"/>/   
  13. 13.         
  14. 14.    </RelativeLayout>  
  15. 15.   
  16. 16.      
  17. 17.</LinearLayout>  
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP