關于自定義控件或屬性?
請轉此學習 ?
看代碼之前先看看效果圖?
時間選擇?
?
使用方法:配置為時間(dateTime:dateFormatStr="HH:mm:ss" dateTime:dateFormat="time")?
看代碼之前先看看效果圖?
日期選擇?
?
使用方法:配置為日期(dateTime:dateFormatStr="yyyy-MM-dd" dateTime:dateFormat="date")?
res/values/attrs.xml?
實現類?
實現類中用到的布局文件?
date_pick_txt.xml?
請轉此學習 ?
看代碼之前先看看效果圖?
時間選擇?

使用方法:配置為時間(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 > ??
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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