EditText 上的 TimePicker 单击
TimePicker on EditText click
如何在 EditText
上设置 Timepicker
单击 android。
我搜索了以下链接,但它不起作用,因为 showDialog()
现在已被删除。
TimePicker Dialog from clicking EditText
http://www.botskool.com/geeks/how-create-time-picker-dialog-selecting-time-android
如有任何帮助,我们将不胜感激。
您必须使用 TimePickerDialog
class 才能将其显示为对话框。这是示例代码:
TimePickerDialog timePickerDialog = new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
}
}, 0, 0, true);
timePickerDialog.show();
您可以在 OnClickListener
方法中调用此代码
将此方法用于时间选择器android开发者网站
public static 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
}
}
你可以这样称呼它
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pick_time"
android:onClick="showTimePickerDialog" />
showTimePickerDialog 方法
public void showTimePickerDialog(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getSupportFragmentManager(), "timePicker");
}
我得到了相同的答案:-
在您的 Fragment 中包含以下方法。
public void selectTimeToDisplay(EditText edText) {
DialogFragment dialogFragment = new DisplayTimeFragment(edText);
dialogFragment.show(getActivity().getSupportFragmentManager(), "DatePicker");
}
public void setSelectedTime(String hourOfDay,String minute, EditText edText) {
edText.setText(hourOfDay + ":" + minute);
}
将 DisplayTimeFragment 定义为片段中的内部 class:-
public class DisplayTimeFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
EditText edText;
public DisplayTimeFragment(EditText edText){
this.edText = edText;
}
@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);
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
String sHrs, sMins;
if(minute<10){
sMins = "0"+minute;
}else{
sMins = String.valueOf(minute);
}
if(hourOfDay<10){
sHrs = "0"+hourOfDay;
}else{
sHrs = String.valueOf(hourOfDay);
}
setSelectedTime(sHrs,sMins, edText);
}
}
现在,在您的Edittext 的onClickListener 中编写以下代码。
作为,
edStartTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectTimeToDisplay(edStartTime);
}
});
注意:-为了顺利运行,在Edittext.
的xml
中设置focusable to false
Bingo.. 它现在可以作为任何 EditText 的魅力...
如何在 EditText
上设置 Timepicker
单击 android。
我搜索了以下链接,但它不起作用,因为 showDialog()
现在已被删除。
TimePicker Dialog from clicking EditText
http://www.botskool.com/geeks/how-create-time-picker-dialog-selecting-time-android
如有任何帮助,我们将不胜感激。
您必须使用 TimePickerDialog
class 才能将其显示为对话框。这是示例代码:
TimePickerDialog timePickerDialog = new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
}
}, 0, 0, true);
timePickerDialog.show();
您可以在 OnClickListener
方法中调用此代码
将此方法用于时间选择器android开发者网站
public static 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
}
}
你可以这样称呼它
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pick_time"
android:onClick="showTimePickerDialog" />
showTimePickerDialog 方法
public void showTimePickerDialog(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getSupportFragmentManager(), "timePicker");
}
我得到了相同的答案:-
在您的 Fragment 中包含以下方法。
public void selectTimeToDisplay(EditText edText) {
DialogFragment dialogFragment = new DisplayTimeFragment(edText);
dialogFragment.show(getActivity().getSupportFragmentManager(), "DatePicker");
}
public void setSelectedTime(String hourOfDay,String minute, EditText edText) {
edText.setText(hourOfDay + ":" + minute);
}
将 DisplayTimeFragment 定义为片段中的内部 class:-
public class DisplayTimeFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
EditText edText;
public DisplayTimeFragment(EditText edText){
this.edText = edText;
}
@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);
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
String sHrs, sMins;
if(minute<10){
sMins = "0"+minute;
}else{
sMins = String.valueOf(minute);
}
if(hourOfDay<10){
sHrs = "0"+hourOfDay;
}else{
sHrs = String.valueOf(hourOfDay);
}
setSelectedTime(sHrs,sMins, edText);
}
}
现在,在您的Edittext 的onClickListener 中编写以下代码。
作为,
edStartTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectTimeToDisplay(edStartTime);
}
});
注意:-为了顺利运行,在Edittext.
xml
中设置focusable to false
Bingo.. 它现在可以作为任何 EditText 的魅力...