作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
datepickerLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
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);
DatePickerDialog datePickerDialog = new DatePickerDialog(getContext(),new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
datePickerDialog.getTouchables().get(0).performClick();
String strdate=year+"-"+(monthOfYear+1)+"-"+dayOfMonth;
}
}, yy, mm, dd);
datePickerDialog.getDatePicker().setMinDate(calendar.getTimeInMillis());
datePickerDialog.show();
}
});
我想在选择要选择日期的年份后单击按钮时在日期选择器中显示年份。
最佳答案
你可以简单地使用下面的代码实现这一点
DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
dialog.getDatePicker().getTouchables().get( 0 ).performClick();
使用下面的类打开对话框
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
OnDateOfBirthSelection registrationIIFragment;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
DatePickerDialog abc = new DatePickerDialog(getActivity(), this, year, month, day);
//18 Years =568,025,136,000 Milliseconds
double diff = System.currentTimeMillis() - 568025136000.0;
abc.getDatePicker().setMaxDate((long) diff);
abc.getDatePicker().getTouchables().get( 0 ).performClick();
return abc;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
// tv1.setText("Year: "+view.getYear()+" Month: "+view.getMonth()+" Day: "+view.getDayOfMonth());
registrationIIFragment.onDateOfBirthSelected(view.getDayOfMonth() + " - " + (view.getMonth() + 1) + " - " + view.getYear());
}
public void setCallbackListener(OnDateOfBirthSelection registrationIIFragment) {
this.registrationIIFragment = registrationIIFragment;
}
public interface OnDateOfBirthSelection {
public void onDateOfBirthSelected(String s);
} }
在按钮中单击创建上面的类实例并调用 show 方法。
DatePickerFragment newFragment = new DatePickerFragment();
newFragment.setCallbackListener(this);
newFragment.show(getSupportFragmentManager(), "datePicker");
上面的代码将显示输出如下图
希望对你有帮助谢谢
关于java - Android 如何在点击按钮时首先在日期选择器中显示年份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51356814/
我是一名优秀的程序员,十分优秀!