高階自定義View專案實戰;Android 時間選擇器PickerView
一、優點(文末有福利)
1.可擴充套件性很強
2.三級聯動
3.單項選擇
4.條件 選擇
二、效果圖




三、使用步驟
1.新增Jcenter倉庫 Gradle依賴:
compile 'com.contrarywind:Android-PickerView:3.2.7'
2.在Activity中新增如下程式碼:
預設的時間選擇器
//時間選擇器 TimePickerView pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() { @Override public void onTimeSelect(Date date,View v) {//選中事件回撥 tvTime.setText(getTime(date)); } }) .build(); pvTime.setDate(Calendar.getInstance());//注:根據需求來決定是否使用該方法(一般是精確到秒的情況),此項可以在彈出選擇器的時候重新設定當前時間,避免在初始化之後由於時間已經設定,導致選中時間與當前時間不匹配的問題。 pvTime.show();
例子 選擇出生年月日
private void initTimePicker1() {//選擇出生年月日 //控制時間範圍(如果不設定範圍,則使用預設時間1900-2100年,此段程式碼可註釋) //因為系統Calendar的月份是從0-11的,所以如果是呼叫Calendar的set方法來設定時間,月份的範圍也要是從0-11 Date curDate = new Date(System.currentTimeMillis());//獲取當前時間 SimpleDateFormat formatter_year = new SimpleDateFormat("yyyy "); String year_str = formatter_year.format(curDate); int year_int = (int) Double.parseDouble(year_str); SimpleDateFormat formatter_mouth = new SimpleDateFormat("MM "); String mouth_str = formatter_mouth.format(curDate); int mouth_int = (int) Double.parseDouble(mouth_str); SimpleDateFormat formatter_day = new SimpleDateFormat("dd "); String day_str = formatter_day.format(curDate); int day_int = (int) Double.parseDouble(day_str); Calendar selectedDate = Calendar.getInstance();//系統當前時間 Calendar startDate = Calendar.getInstance(); startDate.set(1900, 0, 1); Calendar endDate = Calendar.getInstance(); endDate.set(year_int, mouth_int - 1, day_int); //時間選擇器 pvTime1 = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() { @Override public void onTimeSelect(Date date, View v) {//選中事件回撥 // 這裡回撥過來的v,就是show()方法裡面所新增的 View 引數,如果show的時候沒有新增引數,v則為null /*btn_Time.setText(getTime(date));*/ textview1.setText(getTime(date)); } }) .setType(new boolean[]{true, true, true, false, false, false}) //年月日時分秒 的顯示與否,不設定則預設全部顯示 .setLabel("年", "月", "日", "", "", "")//預設設定為年月日時分秒 .isCenterLabel(false) .setDividerColor(Color.RED) .setTextColorCenter(Color.RED)//設定選中項的顏色 .setTextColorOut(Color.BLUE)//設定沒有被選中項的顏色 .setContentSize(21) .setDate(selectedDate) .setLineSpacingMultiplier(1.2f) .setTextXOffset(-10, 0,10, 0, 0, 0)//設定X軸傾斜角度[ -90 , 90°] .setRangDate(startDate, endDate) // .setBackgroundId(0x00FFFFFF) //設定外部遮罩顏色 .setDecorView(null) .build(); } private String getTime(Date date) {//可根據需要自行擷取資料顯示 // SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); return format.format(date); }
自定義佈局
private void initCustomOptionPicker() {//條件選擇器初始化,自定義佈局 /** * @description * * 注意事項: * 自定義佈局中,id為 optionspicker 或者 timepicker 的佈局以及其子控制元件必須要有,否則會報空指標。 * 具體可參考demo 裡面的兩個自定義layout佈局。 */ pvCustomOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() { @Override public void onOptionsSelect(int options1, int option2, int options3, View v) { //返回的分別是三個級別的選中位置 String tx = cardItem.get(options1).getPickerViewText(); btn_CustomOptions.setText(tx); } }) .setLayoutRes(R.layout.pickerview_custom_options, new CustomListener() { @Override public void customLayout(View v) { final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish); final TextView tvAdd = (TextView) v.findViewById(R.id.tv_add); ImageView ivCancel = (ImageView) v.findViewById(R.id.iv_cancel); tvSubmit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { pvCustomOptions.returnData(); pvCustomOptions.dismiss(); } }); ivCancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { pvCustomOptions.dismiss(); } }); tvAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getCardData(); pvCustomOptions.setPicker(cardItem); } }); } }) .isDialog(true) .build(); pvCustomOptions.setPicker(cardItem);//新增資料 }
三級聯動
pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() { @Override public void onOptionsSelect(int options1, int options2, int options3, View v) { //返回的分別是三個級別的選中位置 String tx = options1Items.get(options1).getPickerViewText() + options2Items.get(options1).get(options2) /* + options3Items.get(options1).get(options2).get(options3).getPickerViewText()*/; btn_Options.setText(tx); } }) .setTitleText("城市選擇") .setContentTextSize(20)//設定滾輪文字大小 .setDividerColor(Color.LTGRAY)//設定分割線的顏色 .setSelectOptions(0, 1)//預設選中項 .setBgColor(Color.BLACK) .setTitleBgColor(Color.DKGRAY) .setTitleColor(Color.LTGRAY) .setCancelColor(Color.YELLOW) .setSubmitColor(Color.YELLOW) .setTextColorCenter(Color.LTGRAY) .isCenterLabel(false) //是否只顯示中間選中項的label文字,false則每項item全部都帶有label。 .setLabels("省", "市", "區") .setBackgroundId(0x66000000) //設定外部遮罩顏色 .build(); //pvOptions.setSelectOptions(1,1); /*pvOptions.setPicker(options1Items);//一級選擇器*/ pvOptions.setPicker(options1Items, options2Items);//二級選擇器 /*pvOptions.setPicker(options1Items, options2Items,options3Items);//三級選擇器*/
時間選擇器的屬性大全
.setType(new boolean[]{true, true, true, false, false, false}) //年月日時分秒 的顯示與否,不設定則預設全部顯示 .setLabel("年", "月", "日", "", "", "")//預設設定為年月日時分秒 .setSubmitText("確定")//確定按鈕文字 .setCancelText("取消")//取消按鈕文字 .setTitleText("請選擇")//標題 .setSubCalSize(18)//確定和取消文字大小 .setTitleSize(20)//標題文字大小 .setTitleColor(Color.GREEN)//標題文字顏色 .setSubmitColor(Color.GREEN)//確定按鈕文字顏色 .setCancelColor(Color.GREEN)//取消按鈕文字顏色 .setTitleBgColor(0xFF333333)//標題背景顏色 Night mode .setBgColor(0xFF000000)//滾輪背景顏色 Night mode .setOutSideCancelable(false)//點選螢幕,點在控制元件外部範圍時,是否取消顯示 .isCyclic(false)//是否迴圈滾動 .isCenterLabel(false) //是否只顯示中間選中項的label文字,false則每項item全部都帶有label。 .setDividerColor(Color.YELLOW)//設定分割線的顏色 .setTextColorCenter(Color.RED)//設定選中項的顏色 .setTextColorOut(Color.BLUE)//設定沒有被選中項的顏色 .setContentSize(21)//滾輪文字大小 .setDate(selectedDate)//// 如果不設定的話,預設是系統時間*/ .setLineSpacingMultiplier(1.2f)//設定兩橫線之間的間隔倍數 .setTextXOffset(-10, 0, 10, 0, 0, 0)//設定X軸傾斜角度[ -90 , 90°] .setRangDate(startDate, endDate)////起始終止年月日設定 .setBackgroundId(0x00FFFFFF) //設定外部遮罩顏色 .setDecorView(null)//設定要將pickerview顯示到的容器id 必須是viewgroup .isDialog(false)//是否顯示為對話方塊樣式
更多activity相關知識體系,包括生命週期與核心管理原理+ViewGroup原始碼

Android高階必備