gpt4 book ai didi

android - 如何将时间从 android timepicker 传递到刚刚单击的编辑文本字段?

转载 作者:行者123 更新时间:2023-11-29 17:56:01 25 4
gpt4 key购买 nike

所以在我的应用程序页面上,有两个 EditText 字段(editEarliestDepart 和 editLatestDepart)。

我希望发生两件事:

  1. 当用户单击其中一个时,我希望出现一个 TimePicker fragment 对话框。用户设置时间后,值将传递到刚刚选择的 EditText 字段并显示在其中。

  2. 当用户设置“editLatestDepart”时,确保“editLatestDepart”总是在“editEarliestDepart”之后。

谁能帮我解决这个问题?我已经尝试了好几天,这让我发疯。

谢谢,

凯文

调用Timepicker fragment 对话框的Activity。

public class NewDriverSetupTime extends FragmentActivity implements TimePickedListener   {

String pickerHour, pickerMinute;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_driver_setup_time);

//create on click listener for earliest depart
EditText earliestDepart = (EditText) findViewById(R.id.editEarliestDepart);
earliestDepart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = new TimePickerDialogFragment();
newFragment.show(getFragmentManager(), "timePicker");
}
});

//create on click listener for latest depart
EditText latestDepart = (EditText) findViewById(R.id.editLatestDepart);
latestDepart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = new TimePickerDialogFragment();
newFragment.show(getFragmentManager(), "timePicker");
}
});

}

@Override
public void onTimePicked(Calendar time) {
// TODO Auto-generated method stub
// display the selected time in the clicked editview

}
}

这是实际的 fragment 代码

public class TimePickerDialogFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {

private TimePickedListener mListener;

@Override

public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
@Override
public void onAttach(Activity activity) {
// when the fragment is initially shown (i.e. attached to the activity), cast the activity to the callback interface type
super.onAttach(activity);
try
{
mListener = (TimePickedListener) activity;
}
catch (ClassCastException e)
{
throw new ClassCastException(activity.toString() + " must implement " + TimePickedListener.class.getName());
}

}

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

// when the time is selected, send it to the activity via its callback interface method
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, hourOfDay);
c.set(Calendar.MINUTE, minute);

mListener.onTimePicked(c);

}
public static interface TimePickedListener
{
public void onTimePicked(Calendar time);
}
}

最佳答案

这可能会帮助你...

在 onclick 方法中....

Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
edittext.setText( selectedHour + ":" + selectedMinute);
}
}, hour, minute, true);//Yes 24 hour time
mTimePicker.setTitle("Select Time");
mTimePicker.show();

关于android - 如何将时间从 android timepicker 传递到刚刚单击的编辑文本字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19235921/

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