我想知道我是否可以为我的Android应用程序实现材料设计datepickerdialog
代码:
import java.util.Calendar;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;
import android.widget.EditText;
public class SelectDateFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
EditText mEdit;
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, yy, mm, dd);
}
@Override
public void onDateSet(DatePicker view, int yy, int mm, int dd) {
populateSetDate(yy, mm + 1, dd);
}
public void populateSetDate(int year, int month, int day) {
mEdit = (EditText) getActivity().findViewById(R.id.date);
mEdit.setText(day + "/" + month + "/" + year);
}
}
简单地说,我不想使用第三方库。我只是想知道它是否可以使用android支持库以及如何完成。
答案 0 :(得分:6)
https://github.com/wdullaer/MaterialDateTimePicker
一个好的材料日期选择器库。 易于使用。
只需将compile'com.wdullaer:materialdatetimepicker:1.4.2'添加到您的应用:build.gradle
例如:日期选择器对话框
// Show a datepicker when the dateButton is clicked
dateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar now = Calendar.getInstance();
DatePickerDialog dpd = DatePickerDialog.newInstance(
MainActivity.this,
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
);
dpd.setThemeDark(modeDarkDate.isChecked());
dpd.vibrate(vibrateDate.isChecked());
dpd.show(getFragmentManager(), "Datepickerdialog");
}
});
并从其implements方法'onDateSet'函数
中获取您选择的日期@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
String date = "You picked the following date: "+dayOfMonth+"/"+(++monthOfYear)+"/"+year;
dateTextView.setText(date);
}
答案 1 :(得分:3)