android开发中时间、日期、地址等滚轮选择器很常见,很多App会使用这种效果,收集了一下资料,于是自己做了一个简单封装,方便使用,感谢各位大佬的资料,效果如图:
使用方法代码如下:
public class MainActivity extends AppCompatActivity {
private Dialog dateDialog, timeDialog, chooseDialog;
private TextView mTextView;
private List list = new ArrayList<>();
private ChooseAddressWheel chooseAddressWheel;
private String address;
@Override
protected 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() {
@Override
public 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 mlist) {
DataPickerDialog.Builder builder = new DataPickerDialog.Builder(this);
chooseDialog = builder.setData(mlist).setSelection(1).setTitle("取消")
.setOnDataSelectedListener(new DataPickerDialog.OnDataSelectedListener() {
@Override
public void onDataSelected(String itemValue, int position) {
mTextView.setText(itemValue);
}
@Override
public void onCancel() {
}
}).create();
chooseDialog.show();
}
/**
* 显示日期
* @param date
*/
private void showDateDialog(List date) {
DatePickerDialog.Builder builder = new DatePickerDialog.Builder(this);
builder.setOnDateSelectedListener(new DatePickerDialog.OnDateSelectedListener() {
@Override
public 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])));
}
@Override
public 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() {
@Override
public void onTimeSelected(int[] times) {
mTextView.setText(String.format("%d:%d", times[0], times[1]));
}
}).create();
}
timeDialog.show();
}
布局代码如下:
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=".MainActivity"
android:padding="16dp">
android: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"/>
android: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"/>
android: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"/>
android: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"/>
android: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"/>
项目地址:https://gitee.com/jackning_admin/WheelPickerDemo
小菜鸡写得不好,欢迎大家给出指导意见