目前最新版本是 4.1.9。 GitHub:https://github.com/Bigkoo/Android-PickerView
简介
Android-PickerView 是一款仿 iOS 的 PickerView 控件,带有 3D 圆弧效果,并封装了时间选择和选项选择这两种选择器。
配置
在 app/build.gradle 中添加依赖
implementation
'com.contrarywind:Android-PickerView:4.1.9'
自定义各种属性
Calendar selectedDate
= Calendar
.getInstance();
Calendar startDate
= Calendar
.getInstance();
startDate
.set(2013,1,1);
Calendar endDate
= Calendar
.getInstance();
endDate
.set(2020,1,1);
pvTime
= new TimePickerBuilder(this, new TimePickerView.OnTimeSelectListener() {
@Override
public void onTimeSelect(Date date
,View v
) {
tvTime
.setText(getTime(date
));
}
})
.setType(new boolean[]{true, true, true, true, true, true})
.setCancelText("Cancel")
.setSubmitText("Sure")
.setContentSize(18)
.setTitleSize(20)
.setTitleText("Title")
.setOutSideCancelable(false)
.isCyclic(true)
.setTitleColor(Color
.BLACK
)
.setSubmitColor(Color
.BLUE
)
.setCancelColor(Color
.BLUE
)
.setTitleBgColor(0xFF666666)
.setBgColor(0xFF333333)
.setRange(calendar
.get(Calendar
.YEAR
) - 20, calendar
.get(Calendar
.YEAR
) + 20)
.setDate(selectedDate
)
.setRangDate(startDate
,endDate
)
.setLabel("年","月","日","时","分","秒")
.isDialog(true)
.build();
使用
activity 布局
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.PickViewActivity">
<include
android:id="@+id/include5"
layout="@layout/layout_toolbar" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="224dp"
android:text="时间选择器"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/button6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/include5"
app:layout_constraintVertical_bias="0.711" />
</androidx.constraintlayout.widget.ConstraintLayout>
dialog 布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#EEEEEE">
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#aaa" />
<TextView
android:id="@+id/iv_cancel"
android:layout_width="61dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:gravity="center"
android:layout_marginLeft="19dp"
android:text="取消"
android:textSize="16sp"
android:background="@drawable/shape_white_black_7bg"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择时间"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/black"
android:layout_centerInParent="true"/>
<TextView
android:id="@+id/tv_finish"
android:layout_width="61dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="19dp"
android:gravity="center"
android:text="确定"
android:background="@drawable/shape_yellow3_bg"
android:textColor="@color/black"
android:textSize="16sp" />
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#aaa" />
</RelativeLayout>
<LinearLayout
android:id="@+id/timepicker"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="horizontal">
<com.contrarywind.view.WheelView
android:id="@+id/year"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<com.contrarywind.view.WheelView
android:id="@+id/month"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.1" />
<com.contrarywind.view.WheelView
android:id="@+id/day"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.1" />
<com.contrarywind.view.WheelView
android:id="@+id/hour"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.1" />
<com.contrarywind.view.WheelView
android:id="@+id/min"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.1" />
<com.contrarywind.view.WheelView
android:id="@+id/second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.1" />
</LinearLayout>
</LinearLayout>
shape 文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/yellow" />
<corners android:radius="3dp" />
</shape>
Activity 类中使用
public class PickViewActivity extends BaseActivity {
@BindView(R
.id
.textView
)
TextView tvTime
;
private TimePickerView pvTime
;
@Override
protected int getLayoutId() {
return R
.layout
.activity_pick_view
;
}
@Override
protected void setToolbar() {
}
@Override
protected void initView() {
setNavTitle(R
.string
.select
);
initTimePicker();
}
@OnClick({R
.id
.button6
})
public void clicked(View view
) {
switch (view
.getId()) {
case R
.id
.button6
:
pvTime
.show();
break;
}
}
private void initTimePicker() {
Calendar startDate
= Calendar
.getInstance();
startDate
.set(2019, 1, 1);
Calendar endDate
= Calendar
.getInstance();
endDate
.set(2040, 1, 1);
pvTime
= new TimePickerBuilder(this, new OnTimeSelectListener() {
@Override
public void onTimeSelect(Date date
, View v
) {
tvTime
.setText(getTime(date
));
}
})
.setTimeSelectChangeListener(new OnTimeSelectChangeListener() {
@Override
public void onTimeSelectChanged(Date date
) {
Log
.i("pvTime", "onTimeSelectChanged");
}
})
.setType(new boolean[]{true, true, true, true, true, false})
.isDialog(true)
.addOnCancelClickListener(new View.OnClickListener() {
@Override
public void onClick(View view
) {
Log
.i("pvTime", "onCancelClickListener");
}
})
.setLayoutRes(R
.layout
.pickerview_custom_time
, new CustomListener() {
@Override
public void customLayout(View v
) {
final TextView tvSubmit
= (TextView
) v
.findViewById(R
.id
.tv_finish
);
TextView ivCancel
= (TextView
) v
.findViewById(R
.id
.iv_cancel
);
tvSubmit
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v
) {
pvTime
.returnData();
pvTime
.dismiss();
}
});
ivCancel
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v
) {
pvTime
.dismiss();
}
});
}
})
.setContentTextSize(18)
.setDividerColor(Color
.BLACK
)
.setRangDate(startDate
, endDate
)
.build();
Dialog mDialog
= pvTime
.getDialog();
if (mDialog
!= null
) {
FrameLayout
.LayoutParams params
= new FrameLayout.LayoutParams(
ViewGroup
.LayoutParams
.MATCH_PARENT
,
ViewGroup
.LayoutParams
.WRAP_CONTENT
,
Gravity
.BOTTOM
);
params
.leftMargin
= 0;
params
.rightMargin
= 0;
pvTime
.getDialogContainerLayout().setLayoutParams(params
);
Window dialogWindow
= mDialog
.getWindow();
if (dialogWindow
!= null
) {
dialogWindow
.setWindowAnimations(com
.bigkoo
.pickerview
.R
.style
.picker_view_slide_anim
);
dialogWindow
.setGravity(Gravity
.BOTTOM
);
dialogWindow
.setDimAmount(0.1f);
}
}
}
private String
getTime(Date date
) {
SimpleDateFormat format
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format
.format(date
);
}
}
转载请注明原文地址: https://ju.6miu.com/read-4123.html