java滚动式时间控件,GitHub - duanhong169/PickerView: Android滚动选择器(省市区联动选择、日期选择、时间选择)...

PickerView 68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f6475616e686f6e673136392f5069636b6572566965772e7376673f7374796c653d736f6369616c68747470733a2f2f696d672e736869656c64732e696f2f62616467652f706c6174666f726d2d616e64726f69642d627269676874677265656e2e73766768747470733a2f2f696d672e736869656c64732e696f2f62616467652f416e64726f6964253230417273656e616c2d5069636b6572566965772d677265656e2e7376673f7374796c653d666c617468747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d417061636865253230322d677265656e2e73766768747470733a2f2f6275696c642e61707063656e7465722e6d732f76302e312f617070732f38326135313132382d616137312d346335362d393131342d3431373063353163623839392f6272616e636865732f6d61737465722f6261646765

Android滚动选择器

0dd01bbd0b18572898cee2bc493d8084.giff223aa11b631e9db609703df98b03940.gif

9dd899e5d87eacb39a8e65877723a22a.gifc5dda368fbb48764e85c0b238d966d1e.gif

228fa139256f9633de20157bf2799e41.png944ec74b4700a6756ef3e850c25ce33b.png

使用方法

1. 添加依赖

注:${latestVersion}请替换为当前最新版本号,见releases。

gradle:

implementation 'com.github.duanhong169:picker-view:${latestVersion}'

maven:

com.github.duanhong169

picker-view

${latestVersion}

pom

2. 集成到项目中

2.1 集成PickerView

添加到layout文件中:

android:id="@+id/pickerView"

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

使用列表作为数据源

List items = new ArrayList<>();

for (int i = 0; i < 42; i++) {

items.add(new Item("Item " + i));

}

pickerView.setItems(Item.sampleItems(), item -> textView.setText(item.getText()));

实现Adapter作为数据源

配置数据源:

PickerView.Adapter adapter = new PickerView.Adapter() {

@Override

public int getItemCount() {

return 42;

}

@Override

public String getText(int index) {

return "Item " + index;

}

};

pickerView.setAdapter(adapter);

监听选择事件:

pickerView.setOnSelectedItemChangedListener((pickerView, previousPosition, selectedItemPosition) ->

textView.setText(pickerView.getAdapter().getText(selectedItemPosition)));

2.2 集成DivisionPickerView

添加到layout文件中:

android:id="@+id/divisionPicker"

android:layout_width="match_parent"

android:layout_height="wrap_content"

app:preferredMaxOffsetItemCount="4"

android:background="#e7e7e7"/>

构建省市区数据源:

设置数据源并监听选择事件

final List divisions = Divisions.get(this);

divisionPicker.setDivisions(divisions);

divisionPicker.setOnSelectedDateChangedListener(division -> textView.setText(Division.Helper.getCanonicalName(division)));

2.3 集成DateTimePickerView

添加到layout文件中:

android:id="@+id/datePickerView"

android:layout_width="match_parent"

android:layout_height="wrap_content"

app:preferredMaxOffsetItemCount="3"

app:textSize="18sp"

app:type="dateTime"

app:minutesInterval="fifteen"

app:curved="true"

android:background="#e7e7e7"/>

设置初始日期:

dateTimePickerView.setStartDate(Calendar.getInstance());

// 注意:月份是从0开始计数的

dateTimePickerView.setSelectedDate(new GregorianCalendar(2017, 6, 27, 21, 30));

监听选择事件:

dateTimePickerView.setOnSelectedDateChangedListener(new DateTimePickerView.OnSelectedDateChangedListener() {

@Override

public void onSelectedDateChanged(Calendar date) {

int year = date.get(Calendar.YEAR);

int month = date.get(Calendar.MONTH);

int dayOfMonth = date.get(Calendar.DAY_OF_MONTH);

int hour = date.get(Calendar.HOUR_OF_DAY);

int minute = date.get(Calendar.MINUTE);

String dateString = String.format(Locale.getDefault(), "%d年%02d月%02d日%02d时%02d分", year, month + 1, dayOfMonth, hour, minute);

textView.setText(dateString);

Log.d(TAG, "new date: " + dateString);

}

});

更详细的使用方法请参见示例。

License

Copyright 2018 Hong Duan

Licensed under the Apache License, Version 2.0 (the "License");

you may not use this file except in compliance with the License.

You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software

distributed under the License is distributed on an "AS IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and

limitations under the License.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值