下面我给你详细讲解“Android自定义DataTimePicker实例代码(日期选择器)”的完整攻略。首先,我们来看一下这个自定义DataTimePicker实例的代码:
1. 导入依赖项
dependencies {
implementation 'com.wdullaer:materialdatetimepicker:4.2.3'
}
2. 添加控件到布局中
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_date_picker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">
<EditText
android:id="@+id/edit_text_date_picker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:hint="选择日期"/>
<Button
android:id="@+id/btn_date_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择日期"/>
</RelativeLayout>
3. 初始化DatePickerDialog并显示
public class MainActivity extends AppCompatActivity {
private DatePickerDialog.OnDateSetListener mDateSetListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editTextDatePicker = findViewById(R.id.edit_text_date_picker);
Button buttonDatePicker = findViewById(R.id.btn_date_picker);
mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePickerDialog datePickerDialog, int year, int month, int day) {
String date = String.format(Locale.getDefault(), "%04d-%02d-%02d", year, month + 1, day);
editTextDatePicker.setText(date);
}
};
buttonDatePicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar now = Calendar.getInstance();
DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(
mDateSetListener,
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
);
datePickerDialog.show(getSupportFragmentManager(), "datePickerDialog");
}
});
}
}
以上就是Android自定义DataTimePicker实例代码的完整攻略。值得注意的是,该示例中使用了开源库“MaterialDateTimePicker”,因此在导入依赖项时需要保证网络正常连接。
下面再给你两个示例说明:
示例1
需求:需要给用户提供一个日期选择器,使其可以选择具体的日期。
步骤:
- 在布局文件中添加日期选择器的控件;
- 在Activity的onCreate()方法中通过findViewById()方法获取控件对象;
- 在按钮的监听事件中初始化DatePickerDialog并显示。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_date_picker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">
<EditText
android:id="@+id/edit_text_date_picker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:hint="选择日期"/>
<Button
android:id="@+id/btn_date_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择日期"/>
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
private DatePickerDialog.OnDateSetListener mDateSetListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editTextDatePicker = findViewById(R.id.edit_text_date_picker);
Button buttonDatePicker = findViewById(R.id.btn_date_picker);
mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePickerDialog datePickerDialog, int year, int month, int day) {
String date = String.format(Locale.getDefault(), "%04d-%02d-%02d", year, month + 1, day);
editTextDatePicker.setText(date);
}
};
buttonDatePicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar now = Calendar.getInstance();
DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(
mDateSetListener,
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
);
datePickerDialog.show(getSupportFragmentManager(), "datePickerDialog");
}
});
}
}
示例2
需求:需要给用户提供一个日期选择器,使其可以选择多个不连续的日期。
步骤:
- 在布局文件中添加日期选择器的控件;
- 在Activity的onCreate()方法中通过findViewById()方法获取控件对象;
- 在按钮的监听事件中循环初始化DatePickerDialog并显示。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_date_picker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">
<EditText
android:id="@+id/edit_text_date_picker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:hint="选择日期"/>
<Button
android:id="@+id/btn_date_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择日期"/>
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
private DatePickerDialog.OnDateSetListener mDateSetListener;
private List<String> mDateList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editTextDatePicker = findViewById(R.id.edit_text_date_picker);
Button buttonDatePicker = findViewById(R.id.btn_date_picker);
mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePickerDialog datePickerDialog, int year, int month, int day) {
String date = String.format(Locale.getDefault(), "%04d-%02d-%02d", year, month + 1, day);
mDateList.add(date);
editTextDatePicker.setText(mDateList.toString());
}
};
buttonDatePicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar now = Calendar.getInstance();
mDateList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(
mDateSetListener,
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
);
datePickerDialog.show(getSupportFragmentManager(), "datePickerDialog");
}
}
});
}
}
以上就是两个示例说明,希望能有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Android自定义DataTimePicker实例代码(日期选择器) - Python技术站