Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 从单个对话片段获取日期和时间选择器值,并将其设置为EditText大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在处理用户可以设置日期和时间的应用程序.我想在单个对话片段中设置日期和时间,并将其设置为编辑文本.可以在1个对话片段中使用日期和时间选择器吗?如果是这样,你怎么做?
目前,我正在从对话片段中获取时间,然后将其设置为EditText.

我的代码

LaterTextView=(TextView)findViewById(R.id.PickUp_later_TextView);



       LaterTextView.setOnClickListener(new View.onClickListener() {

            @SuppressWarnings("deprecation")
            public void onClick(View v) {
                showDialog(TIME_DIALOG_ID);

                }
        });




}


protected Dialog onCreateDialog(int id) {
    switch (id) {
    case TIME_DIALOG_ID:

        // set time picker as current time
        return new TimePickerDialog(this,timePickerListener,hour,minute,falsE);

    }
    return null;
}

private TimePickerDialog.onTimeSetListener timePickerListener = new TimePickerDialog.onTimeSetListener() {
    public void onTimeSet(TimePicker view,int SELEctedHour,int SELEctedminutE) {
        hour = SELEctedHour;
        minute = SELEctedminute;

        // set current timE into TextView
        setDate_time_EditTExt.setText(new StringBuilder().append(pad(hour))
                .append(":").append(pad(minutE)));



    }
};
private static String pad(int C) {
    if (c >= 10)
        return String.valueOf(c);
    else
        return "0" + String.valueOf(c);
}

我想从相同的对话框片段设置日期和时间.

解决方法

日期选择器和时间选择器都在单个对话框中.检查这个代码. @H_622_3@main_Activity.java

@Override
public void onCreate(Bundle savedInstanceStatE) {
    super.onCreate(savedInstanceStatE);
    setContentView(R.layout.activity_main);
    custom = new CustomdatetiR_926_11845@ePicker(this,new CustomdatetiR_926_11845@ePicker.ICustomdatetiR_926_11845@eListener() {

                @Override
                public void onSet(Dialog dialog,Calendar calendarSELEcted,Date dateSELEcted,int year,String monthFullName,String monthShortName,int monthnumber,int date,String weekDayFullName,String weekDayShortName,int hour24,int hour12,int min,int sec,String AM_PM) {
                    ((TextView) findViewById(R.id.lablel))
                            .setText(calendarSELEcted
                                            .get(Calendar.DAY_OF_MONTH)
                                    + "/" + (monthnumber+1) + "/" + year
                                    + "," + hour12 + ":" + min
                                    + " " + AM_PM);
                }

                @Override
                public void onCancel() {

                }
            });
    /**
     * Pass Directly current time format it will return AM and PM if you set
     * false
     */
    custom.set24HourFormat(false);
    /**
     * Pass Directly current data and time to show when it pop up
     */
    custom.setDate(Calendar.geTinstance());

    findViewById(R.id.button_datE).setOnClickListener(
            new OnClickListener() {

                @Override
                public void onClick(View v) {
                    custom.showDialog();
                }
            });
}

CustomdatetiR_926_11845@ePicker custom;

@Override
public Boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main,menu);
    return true;
}

CustomdatetiR_926_11845@ePicker.java

public class CustomdatetiR_926_11845@ePicker implements OnClickListener {
private DatePicker datePicker;
private TimePicker timePicker;
private ViewSwitcher viewSwitcher;

private final int SET_DATE = 100,SET_TIME = 101,SET = 102,CANCEL = 103;

private Button btn_setDate,btn_setTime,btn_set,btn_cancel;

private Calendar calendar_date = null;

private Activity activity;

private ICustomdatetiR_926_11845@eListener iCustomdatetiR_926_11845@eListener = null;

private Dialog dialog;

private Boolean is24HourView = true,isAutoDismiss = true;

privatE int SELEctedHour,SELEctedminute;

public CustomdatetiR_926_11845@ePicker(Activity a,ICustomdatetiR_926_11845@eListener customdatetiR_926_11845@eListener) {
    activity = a;
    iCustomdatetiR_926_11845@eListener = customdatetiR_926_11845@eListener;

    dialog = new Dialog(activity);
    dialog.setOnDismissListener(new OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {
            resetData();
        }
    });

