我们如何在不同类的Edittext中设置值

时间:2013-07-18 07:45:59

标签: android-fragments android-timepicker

我正在使用时间片段。点击edittext框后,我打开了这样的时间选择器。

final FragmentManager fm = getFragmentManager();
timE.setOnClickListener(new OnClickListener() {
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @SuppressWarnings("deprecation")
    public void onClick(View v) {
        FragmentManager fm = getFragmentManager();
         dialogFragment2=new DateTimeDialogFragment(ActivityGroup4.context ,2);
        dialogFragment2.setContextandView(appState,vi,timE);
        System.out.println(vi+" view from meeting list ");
        dialogFragment2.setRetainInstance(true);
        dialogFragment2.show(fm, "fragment_name");
    }
});

这导致时间选择器打开。这里appState是上下文对象,vi是可扩展列表适配器groupchildview方法的视图对象,用于访问edittext片段类中的datetime

DateTimeDialogFragment中,当选择器打开并单击设置按钮时,我们会获得该值,但此值不会显示在上一个活动的edittext框中

public class DateTimeDialogFragment extends DialogFragment implements
        OnDateChangedListener, OnTimeChangedListener {
    // Define constants for date-time picker.
    String time_and_date = "";
    int i = 0, j = 0;
    //StringBuilder click = new StringBuilder();;
    public final static int DATE_PICKER = 1;
    public final static int TIME_PICKER = 2;
    public final static int DATE_TIME_PICKER = 3;

    // DatePicker reference
    private DatePicker datePicker;

    // TimePicker reference
    private TimePicker timePicker;

    // Calendar reference
    private Calendar mCalendar;

    // Define activity
    private Activity activity;

    // Define Dialog type
    private int DialogType;

    // Define Dialog view
    private View mView;

    // Constructor start
    public DateTimeDialogFragment(Activity activity) {
        this(activity, DATE_TIME_PICKER);
    }

    Context context;
    View v;
    EditText refrenced;

    public void setContextandView(Context context, View v, EditText refrenced) {
        this.refrenced = refrenced;
        this.context = context;
        this.v = v;
        //time_and_date = refrenced.getText().toString();
    }

    @SuppressLint("ValidFragment")
    public DateTimeDialogFragment(Activity activity, int DialogType) {
        this.activity = activity;
        this.DialogType = DialogType;

        // Inflate layout for the view
        // Pass null as the parent view because its going in the dialog layout
        LayoutInflater inflater = activity.getLayoutInflater();
        mView = inflater.inflate(R.layout.date_time_dialog, null);

        // Grab a Calendar instance
        mCalendar = Calendar.getInstance();

        // Init date picker
        datePicker = (DatePicker) mView.findViewById(R.id.datePicker1);
        datePicker.init(mCalendar.get(Calendar.YEAR),
                mCalendar.get(Calendar.MONTH),
                mCalendar.get(Calendar.DAY_OF_MONTH), this);

        // Init time picker
        timePicker = (TimePicker) mView.findViewById(R.id.timePicker1);

        // Set default Calendar and Time Style
        setIs24HourView(false);
        setCalendarViewShown(false);

        switch (DialogType) {
        case DATE_PICKER:
            timePicker.setVisibility(View.GONE);
            break;
        case TIME_PICKER:
            datePicker.setVisibility(View.GONE);
            break;
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        // Use the Builder class for convenient dialog construction
        Builder builder = new AlertDialog.Builder(activity);

        // Set the layout for the dialog
        builder.setView(mView);

        // Set title of dialog
        builder.setMessage("Set Date")
                // Set Ok button
            .setPositiveButton("Set",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        // User ok the dialog
                        // System.out.println(time_and_date);
                        if (time_and_date != "") {
                            if (time_and_date.length() <= 5) {

                                System.out.println(time_and_date + "  "
                                        + v);
                                // EditText time = (EditText)
                                // v.findViewById(R.id.time_specially_for_fragment);
                                System.out.println(refrenced.getText());
                                System.out.println("sss");
                                refrenced.setText(time_and_date);
                                refrenced.setVisibility(View.INVISIBLE);
                                ;
                            } else {
                                System.out.println(time_and_date);
                                // EditText time = (EditText)
                                // v.findViewById(R.id.date_specially_for_fragment);
                                // time.setText(time_and_date);
                            }
                        } else {
                            System.out.println("went wrong");
                            if (DialogType ==2) {
                                int hour = mCalendar
                                        .get(Calendar.HOUR_OF_DAY);
                                int min = mCalendar
                                        .get(Calendar.MINUTE);
                                time_and_date = "" + hour + ":" + (min);
                                System.out.println(time_and_date);
                                i = j = 0;

                            } else if (DialogType==1
                                    ) {
                                int day = mCalendar
                                        .get(Calendar.DAY_OF_MONTH);
                                int month = mCalendar
                                        .get(Calendar.MONTH);
                                int year = mCalendar.get(Calendar.YEAR);

                                time_and_date=""+day+"/"+(month+1)+"/"+year;
                                i=j=0;
                                System.out.println(time_and_date);
                            }
                        }
                    }
                })
                // Set Cancel button
                .setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // User cancelled the dialog
                            DateTimeDialogFragment.this.getDialog()
                                    .cancel();
                        }
                    });

        // Create the AlertDialog object and return it
        return builder.create();
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        timePicker.setOnTimeChangedListener(this);
    }

    // Convenience wrapper for internal Calendar instance
    public int get(final int field) {
        return mCalendar.get(field);
    }

    // Convenience wrapper for internal Calendar instance
    public long getDateTimeMillis() {
        return mCalendar.getTimeInMillis();
    }

    // Convenience wrapper for internal TimePicker instance
    public void setIs24HourView(boolean is24HourView) {
        timePicker.setIs24HourView(is24HourView);
    }

    // Convenience wrapper for internal TimePicker instance
    public boolean is24HourView() {
        return timePicker.is24HourView();
    }

    // Convenience wrapper for internal DatePicker instance
    public void setCalendarViewShown(boolean calendarView) {
        datePicker.setCalendarViewShown(calendarView);
    }

    // Convenience wrapper for internal DatePicker instance
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public boolean CalendarViewShown() {
        return datePicker.getCalendarViewShown();
    }

    // Convenience wrapper for internal DatePicker instance
    public void updateDate(int year, int monthOfYear, int dayOfMonth) {
        datePicker.updateDate(year, monthOfYear, dayOfMonth);
    }

    // Convenience wrapper for internal TimePicker instance
    public void updateTime(int currentHour, int currentMinute) {
        timePicker.setCurrentHour(currentHour);
        timePicker.setCurrentMinute(currentMinute);
    }

    public String getDateTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
                Locale.US);
        return sdf.format(mCalendar.getTime());
    }

    // Called every time the user changes DatePicker values
    @Override
    public void onDateChanged(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        // Update the internal Calendar instance
        i++;
        // click='Date';
        mCalendar.set(year, monthOfYear, dayOfMonth,
                mCalendar.get(Calendar.HOUR_OF_DAY),
                mCalendar.get(Calendar.MINUTE));

        time_and_date = "" + dayOfMonth + "/" + monthOfYear + "/" + year;
    }

    // Called every time the user changes TimePicker values
    @Override
    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
        // Update the internal Calendar instance
        j++;
        mCalendar.set(mCalendar.get(Calendar.YEAR),
                mCalendar.get(Calendar.MONTH),
                mCalendar.get(Calendar.DAY_OF_MONTH), hourOfDay, minute);
        time_and_date = hourOfDay + ":" + minute;
        System.out.println("from time changing " + time_and_date);
        // gettime();
    }

    String getdate() {
        return time_and_date;
    }

    String gettime() {
        return time_and_date;
    }
}

如何在此edittext框中设置时间

0 个答案:

没有答案