Java 同一对话框片段中的时间和日期选择器
我环顾四周,但我就是找不到一个好方法来制作一个如下图所示的时间和日期选择器。我猜时间选择器只是两个数字选择器,但我不知道在哪里可以找到剩下的一个 [日,日,月] 带有字符串的NumberPicker是一个选项,但是创建带有日期的字符串。有更简单/更好的方法吗Java 同一对话框片段中的时间和日期选择器,java,android,picker,Java,Android,Picker,我环顾四周,但我就是找不到一个好方法来制作一个如下图所示的时间和日期选择器。我猜时间选择器只是两个数字选择器,但我不知道在哪里可以找到剩下的一个 [日,日,月] 带有字符串的NumberPicker是一个选项,但是创建带有日期的字符串。有更简单/更好的方法吗 为您选择完美的日期时间选择器: 如果需要自定义日期时间选择器: 在这里,我可以帮你做些什么:你可以创建一个布局,包括一个日期选择器和一个时间选择器,在一个线性布局中,方向设置为垂直 自定义对话框.xml: <?xml versi
为您选择完美的日期时间选择器:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</DatePicker>
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TimePicker>
</LinearLayout>
要对与计时器选择器交互的用户做出反应,请执行以下操作:
TimePicker tp = (TimePicker)dialog.findViewById(R.id.timepicker1);
tp.setOnTimeChangedListener(myOnTimechangedListener);
若要在用户完成设置后从日期和时间选择器获取值,请在对话框中添加“确定”按钮,然后在用户按下“确定”时从日期和时间选择器读取日期和时间值。我用3个数字图标解决了这一难题。如果有人有同样的问题,这里是我的解决方案。设计不是最终的,但它接近我想要的
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dates = getDatesFromCalender();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialogfragment_time_date_picker, container);
datePicker = (NumberPicker) view.findViewById(R.id.datePicker);
hourPicker = (NumberPicker) view.findViewById(R.id.hourPicker);
minutePicker = (NumberPicker) view.findViewById(R.id.minutePicker);
doneButton = (Button) view.findViewById(R.id.doneButton);
currentTimeButton = (Button) view.findViewById(R.id.currentTimeButton);
datePicker.setMinValue(0);
datePicker.setMaxValue(dates.length - 1);
datePicker.setFormatter(new NumberPicker.Formatter() {
@Override
public String format(int value) {
return dates[value];
}
});
datePicker.setDisplayedValues(dates);
hourPicker.setMinValue(0);
hourPicker.setMaxValue(23);
hourPicker.setValue(rightNow.get(Calendar.HOUR_OF_DAY));
minutePicker.setMinValue(0);
minutePicker.setMaxValue(59);
minutePicker.setValue(rightNow.get(Calendar.MINUTE));
doneButton.setOnClickListener(this);
currentTimeButton.setOnClickListener(this);
getDialog().setTitle(R.string.choose_time);
return view;
}
private String[] getDatesFromCalender() {
Calendar c1 = Calendar.getInstance(TimeZone.getTimeZone("GMT+1"));
Calendar c2 = Calendar.getInstance(TimeZone.getTimeZone("GMT+1"));
List<String> dates = new ArrayList<String>();
DateFormat dateFormat = new SimpleDateFormat("EE, dd MMM", new Locale("sv", "SE"));
dates.add(dateFormat.format(c1.getTime()));
for (int i = 0; i < 60; i++) {
c1.add(Calendar.DATE, 1);
dates.add(dateFormat.format(c1.getTime()));
}
c2.add(Calendar.DATE, -60);
for (int i = 0; i < 60; i++) {
c2.add(Calendar.DATE, 1);
dates.add(dateFormat.format(c2.getTime()));
}
return dates.toArray(new String[dates.size() - 1]);
}
@Override
public void onClick(View v) {
Bundle args = new Bundle();
if (v.getId() == R.id.doneButton) {
//Handle selected time and date
} else if (v.getId() == R.id.currentTimeButton) {
}
}
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
dates=GetDatesFromCalendar();
}
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图=充气机。充气(R.layout.dialogfragment\u time\u date\u picker,container);
datePicker=(NumberPicker)view.findViewById(R.id.datePicker);
hourPicker=(NumberPicker)view.findViewById(R.id.hourPicker);
minutePicker=(NumberPicker)view.findViewById(R.id.minutePicker);
doneButton=(按钮)view.findViewById(R.id.doneButton);
currentTimeButton=(Button)view.findViewById(R.id.currentTimeButton);
datePicker.setMinValue(0);
datePicker.setMaxValue(dates.length-1);
datePicker.setFormatter(新的NumberPicker.Formatter(){
@凌驾
公共字符串格式(int值){
返回日期[值];
}
});
datePicker.setDisplayedValues(日期);
hourPicker.setMinValue(0);
hourPicker.setMaxValue(23);
hourPicker.setValue(rightNow.get(Calendar.HOUR/OF/u DAY));
minutePicker.setMinValue(0);
minutePicker.setMaxValue(59);
setValue(rightNow.get(Calendar.MINUTE));
doneButton.setOnClickListener(这个);
currentTimeButton.setOnClickListener(此);
getDialog().setTitle(R.string.choose_time);
返回视图;
}
私有字符串[]GetDatesFromCalendar(){
Calendar c1=Calendar.getInstance(TimeZone.getTimeZone(“GMT+1”));
calendarC2=Calendar.getInstance(TimeZone.getTimeZone(“GMT+1”));
列表日期=新建ArrayList();
DateFormat DateFormat=新的SimpleDateFormat(“EE,dd MMM”,新的语言环境(“sv”,“SE”);
add(dateFormat.format(c1.getTime());
对于(int i=0;i<60;i++){
c1.添加(日历日期,1);
add(dateFormat.format(c1.getTime());
}
c2.添加(日历日期,-60);
对于(int i=0;i<60;i++){
c2.添加(日历日期,1);
add(dateFormat.format(c2.getTime());
}
返回dates.toArray(新字符串[dates.size()-1]);
}
@凌驾
公共void onClick(视图v){
Bundle args=新Bundle();
if(v.getId()==R.id.doneButton){
//处理选定的时间和日期
}else if(v.getId()==R.id.currentTimeButton){
}
}
还有XML
<?xml version="1.0" encoding="utf-8"?>
谢谢。是的,那很好,但我的计划是让他们两个并排。我不想把事情复杂化。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical" android:layout_height="fill_parent" android:layout_width="match_parent"
>
<LinearLayout android:layout_weight="0.3" android:layout_height="fill_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/datePicker"/>
<NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/hourPicker"/>
<NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/minutePicker"/>
</LinearLayout>
</LinearLayout>
<LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/currentTime"
android:id="@+id/currentTimeButton"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/done"
android:id="@+id/doneButton"/>
</LinearLayout>
</LinearLayout>