    dialog.requestWindowFeature(Window.FEATURE_NO_titlE);
    View dialogView = getdatetiR_926_11845@ePickerLayout();
    dialog.setContentView(dialogView);
}

public View getdatetiR_926_11845@ePickerLayout() {
    LinearLayout.LayoutParams linear_match_wrap = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
    LinearLayout.LayoutParams linear_wrap_wrap = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
    FrameLayout.LayoutParams frame_match_wrap = new FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT);

    LinearLayout.LayoutParams button_params = new LinearLayout.LayoutParams(
            0,LinearLayout.LayoutParams.WRAP_CONTENT,1.0f);

    LinearLayout linear_main = new LinearLayout(activity);
    linear_main.setLayoutParams(linear_match_wrap);
    linear_main.setOrientation(LinearLayout.VERTICAL);
    linear_main.setGravity(Gravity.CENTER);

    LinearLayout linear_child = new LinearLayout(activity);
    linear_child.setLayoutParams(linear_wrap_wrap);
    linear_child.setOrientation(LinearLayout.VERTICAL);

    LinearLayout linear_top = new LinearLayout(activity);
    linear_top.setLayoutParams(linear_match_wrap);

    btn_setDate = new Button(activity);
    btn_setDate.setLayoutParams(button_params);
    btn_setDate.setText("Set Date");
    btn_setDate.setId(SET_DATE);
    btn_setDate.setOnClickListener(this);

    btn_setTime = new Button(activity);
    btn_setTime.setLayoutParams(button_params);
    btn_setTime.setText("Set Time");
    btn_setTime.setId(SET_TIME);
    btn_setTime.setOnClickListener(this);

    linear_top.addView(btn_setDatE);
    linear_top.addView(btn_setTimE);

    viewSwitcher = new ViewSwitcher(activity);
    viewSwitcher.setLayoutParams(frame_match_wrap);

    datePicker = new DatePicker(activity);
    timePicker = new TimePicker(activity);
    timePicker.setOnTimeChangedListener(new OnTimeChangedListener() {
        @Override
        public void onTimeChanged(TimePicker view,int hourOfDay,int minutE) {
            SELEctedHour = hourOfDay;
            SELEctedminute = minute;
        }
    });

    viewSwitcher.addView(timePicker);
    viewSwitcher.addView(datePicker);

    LinearLayout linear_bottom = new LinearLayout(activity);
    linear_match_wrap.topMargin = 8;
    linear_bottom.setLayoutParams(linear_match_wrap);

    btn_set = new Button(activity);
    btn_set.setLayoutParams(button_params);
    btn_set.setText("Set");
    btn_set.setId(SET);
    btn_set.setOnClickListener(this);

    btn_cancel = new Button(activity);
    btn_cancel.setLayoutParams(button_params);
    btn_cancel.setText("Cancel");
    btn_cancel.setId(CANCEL);
    btn_cancel.setOnClickListener(this);

    linear_bottom.addView(btn_set);
    linear_bottom.addView(btn_cancel);

    linear_child.addView(linear_top);
    linear_child.addView(viewSwitcher);
    linear_child.addView(linear_bottom);

    linear_main.addView(linear_child);

    return linear_main;
}

public void showDialog() {
    if (!dialog.isShowing()) {
        if (calendar_date == null)
            calendar_date = Calendar.geTinstance();

        SELEctedHour = calendar_date.get(Calendar.HOUR_OF_DAY);
        SELEctedminute = calendar_date.get(Calendar.minutE);

        timePicker.setIs24HourView(is24HourView);
        timePicker.setCurrentHour(SELEctedHour);
        timePicker.setCurrentminute(SELEctedminutE);

        datePicker.updateDate(calendar_date.get(Calendar.YEAR),calendar_date.get(Calendar.MONTH),calendar_date.get(Calendar.DATE));

        dialog.show();

        btn_setDate.performClick();
    }
}

