網站首頁 編程語言 正文
相信大家都有這樣的一個需求,選擇相應開始時間和結束時間,對數據進行篩選,下面就將使用TimePickerView實現這么一個功能。
一、先導入依賴
implementation "com.contrarywind:Android-PickerView:3.2.7"
二、在界面上畫出選擇時間的框框,這里大家就根據自己的UI畫就行,我個人用的是約束性布局
<?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="時間" ? ? ? ? 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="請選擇開始日期" ? ? ? ? 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="請選擇結束日期" ? ? ? ? android:textSize="40sp" ? ? ? ? app:layout_constraintLeft_toRightOf="@id/tv_default_zhi" ? ? ? ? app:layout_constraintTop_toTopOf="@id/tv_date_start" /> ? ? ? ?? </androidx.constraintlayout.widget.ConstraintLayout>
畫出來就這么一個效果,其實還挺容易的,這是默認的樣子
二、界面畫完接下來就去實現了,我是在fragment中添加的,這個看你自己是在什么地方使用,只是改變上下文而已。
public class TimerFragment extends Fragment implements View.OnClickListener {
? ? private TimePickerView pvTime;//TimePickerView 時間選擇器
? ? 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;
? ? }
? ? //選擇起止時間
? ? private void selectDate() {
? ? ? ? //控制時間范圍(如果不設置范圍,則使用默認時間1900-2100年,此段代碼可注釋)
? ? ? ? //因為系統Calendar的月份是從0-11的,所以如果是調用Calendar的set方法來設置時間,月份的范圍也要是從0-11
? ? ? ? Calendar selectedDate = Calendar.getInstance();
? ? ? ? Calendar startDate = Calendar.getInstance();
? ? ? ? startDate.set(2020, 0, 1);
? ? ? ? Calendar endDate = Calendar.getInstance();
? ? ? ? endDate.set(2040, 11, 31);
? ? ? ? //時間選擇器
? ? ? ? pvTime = new TimePickerView.Builder(getActivity(), new TimePickerView.OnTimeSelectListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onTimeSelect(Date date, View v) {//選中事件回調
? ? ? ? ? ? ? ? // 這里回調的v,就是show()方法里面所添加的 View 參數,如果show的時候沒有添加參數,v則為null
? ? ? ? ? ? ? ? TextView tv = (TextView) v;
? ? ? ? ? ? ? ? if (tv == tvDateStart) {
? ? ? ? ? ? ? ? ? ? startTime = getTimes(date);
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? endTime = getTimes(date);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? //startTime 就為開始時間,endTime為結束時間
? ? ? ? ? ? ? ? Log.e("TAG", "開始: " + startTime + " 結束為" + endTime);
? ? ? ? ? ? ? ? tv.setText(getTimes(date));
? ? ? ? ? ? }
? ? ? ? })
? ? ? ? ? ? ? ? //年月日時分秒的顯示與否,不設置則默認全部顯示,這里可自行定制,true顯示,false不顯示
? ? ? ? ? ? ? ? .setType(new boolean[]{true, true, false, false, false, false})
? ? ? ? ? ? ? ? .setLabel(" 年", "月", "日", "時", "分", "")
? ? ? ? ? ? ? ? .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:
? ? ? ? ? ? ? ? //點擊組件的點擊事件
? ? ? ? ? ? ? ? pvTime.show(tvDateStart);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.tv_date_end:
? ? ? ? ? ? ? ? pvTime.show(tvDateEnd);
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? }
? public String getTimes(Date date) {//可根據需要自行格式化數據顯示
? ? ? ? SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
? ? ? ? return format.format(date);
? ? }
}
就這樣,時間選擇器就做完啦!!!下圖是點擊時間,從底部劃出的框框!
下圖即為選擇時間后的樣子
然后你就可以通過選擇到的起止時間,進行數據的篩選啦!!!是不是挺簡單的!
原文鏈接:https://blog.csdn.net/weixin_45552475/article/details/115300472
相關推薦
- 2022-06-18 android自定義滾動上下回彈scollView_Android
- 2022-07-13 Golang實現常見排序算法的示例代碼_Golang
- 2022-04-05 antd form操作表單元素以及獲取值方法,antd form獲取某個值
- 2022-05-05 Python+OpenCV實現角度測量的示例代碼_python
- 2022-07-04 Python三數之和的實現方式_python
- 2022-07-21 React中this指向問題
- 2023-05-23 Numpy數組轉置的實現_python
- 2022-11-02 Qt5.14.2使用虛擬鍵盤的關鍵代碼_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支