前言:
1.android开发中时间、日期、地址等滚轮选择器很常见,很多App会使用这种效果,收集了一下资料,于是自己做了一个简单封装,方便使用,感谢各位大佬的资料,效果如图:
2.使用方法代码如下:
public class MainActivity extends AppCompatActivity {private Dialog dateDialog, timeDialog, chooseDialog;private TextView mTextView;private List<String> list = new ArrayList<>();private ChooseAddressWheel chooseAddressWheel;private String address;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();initWheel();initData();}
/*** 初始化控件*/private void initView() {mTextView = findViewById(R.id.textView);String[] data = getResources().getStringArray(R.array.list);list.addAll(Arrays.asList(data));}/*** 初始化地址*/private void initWheel() {chooseAddressWheel = new ChooseAddressWheel(this);chooseAddressWheel.setOnAddressChangeListener(new OnAddressChangeListener() {@Overridepublic void onAddressChange(String province, String city, String district) {address = (province + " " + city + " " + district);mTextView.setText(address);}});}/*** 初始化数据*/private void initData() {String address = Utils.readAssert(this, "address.txt");AddressModel model = JsonUtil.parseJson(address, AddressModel.class);if (model != null) {AddressDetailsEntity data = model.Result;if (data == null) return;if (data.ProvinceItems != null && data.ProvinceItems.Province != null) {chooseAddressWheel.setProvince(data.ProvinceItems.Province);chooseAddressWheel.defaultValue(data.Province, data.City, data.Area);}}}
/*** 选择其他* @param view*/public void btnOther(View view) {showChooseDialog(list);}/*** 选择时间* @param view*/public void btnTime(View view) {showTimePick();}/*** 选择日期* @param view*/public void btnDate(View view) {showDateDialog(DateUtil.getDateForString("2000-02-14"));}/*** 选择地址* @param view*/public void btnAddress(View view) {chooseAddressWheel.show(view);}/*** 显示其他列表*/private void showChooseDialog(List<String> mlist) {DataPickerDialog.Builder builder = new DataPickerDialog.Builder(this);chooseDialog = builder.setData(mlist).setSelection(1).setTitle("取消").setOnDataSelectedListener(new DataPickerDialog.OnDataSelectedListener() {@Overridepublic void onDataSelected(String itemValue, int position) {mTextView.setText(itemValue);}@Overridepublic void onCancel() {}}).create();chooseDialog.show();}/*** 显示日期* @param date*/private void showDateDialog(List<Integer> date) {DatePickerDialog.Builder builder = new DatePickerDialog.Builder(this);builder.setOnDateSelectedListener(new DatePickerDialog.OnDateSelectedListener() {@Overridepublic void onDateSelected(int[] dates) {mTextView.setText(String.format("%d-%s-%s", dates[0], dates[1] > 9 ? dates[1] : ("0" + dates[1]), dates[2] > 9 ? dates[2] : ("0" + dates[2])));}@Overridepublic void onCancel() {}}).setSelectYear(date.get(0) - 1).setSelectMonth(date.get(1) - 1).setSelectDay(date.get(2) - 1);builder.setMaxYear(DateUtil.getYear());builder.setMaxMonth(DateUtil.getDateForString(DateUtil.getToday()).get(1));builder.setMaxDay(DateUtil.getDateForString(DateUtil.getToday()).get(2));dateDialog = builder.create();dateDialog.show();}/*** 显示时间*/private void showTimePick() {if (timeDialog == null) {TimePickerDialog.Builder builder = new TimePickerDialog.Builder(this);timeDialog = builder.setOnTimeSelectedListener(new TimePickerDialog.OnTimeSelectedListener() {@Overridepublic void onTimeSelected(int[] times) {mTextView.setText(String.format("%d:%d", times[0], times[1]));}}).create();}timeDialog.show();}
3.布局代码如下:
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"android:padding="16dp"><TextViewandroid:id="@+id/textView"android:layout_width="match_parent"android:layout_height="40dp"app:layout_constraintTop_toTopOf="parent"android:text="显示选择内容"android:textSize="18sp"android:textColor="@color/white"android:gravity="center"android:background="@drawable/shape_bg"/><Buttonandroid:id="@+id/btn_time"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/time"app:layout_constraintTop_toBottomOf="@id/textView"android:layout_marginTop="10dp"android:textSize="18sp"android:textColor="@color/white"android:background="@drawable/shape_bg"android:onClick="btnTime"/><Buttonandroid:id="@+id/btn_date"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/date"android:textSize="18sp"android:textColor="@color/white"android:background="@drawable/shape_bg"app:layout_constraintTop_toBottomOf="@id/btn_time"android:layout_marginTop="10dp"android:onClick="btnDate"/><Buttonandroid:id="@+id/btn_address"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/address"android:textSize="18sp"android:textColor="@color/white"android:background="@drawable/shape_bg"app:layout_constraintTop_toBottomOf="@id/btn_date"android:layout_marginTop="10dp"android:onClick="btnAddress"/><Buttonandroid:id="@+id/btn_other"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/other"android:textSize="18sp"android:textColor="@color/white"android:background="@drawable/shape_bg"app:layout_constraintTop_toBottomOf="@id/btn_address"android:layout_marginTop="10dp"android:onClick="btnOther"/>
4.项目地址如下:
WheelPickerDemo: 省市区地址、时间、日期滚轮选择器 小菜鸡写得不好,欢迎大家给出指导意见
如果觉得《Android地址(省市区) 日期 时间滚轮选择器简单封装》对你有帮助,请点赞、收藏,并留下你的观点哦!