- 论坛徽章:
- 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"/>
- <?xml version="1.0" encoding="UTF-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:dateTime="http://schemas.android.com/apk/res/com.app"/>
- <com.app.view.DatePickText android:layout_marginLeft="7dp" android:layout_width="230dp" android:layout_height="35dp"
- 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"/>
- <?xml version="1.0" encoding="UTF-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:dateTime="http://schemas.android.com/apk/res/com.app"/>
- <com.app.view.DatePickText android:layout_marginLeft="7dp" android:layout_width="230dp" android:layout_height="35dp"
- 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>
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <declare-styleable name="DatePickText">
-
- <attr name="dateFormatStr" format="string"/>
- <attr name="dateFormat" >
- <!-- yyyy-MM-dd -->
- <enum name="date" value="0" />
- <!-- HH:mm:ss -->
- <enum name="time" value="1" />
- </attr>
-
- </declare-styleable>
- </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.}
- package com.app.view;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Locale;
- import android.app.DatePickerDialog;
- import android.app.TimePickerDialog;
- import android.content.Context;
- import android.content.res.TypedArray;
- import android.util.AttributeSet;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.DatePicker;
- import android.widget.EditText;
- import android.widget.ImageButton;
- import android.widget.LinearLayout;
- import android.widget.TimePicker;
- import com.app.R;
- public class DatePickText extends LinearLayout {
- private Integer dateFormat;
- private String layout_height,layout_width;
- private String dateFormatStr;
- private EditText edit;
- private ImageButton btn_date;
- private LinearLayout layout;
- public static final int TOP = 0;
- public static final int BOTTOM = 1;
- public static final int LEFT = 2;
- public static final int RIGHT = 3;
-
- public static final int DATE = 0;
- public static final int TIME = 1;
- private SimpleDateFormat df ;
- private final Calendar cal = Calendar.getInstance(Locale.SIMPLIFIED_CHINESE);
-
- public DatePickText(Context context) {
- super(context);
-
- }
-
- public DatePickText(Context context, AttributeSet attrs) {
- super(context, attrs);
-
- TypedArray typeA =context.obtainStyledAttributes(attrs, R.styleable.DatePickText);
-
- layout_height=typeA.getString(R.styleable.DatePickText_layout_height);
- layout_width=typeA.getString(R.styleable.DatePickText_layout_width);
- dateFormatStr=typeA.getString(R.styleable.DatePickText_dateFormatStr);
- dateFormat=typeA.getInteger(R.styleable.DatePickText_dateFormat,DATE);
- //typeA.g
-
- LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- layoutInflater.inflate(R.layout.date_pick_txt,this);
- layout=(LinearLayout)findViewById(R.id.date_linear);
- edit=(EditText)findViewById(R.id.date_txt);
- btn_date=(ImageButton)findViewById(R.id.date_btn);
-
- processUi(context);
- }
-
- private void processUi(final Context context){
- //ViewGroup.LayoutParams params=new ViewGroup.LayoutParams(params);
- //layout.setLayoutParams(params);
-
- btn_date.setOnClickListener(new OnClickListener(){
-
- @Override
- public void onClick(View v) {
- System.out.println("-------------click------------");
- buildDateOrTimeDialog(context);
-
- }
-
- });
-
- }
- private void buildDateOrTimeDialog(Context context){
- df = new SimpleDateFormat(dateFormatStr);
-
- switch(dateFormat)
- {
- case DATE:
- date:
- new DatePickerDialog( context,listener ,
- cal .get(Calendar. YEAR ),
-
- cal .get(Calendar. MONTH ),
-
- cal .get(Calendar. DAY_OF_MONTH )
-
- ).show();
- break;
-
- case TIME:
- System.out.println("----------time---------------");
- new TimePickerDialog(context,timeListen,cal.get(Calendar.HOUR_OF_DAY),cal.get(Calendar.MINUTE),true).show();
- break;
- default:
- new DatePickerDialog( context,listener ,
- cal .get(Calendar. YEAR ),
-
- cal .get(Calendar. MONTH ),
-
- cal .get(Calendar. DAY_OF_MONTH )
-
- ).show();
-
- }
-
- }
-
-
- private DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener(){ //
-
- @Override
-
- public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
-
- cal .set(Calendar. YEAR , arg1);
-
- cal .set(Calendar. MONTH , arg2);
-
- cal .set(Calendar. DAY_OF_MONTH , arg3);
-
- updateDate();
-
- }
-
- };
-
- // 当 DatePickerDialog 关闭,更新日期显示
-
- private void updateDate(){
-
- edit.setText( df .format( cal .getTime()));
-
- }
-
- TimePickerDialog.OnTimeSetListener timeListen = new TimePickerDialog.OnTimeSetListener() {
- //同DatePickerDialog控件
- @Override
- public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
- cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
- cal.set(Calendar.MINUTE, minute);
- cal.set(Calendar.SECOND, cal.get(Calendar.SECOND));
- updateTimes();
- }
-
- };
-
- //更新页面TextView的方法
- private void updateTimes() {
-
- edit.setText(df.format(cal.getTime()));
- }
- }
复制代码 实现类中用到的布局文件
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>
复制代码 |
|