如何在2个不同的EditText中设置2个timepicker?

时间:2017-02-02 09:34:02

标签: java android timepicker

我想在两个TimePicker中设置两个EditText但是当我选择任何时间选择器时,它将在EditTextStarttime中显示相同的时间。

有关。例如:当我选择TimePicker(EditText)Endtime时,它也会自动设置在starttime.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Initialize a new time picker dialog fragment DialogFragment dFragment = new TimePickerFragment(); // Show the time picker dialog fragment dFragment.show(getActivity().getFragmentManager(),"Time Picker"); isFromClicked = false; } }); endtime.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Initialize a new time picker dialog fragment DialogFragment dFragment1 = new TimePickerFragment(); // Show the time picker dialog fragment dFragment1.show(getActivity().getFragmentManager(),"Time Picker"); isFromClicked = true; } }); (EditText)中。

这里的代码: -

main.java

public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener{

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){
        // Get a Calendar instance
        final Calendar calendar = Calendar.getInstance();
        // Get the current hour and minute
        int hour = calendar.get(Calendar.HOUR);
        int minute = calendar.get(Calendar.MINUTE);


        // TimePickerDialog Theme : THEME_DEVICE_DEFAULT_LIGHT
        TimePickerDialog tpd = new TimePickerDialog(getActivity(),
                AlertDialog.THEME_DEVICE_DEFAULT_LIGHT,this,hour,minute,false);


        // Return the TimePickerDialog
        return tpd;
    }

  public void onTimeSet(TimePicker view, int hourOfDay, int minute){

        String am_pm = "";

        Calendar datetime = Calendar.getInstance();
        datetime.set(Calendar.HOUR_OF_DAY, hourOfDay);
        datetime.set(Calendar.MINUTE, minute);

        if (datetime.get(Calendar.AM_PM) == Calendar.AM)
            am_pm = "AM";
        else if (datetime.get(Calendar.AM_PM) == Calendar.PM)
            am_pm = "PM";

        String strHrsToShow = (datetime.get(Calendar.HOUR) == 0) ?"12":datetime.get(Calendar.HOUR)+"";

        ((EditText)getActivity().findViewById(R.id.start_time)).setText( strHrsToShow+":"+datetime.get(Calendar.MINUTE)+" "+am_pm );

        ((EditText)getActivity().findViewById(R.id.End_time)).setText( strHrsToShow+":"+datetime.get(Calendar.MINUTE)+" "+am_pm);

    }
}

TimePickerFragment.java

#include <iostream>
#include "boost/filesystem.hpp"

using namespace std;
using namespace boost::filesystem;

int main(int argc, char* argv[])
{
    // list all files in current directory.
    // You could put any file path in here, e.g. "/home/me/mwah" to list that
    // directory
    path p("/home/baptiste/Bureau");

    directory_iterator end_itr;

    // cycle through the directory
    for (directory_iterator itr(p); itr != end_itr; ++itr)
    {
        // If it's not a directory, list it. If you want to list directories too,
        // just remove this check.
        if (is_regular_file(itr->path())) 
        {
            // assign current file name to current_file and echo it out to the
            // console.
            string current_file = itr->path().string();
            cout << current_file << endl;
        }
    }
}

3 个答案:

答案 0 :(得分:0)

嗯,它正在完成您要告诉它的事情

 ((EditText)getActivity().findViewById(R.id.start_time)).setText( strHrsToShow+":"+datetime.get(Calendar.MINUTE)+" "+am_pm );

 ((EditText)getActivity().findViewById(R.id.End_time)).setText( strHrsToShow+":"+datetime.get(Calendar.MINUTE)+" "+am_pm);

在这里,您将EditTexts的文本设置为相同的值

答案 1 :(得分:0)

喜欢这个

  1. 处理两个编辑文本
  2. 的点击事件
  3. 点击打开时间选择器对话框
  4. 设定时间

答案 2 :(得分:0)

将此课程用于时间选择器。我已经使用它,它适用于2个editexts

    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");