如何在此示例中获取TIME的值,以便我可以在我的主代码中使用它,这在save javaFile中。我试着做一个公共字符串时间,然后在代码中使用像clock1 = time;
为了更清楚我想要实现的目标:
这在我的主要代码中:
spinner_dailyInput.setAdapter(adapter3);
spinner_dailyInput.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 1:
for(int i=0;i<1;i++) {
setTimePicker();
clock1=time;
}
break;
这基本上是一个旋转,如果用户选择例如1,他将得到1(选择时间),现在我需要将他输入的时间保存到变量以供进一步使用。如果用户选择2例如他将获得一个时钟,输入时间点击保存(这需要保存到Clock1),然后弹出另一个时间戳(再次选择时间然后保存到Clock2)。
public void setTimePicker(){
Calendar mcurrentTime = Calendar.getInstance();
final int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
hr=String.valueOf(selectedHour);
mini=String.valueOf(selectedMinute);
time=hr+":"+mini;
Log.e(TAG+".........TimePicker.....................",time+"");
}
}, hour, minute, true);
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
答案 0 :(得分:0)
String time = null;
public void setTimePicker(){
...
mTimePicker = new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
...
time=hr+":"+mini;
}
}, hour, minute, true);
...
//Here, time could still be null if mTimePicker.show(); is an asynchronous call (most likely)
}
调用onTimeSet
后,实例中的变量time
将保留您想要的值。但是这个调用可以是异步,如果是这样,不希望直接在setTimePicker()
调用结束时获取值,该值只会在用户输入值后发生在显示的TimerPickerDialog中。
答案 1 :(得分:0)
spinner_dailyInput.setAdapter(adapter3);
spinner_dailyInput.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 1:
for(int i=0;i<1;i++) {
setTimePicker(i);
//clock1=time; <--- wrong. time is still null here
}
break;
}
}
}
public void setTimePicker(final int i){
Calendar mcurrentTime = Calendar.getInstance();
final int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
hr=String.valueOf(selectedHour);
mini=String.valueOf(selectedMinute);
time=hr+":"+mini;
Log.e(TAG+".........TimePicker.....................",time+"");
mySimpleCallback(i, time)';//<---- not null here
}
}, hour, minute, true);
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
private void mySimpleCallback(int i, String time ) {
//not null here because it fires after your code execution
if (i == 0)
clock1 = time;
else
clock2 = time;
}