android時(shí)間選擇控件之TimePickerView使用方法詳解
相信大家都有這樣的一個(gè)需求,選擇相應(yīng)開始時(shí)間和結(jié)束時(shí)間,對(duì)數(shù)據(jù)進(jìn)行篩選,下面就將使用TimePickerView實(shí)現(xiàn)這么一個(gè)功能。
一、先導(dǎo)入依賴
implementation "com.contrarywind:Android-PickerView:3.2.7"
二、在界面上畫出選擇時(shí)間的框框,這里大家就根據(jù)自己的UI畫就行,我個(gè)人用的是約束性布局
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent"> ? ? <TextView ? ? ? ? android:id="@+id/tv_history_default" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_marginLeft="40dp" ? ? ? ? android:layout_marginTop="10dp" ? ? ? ? android:text="時(shí)間" ? ? ? ? android:textColor="@color/white" ? ? ? ? android:textSize="36sp" ? ? ? ? app:layout_constraintLeft_toLeftOf="parent" ? ? ? ? app:layout_constraintTop_toTopOf="parent" /> ? ? <TextView ? ? ? ? android:id="@+id/tv_date_start" ? ? ? ? android:layout_width="300dp" ? ? ? ? android:layout_height="72dp" ? ? ? ? android:layout_marginLeft="31dp" ? ? ? ? android:background="@drawable/style_history_time" ? ? ? ? android:gravity="center" ? ? ? ? android:hint="請(qǐng)選擇開始日期" ? ? ? ? android:textSize="40sp" ? ? ? ? app:layout_constraintBottom_toBottomOf="@id/tv_history_default" ? ? ? ? app:layout_constraintLeft_toRightOf="@id/tv_history_default" ? ? ? ? app:layout_constraintTop_toTopOf="@id/tv_history_default" /> ? ? <TextView ? ? ? ? android:id="@+id/tv_default_zhi" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_marginLeft="54dp" ? ? ? ? android:text="至" ? ? ? ? android:textColor="@color/white" ? ? ? ? android:textSize="36sp" ? ? ? ? app:layout_constraintLeft_toRightOf="@id/tv_date_start" ? ? ? ? app:layout_constraintTop_toTopOf="@id/tv_history_default" /> ? ? <TextView ? ? ? ? android:id="@+id/tv_date_end" ? ? ? ? android:layout_width="300dp" ? ? ? ? android:layout_height="72dp" ? ? ? ? android:layout_marginLeft="54dp" ? ? ? ? android:background="@drawable/style_history_time" ? ? ? ? android:gravity="center" ? ? ? ? android:hint="請(qǐng)選擇結(jié)束日期" ? ? ? ? android:textSize="40sp" ? ? ? ? app:layout_constraintLeft_toRightOf="@id/tv_default_zhi" ? ? ? ? app:layout_constraintTop_toTopOf="@id/tv_date_start" /> ? ? ? ?? </androidx.constraintlayout.widget.ConstraintLayout>
畫出來就這么一個(gè)效果,其實(shí)還挺容易的,這是默認(rèn)的樣子
二、界面畫完接下來就去實(shí)現(xiàn)了,我是在fragment中添加的,這個(gè)看你自己是在什么地方使用,只是改變上下文而已。
public class TimerFragment extends Fragment implements View.OnClickListener { ? ? private TimePickerView pvTime;//TimePickerView 時(shí)間選擇器 ? ? private String startTime; ? ? private String endTime; ? ? private TextView tvDateStart, tvDateEnd; ? ? @Override ? ? public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ? ? ? ? View view = inflater.inflate(R.layout.fragment_timer, container, false); ? ? ? ? tvDateStart = view.findViewById(R.id.tv_date_start); ? ? ? ? tvDateEnd = view.findViewById(R.id.tv_date_end); ? ? ? ? tvDateStart.setOnClickListener(this); ? ? ? ? tvDateEnd.setOnClickListener(this); ? ? ? ? selectDate(); ? ? ? ? return view; ? ? } ? ? //選擇起止時(shí)間 ? ? private void selectDate() { ? ? ? ? //控制時(shí)間范圍(如果不設(shè)置范圍,則使用默認(rèn)時(shí)間1900-2100年,此段代碼可注釋) ? ? ? ? //因?yàn)橄到y(tǒng)Calendar的月份是從0-11的,所以如果是調(diào)用Calendar的set方法來設(shè)置時(shí)間,月份的范圍也要是從0-11 ? ? ? ? Calendar selectedDate = Calendar.getInstance(); ? ? ? ? Calendar startDate = Calendar.getInstance(); ? ? ? ? startDate.set(2020, 0, 1); ? ? ? ? Calendar endDate = Calendar.getInstance(); ? ? ? ? endDate.set(2040, 11, 31); ? ? ? ? //時(shí)間選擇器 ? ? ? ? pvTime = new TimePickerView.Builder(getActivity(), new TimePickerView.OnTimeSelectListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onTimeSelect(Date date, View v) {//選中事件回調(diào) ? ? ? ? ? ? ? ? // 這里回調(diào)的v,就是show()方法里面所添加的 View 參數(shù),如果show的時(shí)候沒有添加參數(shù),v則為null ? ? ? ? ? ? ? ? TextView tv = (TextView) v; ? ? ? ? ? ? ? ? if (tv == tvDateStart) { ? ? ? ? ? ? ? ? ? ? startTime = getTimes(date); ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? endTime = getTimes(date); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? //startTime 就為開始時(shí)間,endTime為結(jié)束時(shí)間 ? ? ? ? ? ? ? ? Log.e("TAG", "開始: " + startTime + " 結(jié)束為" + endTime); ? ? ? ? ? ? ? ? tv.setText(getTimes(date)); ? ? ? ? ? ? } ? ? ? ? }) ? ? ? ? ? ? ? ? //年月日時(shí)分秒的顯示與否,不設(shè)置則默認(rèn)全部顯示,這里可自行定制,true顯示,false不顯示 ? ? ? ? ? ? ? ? .setType(new boolean[]{true, true, false, false, false, false}) ? ? ? ? ? ? ? ? .setLabel(" 年", "月", "日", "時(shí)", "分", "") ? ? ? ? ? ? ? ? .isCenterLabel(true) ? ? ? ? ? ? ? ? .setDividerColor(Color.DKGRAY) ? ? ? ? ? ? ? ? .setContentSize(50) ? ? ? ? ? ? ? ? .setDate(selectedDate) ? ? ? ? ? ? ? ? .setRangDate(startDate, endDate) ? ? ? ? ? ? ? ? .setDecorView(null) ? ? ? ? ? ? ? ? .build(); ? ? } ? ? @Override ? ? public void onClick(View v) { ? ? ? ? switch (v.getId()) { ? ? ? ? ? ? case R.id.tv_date_start: ? ? ? ? ? ? ? ? //點(diǎn)擊組件的點(diǎn)擊事件 ? ? ? ? ? ? ? ? pvTime.show(tvDateStart); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case R.id.tv_date_end: ? ? ? ? ? ? ? ? pvTime.show(tvDateEnd); ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? } ? public String getTimes(Date date) {//可根據(jù)需要自行格式化數(shù)據(jù)顯示 ? ? ? ? SimpleDateFormat format = new SimpleDateFormat("yyyy-MM"); ? ? ? ? return format.format(date); ? ? } }
就這樣,時(shí)間選擇器就做完啦!?。∠聢D是點(diǎn)擊時(shí)間,從底部劃出的框框!
下圖即為選擇時(shí)間后的樣子
然后你就可以通過選擇到的起止時(shí)間,進(jìn)行數(shù)據(jù)的篩選啦?。?!是不是挺簡單的!
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決Android Studio突然不顯示logcat日志的問題
這篇文章主要介紹了解決Android Studio突然不顯示logcat日志的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04Android webview注入JS代碼 修改網(wǎng)頁內(nèi)容操作
這篇文章主要介紹了Android webview注入JS代碼 修改網(wǎng)頁內(nèi)容操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android Framework Application Framework層簡單介紹
這篇文章主要介紹了 Android Framework Application Framework層簡單介紹的相關(guān)資料,需要的朋友可以參考下2016-11-11Android Webview添加網(wǎng)頁加載進(jìn)度條實(shí)例詳解
這篇文章主要介紹了Android Webview添加網(wǎng)頁加載進(jìn)度條實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2016-01-01Android之日期及時(shí)間選擇對(duì)話框用法實(shí)例分析
這篇文章主要介紹了Android之日期及時(shí)間選擇對(duì)話框用法,以實(shí)例形式較為詳細(xì)的分析了Android創(chuàng)建日期及時(shí)間選擇對(duì)話框的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09android如何取得本地通訊錄的頭像的原圖的實(shí)現(xiàn)代碼
這篇文章主要介紹了android如何取得本地通訊錄的頭像的原圖的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12Kotlin編程基礎(chǔ)數(shù)據(jù)類型示例詳解
這篇文章主要為大家介紹了Kotlin編程基礎(chǔ)數(shù)據(jù)類型示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08