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

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

热度:105   发布时间:2016-05-01 14:25:32.0
android自定义控件或属性-日期时间选择框
关于自定义控件或属性
请转此学习
看代码之前先看看效果图
时间选择

使用方法:配置为时间(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 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 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>

实现类
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 version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="horizontal" android:id="@+id/date_linear"  android:layout_width="230dp"  android:layout_height="35dp">   <RelativeLayout android:id="@+id/date_relative" android:layout_height="fill_parent" android:layout_width="fill_parent">       <EditText  android:id="@+id/date_txt" android:editable="false" android:layout_height="fill_parent" android:layout_width="fill_parent"             android:includeFontPadding="false" android:hint="yyyy-mm-dd"/>             <ImageButton android:src="@drawable/date_pic" android:layout_width="28dp" android:layout_marginLeft="-33dp"             android:layout_alignBottom="@+id/date_txt"   android:layout_centerInParent="true" android:layout_centerHorizontal="true"          android:layout_height="26dp" android:layout_toRightOf="@+id/date_txt" android:id="@+id/date_btn"/>/           </RelativeLayout>     </LinearLayout>

1 楼 endual 2012-06-19  
这个正好是我要的效果呵呵,就是把代码复制下来错误了。
哎。。。,这个自定义的xml第一次用,所以不太懂。
多谢多谢
2 楼 喧嚣求静 2012-08-24  
endual 写道
这个正好是我要的效果呵呵,就是把代码复制下来错误了。
哎。。。,这个自定义的xml第一次用,所以不太懂。
多谢多谢

是不是,引用到图片资源没有,这个只要同名,你随便给个图片就可
  相关解决方案