gpt4 book ai didi

android - TimePicker 不根据用户输入更新

转载 作者:行者123 更新时间:2023-11-29 16:07:47 25 4
gpt4 key购买 nike

我有一个使用自定义日期时间选择器的应用程序。当用户选择一个时间并单击 SET 按钮时,它会在微调器上设置时间。我的应用会检查时间,如果它根据某些标准不合适,我会再次显示微调器。

问题是,当用户第二次或随后更改微调器上的时间时,初始时间仍然设置。如何从反射(reflect)在 dateTime 对象中的微调器获取时间?

我试过调用 TPic.getCurrentHour 等,也试过在 TPic 对象上调用 refreshDrawableState,但它仍然不起作用。有什么想法吗?

Button ShowDTPicker;
Button ShowDatePicker;
Button ShowTimePicker;
Button Set;
Button ReSet;
Button Cancel;
DatePicker DPic;
TimePicker TPic;
TextView Date;
private ViewSwitcher switcher;
static final int DATE_TIME_DIALOG_ID = 999;
Dialog dialog;

public void showDateTimeDialog(){


//final Calendar c = Calendar.getInstance();

final SimpleDateFormat dfDate = new SimpleDateFormat("dd-MMM-yyyy HH:mm");



dialog = new Dialog(NfcscannerActivity.this);
dialog.setContentView(R.layout.datetimepicker);
switcher = (ViewSwitcher) dialog.findViewById(R.id.DateTimePickerVS);

TPic = (TimePicker) dialog.findViewById(R.id.TimePicker);
DPic = (DatePicker) dialog.findViewById(R.id.DatePicker);
TPic.setIs24HourView(true);



ShowDatePicker = ((Button) dialog.findViewById(R.id.SwitchToDate));
ShowDatePicker.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
switcher.showPrevious();
ShowDatePicker.setEnabled(false);
ShowTimePicker.setEnabled(true);
}
});
ShowTimePicker = ((Button) dialog.findViewById(R.id.SwitchToTime));
ShowTimePicker.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
switcher.showNext();
ShowDatePicker.setEnabled(true);
ShowTimePicker.setEnabled(false);
}
});

Set = ((Button) dialog.findViewById(R.id.SetDateTime));
Set.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {

Calendar c = Calendar.getInstance();
c.set(DPic.getYear(), DPic.getMonth(), DPic.getDayOfMonth(), TPic.getCurrentHour(), TPic.getCurrentMinute());
Log.e(TAG, "TPic hour and minute = " + TPic.getCurrentHour() + " " + TPic.getCurrentMinute());
timeSetOnSpinner = new DateTime(c);


................
................
...............
//check if time is suitable, if not call showDateTimeDialog() again


}
});
ReSet = ((Button) dialog.findViewById(R.id.ResetDateTime));
ReSet.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Calendar c = Calendar.getInstance();
DPic.updateDate(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
TPic.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));
TPic.setCurrentMinute(c.get(Calendar.MINUTE));
}
});
Cancel = ((Button) dialog.findViewById(R.id.CancelDialog));
Cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {

dialog.cancel();



}
});

dialog.setTitle("Please enter time of last logout");

try{
ReSet.performClick();
dialog.show();
}catch(Exception e){
//ignore
}
Log.e(TAG,"Just executed dialog.show() and at the end of showDateTimeDialog method");
}//showDateTimeDialog()

@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_TIME_DIALOG_ID:

return dialog;
}
return null;
}

.

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout

android:padding="5dip"
android:fitsSystemWindows="true"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/DateTimePicker"
xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout

android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/ViewSwitchButtons"
android:layout_marginBottom="5dip">

<Button
android:layout_height="wrap_content"
android:layout_width="0dip"
android:id="@+id/SwitchToDate"
android:text="Set date"
android:enabled="false"
android:layout_weight="1"/>

<Button
android:layout_height="wrap_content"
android:layout_width="0dip"
android:id="@+id/SwitchToTime"
android:text="Set time"
android:layout_weight="1"/>

</LinearLayout>

<ViewSwitcher
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/DateTimePickerVS"
android:layout_below="@+id/ViewSwitchButtons"
android:outAnimation="@android:anim/fade_out"
android:inAnimation="@android:anim/fade_in">



<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/TimeLayout"
android:fillViewport="true">

<TimePicker
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/TimePicker"
android:layout_marginRight="5dip"
android:layout_marginLeft="5dip"/>

</LinearLayout>

<LinearLayout android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/DateLayout"
android:fillViewport="true">

<DatePicker
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/DatePicker"
android:layout_marginRight="5dip"
android:layout_marginLeft="5dip"/>

</LinearLayout>

</ViewSwitcher>

<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/ControlButtons"
android:layout_below="@+id/DateTimePicker"
android:paddingTop="185dip">

<Button
android:layout_height="wrap_content"
android:layout_width="0dip"
android:id="@+id/SetDateTime"
android:text="@android:string/ok"
android:layout_weight="1"/>

<Button
android:layout_height="wrap_content"
android:layout_width="0dip"
android:id="@+id/ResetDateTime"
android:text="Reset"
android:layout_weight="1"/>

<Button
android:layout_height="wrap_content"
android:layout_width="0dip"
android:id="@+id/CancelDialog"
android:text="@android:string/cancel"
android:layout_weight="1"/>

</LinearLayout>

</RelativeLayout>

最佳答案

Android: How to get the time from a TimePicker when it is typed in

似乎 DatePicker 或 TimePicker 在其焦点消失时更新自己的值。使用 clearFocus()设置按钮的OnClickListener中的方法。

我认为代码应该是这样的:

Set = ((Button) dialog.findViewById(R.id.SetDateTime));
Set.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
DPic.clearFocus();
TPic.clearFocus();

Calendar c = Calendar.getInstance();
c.set(DPic.getYear(), DPic.getMonth(), DPic.getDayOfMonth(),
TPic.getCurrentHour(), TPic.getCurrentMinute());
Log.e(TAG, "TPic hour and minute = " + TPic.getCurrentHour()
+ " " + TPic.getCurrentMinute());
timeSetOnSpinner = new DateTime(c);
}
});

请注意 ShowDTPickerShowDatePickerShowTimePickerSetReSet , Cancel, DPic, TPic, Date 未遵循 Java naming conventions .首字母大写的名称用于 classes or interfaces (并且 Date 类也存在!)。如果它们是公共(public)类变量,则它们的每个首字母都应小写。还使用 mSwitcher 而不是 switcher 因为它是私有(private)类变量。

关于android - TimePicker 不根据用户输入更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16591574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com