Android开发:安卓选择器类库,包括日期选择器、时间选择器、文件选择器、目录选择器等

项目中用到了时间选择器和单文本选择器,选择了这个AndroidPicker/WheelPicker库,感觉还不错,挺好用,把事例写一下。

github地址:https://github.com/gzu-liyujiang/AndroidPicker

一、引入依赖

第一步,在项目根目录下的build.gradle里加:

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
    }
}

第二步,在项目的app模块下的build.gradle里加: 滚轮选择器:

implementation('com.github.gzu-liyujiang.AndroidPicker:WheelPicker:1.5.6') {
    exclude group: 'com.android.support'
}

除了时间选择器,还有日期、城市、文件选择器等,如果有需要,可按上面github地址查找。

二、具体使用

调用:

 /**
     * 点击事件
     */
    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.tv_back:
                    finish();
                    break;
                case R.id.llCarNum:
                    onSinglePicker(llCarNum);
                    break;
                case R.id.llTime:
                    onYearMonthDayPicker(llTime);
                    break;
                default:
                    break;
            }
        }
    };

 

  /**
     * 时间选择器
     *
     * @param view 父控件
     */
    public void onYearMonthDayPicker(View view) {
        final DatePicker picker = new DatePicker(this);
        picker.setCanceledOnTouchOutside(true);
        picker.setUseWeight(true);
        picker.setTopPadding(ConvertUtils.toPx(this, 10));
        picker.setRangeEnd(2111, 1, 11);
        picker.setRangeStart(2000, 10, 1);
        picker.setSelectedItem(2019, 10, 1);
        picker.setResetWhileWheel(false);
        picker.setSubmitTextColor(getResources().getColor(R.color.blue_main));
        picker.setCancelTextColor(getResources().getColor(R.color.blue_main));
        picker.setTextColor(getResources().getColor(R.color.blue_main));
        picker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
            @Override
            public void onDatePicked(String year, String month, String day) {
//                ToastUtil.showToast(context, year + "-" + month + "-" + day);
//选中后的逻辑操作
                tvTime.setText(String.format("%s-%s-%s", year, month, day));
                loadData(tvTime.getText().toString(), carId);
            }
        });
        picker.setOnWheelListener(new DatePicker.OnWheelListener() {
            @Override
            public void onYearWheeled(int index, String year) {
                picker.setTitleText(year + "-" + picker.getSelectedMonth() + "-" + picker.getSelectedDay());
            }

            @Override
            public void onMonthWheeled(int index, String month) {
                picker.setTitleText(picker.getSelectedYear() + "-" + month + "-" + picker.getSelectedDay());
            }

            @Override
            public void onDayWheeled(int index, String day) {
                picker.setTitleText(picker.getSelectedYear() + "-" + picker.getSelectedMonth() + "-" + day);
            }
        });
        picker.show();
    }

 

 

/**
     * 车辆选择器
     * CarList.Cars 实体类
     * carsResource 数据源
     *
     * @param view 父控件
     */
    public void onSinglePicker(View view) {
        SinglePicker picker = new SinglePicker<>(this, carsResource);
        picker.setCanceledOnTouchOutside(false);
        picker.setSelectedIndex(1);
        picker.setCycleDisable(false);
        picker.setSubmitTextColor(getResources().getColor(R.color.blue_main));
        picker.setCancelTextColor(getResources().getColor(R.color.blue_main));
        picker.setTextColor(getResources().getColor(R.color.blue_main));
        picker.setOnItemPickListener(new SinglePicker.OnItemPickListener() {
            @Override
            public void onItemPicked(int index, CarList.Cars item) {
                carId = item.car_id;
                tvCarNum.setText(item.car_num);
                loadData(tvTime.getText().toString(), item.car_id);
            }
        });
        if (picker != null) {
            picker.show();
        }
    }

 

以上就是具体的使用方法了,更多信息,请查看GitHub网站,地址在文章开头。 

 

Android开发:安卓选择器类库,包括日期选择器、时间选择器、文件选择器、目录选择器等_第1张图片

 

你可能感兴趣的:(工具类)