Android 一个好看的时间控件
1.依赖包
compile 'com.wx.wheelview:wheelview:1.3.3'
2.在布局中使用
<LinearLayout
android:layout_width="60dp"
android:layout_height="60dp"
android:orientation="vertical">
<com.wx.wheelview.widget.WheelView
android:id="@+id/addtime_hour_wheelview"
android:layout_width="match_parent"
android:layout_height="57dp"
/>
<ImageView //时间下的指示条
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#ff5d5d"
/>
</LinearLayout>
3.代码中的使用
mAddtimeHourWheelview.setWheelAdapter(new ArrayWheelAdapter(this));
mAddtimeHourWheelview.setSkin(WheelView.Skin.None); //无边框
mAddtimeHourWheelview.setWheelSize(1); //只显示一行
final ArrayList<String> hours = createHours(); //获取到小时数据 可以改为分钟数据
mAddtimeHourWheelview.setWheelData(hours);
WheelView.WheelViewStyle style = new WheelView.WheelViewStyle(); //风格
style.selectedTextColor = Color.parseColor("#ff5d5d"); //文字颜色
style.textColor = Color.RED;
style.selectedTextSize = 26; //文字大小
mAddtimeHourWheelview.setStyle(style);
mAddtimeHourWheelview.setOnWheelItemSelectedListener(new WheelView.OnWheelItemSelectedListener() { //设置监听
@Override
public void onItemSelected(int position, Object o) {
String hoursString = hours.get(position);
mAddtimeStartTv.setText(hoursString + " : "); //获取到时间
}
});
数据源
//小时
private ArrayList<String> createHours() {
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < 24; i++) {
if (i < 10) {
list.add("0" + i);
} else {
list.add("" + i);
}
}
return list;
}
//分钟
private ArrayList<String> createMinutes() {
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < 60; i++) {
if (i < 10) {
list.add("0" + i);
} else {
list.add("" + i);
}
}
return list;
}