亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

android自定義控件或屬性-日期時間選擇框

系統 2113 0

關于自定義控件或屬性?
請轉此學習 ?
看代碼之前先看看效果圖?
時間選擇?
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" /> ??

看代碼之前先看看效果圖?

日期選擇?
android自定義控件或屬性-日期時間選擇框 ?
使用方法:配置為日期(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 > ??

android自定義控件或屬性-日期時間選擇框


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 成熟女人免费一级毛片 | 国产成人免费在线视频 | 夜夜骑加勒比 | 99视频在线精品免费观看18 | 欧美日韩网站 | 精品视频 九九九 | 91在线播| 久久大伊人| 四虎网站在线播放 | 国产永久视频 | 综合88| 精品国产一区二区在线观看 | 久久精品这里精品 | 日韩欧美亚 | 99热免费观看 | 老司机免费福利影院 | 久久99国产精品免费观看 | 四虎影视永久费观看在线 | 免费的毛片 | 亚洲欧美香蕉在线日韩精选 | 日本午夜www高清视频 | 妞干网这里只有精品 | 欧美1区| 亚洲精品久久久久福利网站 | 久久人人爽人人爽人人片av不 | 亚洲精品国产成人中文 | 亚洲精品不卡久久久久久 | 成人在线一区二区 | 精品成人免费一区二区在线播放 | 天堂亚洲国产日韩在线看 | 四虎在线观看网址 | 天海翼精品久久中文字幕 | 嫩草嫩草55av | 久久国产这里只有精品 | 国产成人精品高清免费 | 欧洲一级黄色片 | 久久国产精品免费视频 | 日本毛片在线观看 | 露脸超嫩97后在线播放 | 成人国产精品毛片 | 一个色在线 |