当前位置: 代码迷 >> Android >> android自定义控件或属性-日期时间取舍框(转)
  详细解决方案

android自定义控件或属性-日期时间取舍框(转)

热度:121   发布时间:2016-05-01 20:50:15.0
android自定义控件或属性-日期时间选择框(转)

android自定义控件或属性-日期时间选择框

    博客分类:?
  • android
android?
关于自定义控件或属性?
请转此学习?
看代码之前先看看效果图?
时间选择?
?
使用方法:配置为时间(dateTime:dateFormatStr="HH:mm:ss" dateTime:dateFormat="time")?
Xml代码??收藏代码
  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  3. ????????xmlns:dateTime="http://schemas.android.com/apk/res/com.app"/>??
  4. ??
  5. ?<com.app.view.DatePickText??android:layout_marginLeft="7dp"???android:layout_width="230dp"?android:layout_height="35dp"???
  6. ?????????android:id="@+id/v_birthday"?dateTime:dateFormatStr="HH:mm:ss"?dateTime:dateFormat="time"/>??

看代码之前先看看效果图?

日期选择?
?
使用方法:配置为日期(dateTime:dateFormatStr="yyyy-MM-dd" dateTime:dateFormat="date")?
Xml代码??收藏代码
  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  3. ????????xmlns:dateTime="http://schemas.android.com/apk/res/com.app"/>??
  4. ??
  5. ?<com.app.view.DatePickText??android:layout_marginLeft="7dp"???android:layout_width="230dp"?android:layout_height="35dp"???
  6. ?????????android:id="@+id/v_birthday"?dateTime:dateFormatStr="yyyy-MM-dd"?dateTime:dateFormat="date"/>??

res/values/attrs.xml?
Xml代码??收藏代码
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <resources>??
  3. ????<declare-styleable?name="DatePickText">????
  4. ?????????
  5. ????????<attr?name="dateFormatStr"?format="string"/>??
  6. ????????<attr?name="dateFormat"?>???
  7. ??????????????<!--?yyyy-MM-dd??-->???
  8. ?????????????<enum?name="date"?value="0"?/>??
  9. ?????????????<!--?HH:mm:ss?-->??
  10. ?????????????<enum?name="time"?value="1"?/>??
  11. ?????????</attr>??
  12. ????????
  13. ????</declare-styleable>????
  14. </resources>??

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

实现类中用到的布局文件?
date_pick_txt.xml?
Xml代码??收藏代码
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <LinearLayout??
  3. ??xmlns:android="http://schemas.android.com/apk/res/android"??
  4. ??android:orientation="horizontal"?android:id="@+id/date_linear"??
  5. ??android:layout_width="230dp"??
  6. ??android:layout_height="35dp">??
  7. ???<RelativeLayout?android:id="@+id/date_relative"?android:layout_height="fill_parent"?android:layout_width="fill_parent">??
  8. ???????<EditText??android:id="@+id/date_txt"?android:editable="false"?android:layout_height="fill_parent"?android:layout_width="fill_parent"??
  9. ?????????????android:includeFontPadding="false"?android:hint="yyyy-mm-dd"/>??
  10. ?????????????<ImageButton?android:src="@drawable/date_pic"?android:layout_width="28dp"?android:layout_marginLeft="-33dp"???
  11. ????????????android:layout_alignBottom="@+id/date_txt"???android:layout_centerInParent="true"?android:layout_centerHorizontal="true"??
  12. ??????????android:layout_height="26dp"?android:layout_toRightOf="@+id/date_txt"?android:id="@+id/date_btn"/>/??
  13. ?????????
  14. ????</RelativeLayout>??
  15. ???
  16. ??????
  17. </LinearLayout>??
  相关解决方案