Android实现双时间选择器

一.、设计图

Android实现双时间选择器

 

二、实现代码:

1. 布局文件代码(dialog_time_picker.xml)

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_container_timepicker"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="visible"
    android:background="@color/design_default_color_error">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@color/white">
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="@dimen/dimen_16dp">
            <TextView
                android:id="@+id/tv_cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="@dimen/dimen_14dp"
                android:text="取消"
                android:layout_alignParentLeft="true"
                android:textColor="@color/color_666666"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="选择时间"
                android:layout_centerHorizontal="true"
                android:textSize="@dimen/dimen_18dp"
                android:textColor="@color/color_333333"/>
            <TextView
                android:id="@+id/tv_confirm"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="@dimen/dimen_14dp"
                android:text="确定"
                android:layout_alignParentRight="true"
                android:textColor="#FE6058"/>
        </RelativeLayout>

     
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:orientation="horizontal"
            android:gravity="center">
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical"
                android:gravity="center">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="@dimen/dimen_14dp"
                    android:text="开始时间"
                    android:textColor="@color/color_666666"/>
                <TimePicker
                    android:id="@+id/timepicker_start"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#ffffff"
                    android:timePickerMode="spinner"
                    />
            </LinearLayout>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="至"/>
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical"
                android:gravity="center">
                <TextView
                    android:textSize="@dimen/dimen_14dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="结束时间"
                    android:textColor="@color/color_666666"/>
                <TimePicker
                    android:id="@+id/timepicker_end"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="#ffffff"
                    android:timePickerMode="spinner"
                    />
            </LinearLayout>

        </LinearLayout>
    </LinearLayout>


</RelativeLayout>

 2. JAVA代码:

 public void setTimePicker(){
        bottomSheetDialog =new BottomSheetDialog(getContext());
        View dialogView= LayoutInflater.from(getContext())
                .inflate(R.layout.dialog_time_picker,null);
        TimePicker startTimePicker = dialogView.findViewById(R.id.timepicker_start);
        TimePicker endTimePicker = dialogView.findViewById(R.id.timepicker_end);
        TextView tvCancel = dialogView.findViewById(R.id.tv_cancel);
        TextView tvConfirm = dialogView.findViewById(R.id.tv_confirm);
        startTimePicker.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);  //设置点击事件不弹键盘
        startTimePicker.setIs24HourView(true);   //设置时间显示为24小时
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            startTimePicker.setHour(startHour);
            startTimePicker.setMinute(startMinute); //设置当前分(0-59)
        }

        startTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {  //获取当前选择的时间
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                startHour = hourOfDay;
                startMinute = minute;
            }
        });
        endTimePicker.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);  //设置点击事件不弹键盘
        endTimePicker.setIs24HourView(true);   //设置时间显示为24小时
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            endTimePicker.setHour(endHour);
            endTimePicker.setMinute(endMinute); //设置当前分(0-59)
        }
        endTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {  //获取当前选择的时间
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                endHour = hourOfDay;
                endMinute = minute;
            }
        });

        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomSheetDialog.dismiss();
            }
        });

        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (startHour > endHour || (startHour == endHour && startMinute >= endMinute) ){
                    ToastUtils.showToast(getContext(),"结束时间小于开始时间,请重新选择!");
                    return;
                }
                Log.e("开始时间:",startHour +":"+startMinute );
                Log.e("结束时间:",endHour +":"+endMinute);
                bottomSheetDialog.dismiss();
            }
        });

        bottomSheetDialog.setContentView(dialogView);
    }
            

3. 调用方法:

 if(bottomSheetDialog != null)  bottomSheetDialog.show();

  

 

Android实现双时间选择器

上一篇:13.gitlab搭建仓库及联通Jenkins


下一篇:WPF 项目中的App.config配置信息的读写操作