我似乎无法弄清楚要放在onTimeSet()方法中的内容,以便将setText设置为与其关联的按钮。
我希望用户单击布局中的按钮,它会显示TimePickerDialog。一旦用户选择了他们想要使用的时间,我希望将时间设置为相同的按钮文本。
有什么想法吗?
public class TimePickerFragment extends DialogFragment implements
TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final 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()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
}
}
答案 0 :(得分:1)
是的,将数据从Fragment传回包含的Activity会有点复杂。
基本上你有两种选择,一种是简单而且前瞻性,另一种更复杂但从长远来看(对于更复杂的要求)会更好:
<强>简单强>
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
String timeString = hourOfDay+" "+minute; // <-- just for example, you'll want to do better time-formatting here.
Button b = (Button)getActivity().findViewById(R.id.yourButtonsId);
b.setText(timeString);
}
<强>先进强>
在Fragment中使用Listener 接口,让Activity实现该接口,并将自己设置为Fragment的监听器:
将此添加到您的片段:
public interface Listener {
public void setTime(int hourOfDay, int minute);
}
// hold the listener
private Listener mListener;
// used by Activity to set itself as the listener for the fragment
public void setListener(Listener listener) {
mListener = listener;
}
在片段中实施onTimeSet
,如下所示:
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
if (mListener != null) mListener.setTime(hourOfDay, minute);
}
将implements TimePickerFragment.Listener
添加到您的Activity的类声明中。
在您的活动中实施监听器的public void setTime(int hourOfDay, int minute)
方法,例如像这样:
public void setTime(int hourOfDay, int minute) {
String timeString = hourOfDay+" "+minute; // <-- just for example, you'll want to do better time-formatting here.
Button b = (Button)findViewById(R.id.yourButtonsId);
b.setText(timeString);
}
在实例化片段后将其添加到您的活动中:
timePickerFragment.setListener(this);
答案 1 :(得分:0)
// button click listener
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(button.getText().toString().trim().length()>0){
getTimeDialog(button.getText().toString(), new CustomClickListner() {
@Override
public void onClick(String value) {
btnLogin.setText(value);
}
});
}else{
getTimeDialog("", new CustomClickListner() {
@Override
public void onClick(String value) {
btnLogin.setText(value);
}
});
}
}
});
// custom interface time dialog set time listener
public interface CustomClickListner {
public void onClick(String value);
}
// open time dialog
public void getTimeDialog(final String strTime, final CustomClickListner target) {
Calendar date = getTimeFromString(strTime);
TimePickerDialog timeDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar date = Calendar.getInstance();
date.set(Calendar.HOUR_OF_DAY, hourOfDay);
date.set(Calendar.MINUTE, minute);
String dateString = new SimpleDateFormat("kk:mm:ss").format(date);
target.onClick(dateString);
}
}, date.get(Calendar.HOUR), date.get(Calendar.MINUTE), true);
timeDialog.show();
}
答案 2 :(得分:0)
只是您使用以下步骤
private void SetTime() {
buttonTaskTime.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String taskTime = buttonTaskTime.getText().toString();
int mHour = Integer.parseInt(taskTime.split(":")[0]);
String minutesmeridian = taskTime.split(":")[1];
int mMinute = Integer.parseInt(minutesmeridian.split(" ")[0]);
String mMeridian = minutesmeridian.split(" ")[1];
if (mMeridian.equals(postMeridain)) {
if (!(mHour == 12)) {
mHour += 12;
}
} else {
if (mHour == 12) {
mHour += 12;
}
}
hour = mHour;
minute = mMinute;
new TimePickerDialog(getActivity(), R.style.Theme_CustomDialog,
timePicker, hour, minute, false).show();
}
});
}
TimePickerDialog.OnTimeSetListener timePicker = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minutes) {
hour = hourOfDay;
minute = minutes;
calendarNewTaskTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendarNewTaskTime.set(Calendar.MINUTE, minutes);
calendarNewTaskTime.set(Calendar.SECOND, 0);
UpdateTime();
}
};
private void UpdateTime() {
String meridian = " ";
if (hour > 12) {
hour -= 12;
meridian = "PM";
} else if (hour == 0) {
hour = 12;
meridian = "AM";
} else if (hour == 12)
meridian = "PM";
else
meridian = "AM";
String min = " ";
if (minute < 10) {
min = "0" + minute;
} else
min = String.valueOf(minute);
buttonTaskTime.setText(hour + ":" + min + " " + meridian);
}
答案 3 :(得分:0)
尝试使用我常用的代码
custom = new CustomDateTimePicker(this,
new CustomDateTimePicker.ICustomDateTimeListener() {
@Override
public void onSet(Dialog dialog, Calendar calendarSelected,
Date dateSelected, int year, String monthFullName,
String monthShortName, int monthNumber, int date,
String weekDayFullName, String weekDayShortName,
int hour24, int hour12, int min, int sec,
String AM_PM) {
((Button) findViewById(R.id.alerttime))
.setText(calendarSelected
.get(Calendar.DAY_OF_MONTH)
+ "/" + monthNumber + "/" + year + " "+
+ hour12 + ":" + min + ":" + "00"
+ " " + AM_PM);
}