我遵循了本指南: http: //developer.android.com/guide/topics/ui/controls/pickers.html 我不太确定这个(我对 android dev 很陌生)我有一个包我的mainActivity 类和 timepicker 类。我希望在设置时间时将其写入 mainactivity 的变量,或者相反,这样做“onTimeSet”将值保存为类变量并从我的 mainActivity 中读取这些变量。我怎么做?
这是我的片段类:
package com.example.remoteswitch;
import java.util.Calendar;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.widget.TimePicker;
public class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
int hour, min;
@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, true);
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
hour = hourOfDay;
min = minute;
}
}
在我的 MainActivity 类中,我有一个在按下按钮时调用的函数:
public void showTimePickerDialog(View v) {
//DialogFragment newFragment = new TimePickerFragment();
TimePickerFragment newFragment = new TimePickerFragment();
newFragment.show(getFragmentManager(), "timePicker");
//todo: read values from timePicker
int pickerHour = 0, pickerMin = 0;
//some manipulations on this data
}
}