public void setAutoDismiss(Boolean isAutoDismiss) {
    this.isAutoDismiss = isAutoDismiss;
}

public void dismissDialog() {
    if (!dialog.isShowing())
        dialog.dismiss();
}

public void setDate(Calendar calendar) {
    if (calendar != null)
        calendar_date = calendar;
}

public void setDate(Date datE) {
    if (date != null) {
        calendar_date = Calendar.geTinstance();
        calendar_date.setTime(datE);
    }
}

public void setDate(int year,int month,int day) {
    if (month < 12 && month >= 0 && day < 32 && day >= 0 && year > 100
            && year < 3000) {
        calendar_date = Calendar.geTinstance();
        calendar_date.set(year,month,day);
    }

}

public void setTimeIn24HourFormat(int hourIn24Format,int minutE) {
    if (hourIn24Format < 24 && hourIn24Format >= 0 && minute >= 0
            && minute < 60) {
        if (calendar_date == null)
            calendar_date = Calendar.geTinstance();

        calendar_date.set(calendar_date.get(Calendar.YEAR),calendar_date.get(Calendar.DAY_OF_MONTH),hourIn24Format,minutE);

        is24HourView = true;
    }
}

public void setTimeIn12HourFormat(int hourIn12Format,int minute,Boolean isAM) {
    if (hourIn12Format < 13 && hourIn12Format > 0 && minute >= 0
            && minute < 60) {
        if (hourIn12Format == 12)
            hourIn12Format = 0;

        int hourIn24Format = hourIn12Format;

        if (!isAM)
            hourIn24Format += 12;

        if (calendar_date == null)
            calendar_date = Calendar.geTinstance();

        calendar_date.set(calendar_date.get(Calendar.YEAR),minutE);

        is24HourView = false;
    }
}

public void set24HourFormat(Boolean is24HourFormat) {
    is24HourView = is24HourFormat;
}

public interface ICustomdatetiR_926_11845@eListener {
    public void onSet(Dialog dialog,String AM_PM);

    public void onCancel();
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case SET_DATE:
        btn_setTime.setEnabled(true);
        btn_setDate.setEnabled(false);
        viewSwitcher.shownext();
        break;

    case SET_TIME:
        btn_setTime.setEnabled(false);
        btn_setDate.setEnabled(true);
        viewSwitcher.showPrevIoUs();
        break;

    case SET:
        if (iCustomdatetiR_926_11845@eListener != null) {
            int month = datePicker.getMonth();
            int year = datePicker.getYear();
            int day = datePicker.getDayOfMonth();


            calendar_date.set(year,day,SELEctedHour,SELEctedminutE);

            iCustomdatetiR_926_11845@elistener.onSet(dialog,calendar_date,calendar_date.getTime(),calendar_date
                            .get(Calendar.YEAR),getMonthFullName(calendar_date.get(Calendar.MONTH)),getMonthShortName(calendar_date.get(Calendar.MONTH)),calendar_date
                            .get(Calendar.DAY_OF_MONTH),getWeekDayFullName(calendar_date
                            .get(Calendar.DAY_OF_WEEK)),getWeekDayShortName(calendar_date
                            .get(Calendar.DAY_OF_WEEK)),calendar_date
                            .get(Calendar.HOUR_OF_DAY),getHourIn12Format(calendar_date
                            .get(Calendar.HOUR_OF_DAY)),calendar_date
                            .get(Calendar.minutE),calendar_date
                            .get(Calendar.SECOND),getAMPM(calendar_datE));
        }
        if (dialog.isShowing() && isAutoDismiss)
            dialog.dismiss();
        break;

    case CANCEL:
        if (iCustomdatetiR_926_11845@eListener != null)
            iCustomdatetiR_926_11845@elistener.onCancel();
        if (dialog.isShowing())
            dialog.dismiss();
        break;
    }
}

