我正在尝试将time picker
添加到日期选择器中。我是java的初学者,也许有人可以告诉我正确的方法?我希望时间选择器显示在日历视图的顶部,在我选择日期后,时间选择器弹出。可能吗 ?在xml文件中,只有很少的EditText字段已在此类中声明。
public class CompletedWorksActivity extends Activity implements
DatePickerDialog.OnDateSetListener, OnClickListener, OnTimeSetListener {
private TextView text_date;
private Button search;
private EditText from;
private EditText to;
private int yearfrom;
private int monthfrom;
private int dayfrom;
private int hourfrom;
private int minfrom;
private int yearto;
private int monthto;
private int dayto;
private int hourto;
private int minto;
private int hour;
private int min;
private DatePickerDialog date_picker;
private TimePickerDialog time_picker;
private boolean fromEdit;
private boolean toEdit;
@Override
protected void onCreate(Bundle menuinstance) {
super.onCreate(menuinstance);
setContentView(R.layout.completed_works);
Intent intent = getIntent(); // gaunam
User user = (User) intent.getSerializableExtra("user");
from = (EditText) findViewById(R.id.datefrom);
to = (EditText) findViewById(R.id.dateto);
Calendar cal = Calendar.getInstance();
hour = cal.get(Calendar.HOUR_OF_DAY);
min = cal.get(Calendar.MINUTE);
date_picker = new DatePickerDialog(this, this, cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
date_picker.getDatePicker().setCalendarViewShown(true);
date_picker.getDatePicker().setSpinnersShown(false);
time_picker = new TimePickerDialog(this, this, hour, min, true);
from.setOnFocusChangeListener(focusListener);
to.setOnFocusChangeListener(focusListener);
time_picker.set
search = (Button) findViewById(R.id.searchSubmit);
// search.setOnClickListener(searchlistener);
}
@Override
public void onDateSet(DatePicker dp, int y, int m, int d) {
String cheapDate = y + "-" + (m + 1) + "-" + d;
View v = getCurrentFocus();
if (v == from) {
yearfrom = y;
monthfrom = m;
dayfrom = d;
Log.d("year from ", "" + yearfrom + monthfrom + dayfrom);
} else {
yearto = y;
monthto = m;
dayto = d;
Log.d("year to ", "" + yearto + monthto + dayto);
// Toast.makeText(getApplicationContext(),yearto+monthto+dayto ,
// Toast.LENGTH_LONG ).show();
}
if (fromEdit) {
from.setText(cheapDate);
} else {
to.setText(cheapDate);
}
}
private View.OnClickListener searchlistener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
};
private View.OnFocusChangeListener focusListener = new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
EditText edit = (EditText) v;
int len = edit.getText().toString().length();
if (edit == findViewById(R.id.datefrom)) {
Toast.makeText(getApplicationContext(), "Data nuo ? ",
Toast.LENGTH_LONG).show();
fromEdit = v.getId() == R.id.datefrom;
date_picker.show();
} else {
Toast.makeText(getApplicationContext(), "Data iki ? ",
Toast.LENGTH_LONG).show();
fromEdit = v.getId() == R.id.datefrom;
date_picker.show();
}
}
}
};
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:3)
Android使用android.widget.TimePicker component
设置时间非常容易。
请参阅this Tutorial在这里,您可以看到用户如何使用易于使用的android.app.TimePickerDialog
对话框选择小时和分钟。
答案 1 :(得分:-1)
尝试以下代码:
date_time_layout.xml
<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:calendarViewShown="false" > <!-- style="@style/date_picker_style" -->
</DatePicker>
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" >
</TimePicker>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" >
<Button
android:id="@+id/btnCancelDT"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/button_selector"
android:text="Cancel"
android:textColor="@color/white"
android:textStyle="bold" />
<Button
android:id="@+id/btnSetDT"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/button_selector"
android:text="Set"
android:textColor="@color/white"
android:textStyle="bold" />
</LinearLayout>
活动类:
public void showDateTimePickerDialog() {
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.date_time_layout);
dialog.setTitle("Set Schedule Call");
dialog.show();
Button btnCancel = (Button)dialog.findViewById(R.id.btnCancelDT);
Button btnSet = (Button)dialog.findViewById(R.id.btnSetDT);
final DatePicker dp = (DatePicker)dialog.findViewById(R.id.datePicker1);
final TimePicker tp = (TimePicker)dialog.findViewById(R.id.timePicker1);
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
btnSet.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String am_pm = "";
// TODO Auto-generated method stub
int m = dp.getMonth()+1;
int d = dp.getDayOfMonth();
int y = dp.getYear();
int h = tp.getCurrentHour();
int min = tp.getCurrentMinute();
String strm = String.valueOf(min);
if(strm.length()==1){
strm = "0"+strm;
}
if(h>12){
am_pm = "PM";
h = h-12;
}else{
am_pm = "AM";
}
date = m+"/"+d+"/"+y+" "+h+":"+strm+":00 "+am_pm;
time = h+":"+strm+" "+am_pm;
System.out.println("Date: " + date +" Time: "+ time);
dialog.dismiss();
}
});
}
按下按钮或根据需要调用此方法。