一、效果图
年月选择器.png二、思路
其实用的是'cn.aigestudio.wheelpicker:WheelPicker:1.1.3',可自行百度这个别人写的库,它还能实现很多功能
注:我这个是没联动效果的,它的库其他方法有
三、案例关键代码
xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@color/m_white">
<TextView
android:id="@+id/cancel"
android:layout_width="60dp"
android:layout_height="40dp"
android:gravity="center"
android:text="取消"
android:textColor="@color/color_666666"
android:textSize="@dimen/m_size_17"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/ok"
android:layout_width="60dp"
android:layout_height="40dp"
android:gravity="center"
android:text="确定"
android:textColor="@color/color_3C76FF"
android:textSize="@dimen/m_size_17"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/view_line"
style="@style/full_under_line"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/cancel" />
<com.aigestudio.wheelpicker.WheelPicker
android:id="@+id/mWheelPicker_1"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/mWheelPicker_2"
app:layout_constraintTop_toBottomOf="@id/view_line"
android:layout_marginLeft="30dp"
app:wheel_atmospheric="true"
app:wheel_curtain_color="@color/m_blue"
app:wheel_curved="true"
app:wheel_cyclic="true"
app:wheel_indicator_color="@color/m_red_one"
app:wheel_item_text_color="@color/color_919191"
app:wheel_item_text_size="@dimen/m_size_23"
app:wheel_selected_item_text_color="@color/color_000000" />
<com.aigestudio.wheelpicker.WheelPicker
android:id="@+id/mWheelPicker_2"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@id/mWheelPicker_1"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@id/mWheelPicker_1"
android:layout_marginRight="30dp"
app:wheel_atmospheric="true"
app:wheel_curtain_color="@color/m_blue"
app:wheel_curved="true"
app:wheel_cyclic="true"
app:wheel_indicator_color="@color/m_red_one"
app:wheel_indicator_size="@dimen/m_size_24"
app:wheel_item_text_color="@color/color_919191"
app:wheel_item_text_size="@dimen/m_size_23"
app:wheel_selected_item_text_color="@color/color_000000" />
</android.support.constraint.ConstraintLayout>
java代码
val CEOYEAR = arrayListOf<String>("2020","2021","2022","2023","2024","2025","2026","2027","2028","2029","2030")
val CEOMONTH= arrayListOf<String>("1","2","3","4","5","6","7","8","9","10","11","12")
val STARTYEAR = arrayListOf<String>("2017","2018","2019","2020","2021","2022","2023","2024","2025","2026","2027")
/**
* @desc : 两个滚动器
* @author : congge on 2020/2/20 17:20
**/
fun showTwoWheelPicker(context:Context,data1: MutableList<String>,data2: MutableList<String>,twoWheelListener: TwoWheelListener?){
var dialog = getDialog(context)
val window = dialog.window
window.setGravity(Gravity.BOTTOM)
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
window.setContentView(R.layout.dialog_two_wheel)
val wv1 = window.findViewById<WheelPicker>(R.id.mWheelPicker_1)
val wv2 = window.findViewById<WheelPicker>(R.id.mWheelPicker_2)
wv1.data = data1
wv2.data = data2
//取消
window.findViewById<View>(R.id.cancel).setOnClickListener {
dialog.dismiss()
}
//确定
window.findViewById<View>(R.id.ok).setOnClickListener {
dialog.dismiss()
twoWheelListener?.onOk(data1[wv1.currentItemPosition],data2[wv2.currentItemPosition])
}
}
interface TwoWheelListener {
fun onOk(str1: String, str2: String)
}
private fun getDialog(context:Context):Dialog{
return AlertDialog.Builder(context, R.style.mydialog).setCancelable(false).show()
}
四、总结
有更多需求的还是去阅读第三方库
网友评论