/**
 * @param date
 *            date in String
 * @param fromFormat
 *            format of your <b>date</b> eg: if your date is 2011-07-07
 *            09:09:09 then your format will be <b>yyyy-MM-dd hh:mm:ss</b>
 * @param toFormat
 *            format to which you want to convert your <b>date</b> eg: if
 *            required format is 31 July 2011 then the toFormat should be
 *            <b>d MMMM yyyy</b>
 * @return formatted date
 */
public static String convertDate(String date,String fromFormat,String toFormat) {
    try {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(fromFormat);
        Date d = simpleDateFormat.parse(datE);
        Calendar calendar = Calendar.geTinstance();
        calendar.setTime(d);

        simpleDateFormat = new SimpleDateFormat(toFormat);
        simpleDateFormat.setCalendar(calendar);
        date = simpleDateFormat.format(calendar.getTime());

    } catch (Exception E) {
        e.printStackTrace();
    }

    return date;
}

private String getMonthFullName(int monthnumber) {
    Calendar calendar = Calendar.geTinstance();
    calendar.set(Calendar.MONTH,monthnumber);

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMMM");
    simpleDateFormat.setCalendar(calendar);
    String monthName = simpleDateFormat.format(calendar.getTime());

    return monthName;
}

private String getMonthShortName(int monthnumber) {
    Calendar calendar = Calendar.geTinstance();
    calendar.set(Calendar.MONTH,monthnumber);

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM");
    simpleDateFormat.setCalendar(calendar);
    String monthName = simpleDateFormat.format(calendar.getTime());

    return monthName;
}

private String getWeekDayFullName(int weekDaynumber) {
    Calendar calendar = Calendar.geTinstance();
    calendar.set(Calendar.DAY_OF_WEEK,weekDaynumber);

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE");
    simpleDateFormat.setCalendar(calendar);
    String weekName = simpleDateFormat.format(calendar.getTime());

    return weekName;
}

private String getWeekDayShortName(int weekDaynumber) {
    Calendar calendar = Calendar.geTinstance();
    calendar.set(Calendar.DAY_OF_WEEK,weekDaynumber);

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EE");
    simpleDateFormat.setCalendar(calendar);
    String weekName = simpleDateFormat.format(calendar.getTime());

    return weekName;
}

privatE int getHourIn12Format(int hour24) {
    int hourIn12Format = 0;

    if (hour24 == 0)
        hourIn12Format = 12;
    else if (hour24 <= 12)
        hourIn12Format = hour24;
    else
        hourIn12Format = hour24 - 12;

    return hourIn12Format;
}

private String getAMPM(Calendar calendar) {
    String ampm = (calendar.get(Calendar.AM_PM) == (Calendar.AM)) ? "AM"
            : "PM";
    return ampm;
}

private void resetData() {
    calendar_date = null;
    is24HourView = true;
}

public static String pad(int IntegerToPad) {
    if (IntegerToPad >= 10 || IntegerToPad < 0)
        return String.valueOf(IntegerToPad);
    else
        return "0" + String.valueOf(IntegerToPad);
}
}

activity_main.xml中

<RelativeLayout xmlns:android="http://scheR_926_11845@as.android.com/apk/res/android"
xmlns:tools="http://scheR_926_11845@as.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/lablel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:padding="@dimen/padding_medium"
    android:text="@String/Hello_world"
    android:textColor="#000"
    android:textSize="20dp"
    tools:context=".MainActivity" />

<Button
    android:id="@+id/button_date"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layouT_Below="@id/lablel"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="80dp"
    android:text="Date Time Picker" />

</RelativeLayout>

大佬总结

以上是大佬教程为你收集整理的android – 从单个对话片段获取日期和时间选择器值,并将其设置为EditText全部内容,希望文章能够帮你解决android – 从单个对话片段获取日期和时间选择器值,并将其设置为EditText所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。