任何人都可以帮助我在同一活动中实施两个时间选择器并将其4个值(小时,分钟)存储到4个不同的变量中.... 我的代码如下(不工作)......
public class Time扩展AppCompatActivity {
static final int START_TIME_ID=0;
static final int END_TIME_ID=1;
EditText edit_start_time,edit_end_time;
public int hour,minute;
private int chour,cminute;
public Time(){
final Calendar calendar=Calendar.getInstance();
chour=calendar.get(Calendar.HOUR_OF_DAY);
cminute=calendar.get(Calendar.MINUTE);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_Time);
edit_start_time = (EditText) findViewById(R.id.edit_start_time);
edit_end_time = (EditText) findViewById(R.id.edit_end_time);
edit_start_time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(START_TIME_ID);
}
});
edit_end_time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
showDialog(END_TIME_ID);
}
});
}
private TimePickerDialog.OnTimeSetListener mStartTime=new TimePickerDialog.OnTimeSetListener()
{
public void onTimeSet(TimePicker view,int hourofday,int min)
{
hour=hourofday;
minute=min;
}
};
private TimePickerDialog.OnTimeSetListener mEndTime=new TimePickerDialog.OnTimeSetListener()
{
public void onTimeSet(TimePicker view,int hourofday,int min)
{
hour=hourofday;
minute=min;
}
};
@Override
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case START_TIME_ID:
return new TimePickerDialog(this,mStartTime,chour,cminute,false);
case END_TIME_ID:
return new TimePickerDialog(this,mEndTime,chour,cminute,false);
}
return null;
}
答案 0 :(得分:0)
理解这段代码,
private TimePickerDialog.OnTimeSetListener showTimePicker =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int selectedHour,
int selectedMinute) {
if (view.getId() == R.id.startTime)
//todo stuff
else if(view.getId() == R.id.endTime)
//todo stuff
}
};
startTime.setOnClickListener(showTimePicker);
endTime.setOnClickListener(showTimePicker);
答案 1 :(得分:0)
在我看来
无需制作两个时间选择器。取一个布尔变量。当点击edit_start_time然后值等于true,当点击edit_end_time时将其改为false。
在布尔变量的基础上设置4个不同的变量值。
希望对你有所帮助。
答案 2 :(得分:0)
也许这段代码会有所帮助
public class TimePicker extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
TextView textView;
Context context;
private TimePickerDialog timePickerDialog;
public TimePicker(EditText text, Context context) {
this.text= textView;
this.context = context;
}
public TimePicker() {
}
@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()));
}
@Override
public void onTimeSet(android.widget.TimePicker view, int hourOfDay, int minute) {
Time t = new Time(hourOfDay,minute,0);//seconds by default set to zero
Format formatter;
formatter = new SimpleDateFormat("h:mm a", Locale.ENGLISH);
String time = formatter.format(t);
textView.setText(hourOfDay+":"+minute);
}
}
使用
TimePicker timePicker = new TimePicker(editTextName, getActivity());
timePicker.show(getActivity().getSupportFragmentManager(), "Start Time");