如何为不同的TimePickerDialog两次引用onTimeSet函数

时间:2018-08-02 04:15:26

标签: android android-studio user-interface

我正在尝试创建两个TimePickerDialogs,一个用于开始时间,另一个用于结束时间。如何确保ontimeset方法为所需的两个不同实例设置单独的时间。

@Override
public void onTimeSet(TimePicker timePicker, int i, int i1) {

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_date);



    mTimePicker.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DialogFragment timePicker = new TimePickerFragment();
            timePicker.show(getSupportFragmentManager(),"time picker");

        }
    });

    mTimePicker1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DialogFragment timePicker1 = new TimePicker1Fragment();
            timePicker1.show(getSupportFragmentManager(),"time picker1");

        }

    });


    mTimePicker = (TextView) findViewById(R.id.inputstarttime);
    mTimePicker1 = (TextView) findViewById(R.id.inputendtime);

2 个答案:

答案 0 :(得分:0)

您可以执行类似的操作

 String callback = "";
    @Override
    public void onTimeSet(TimePicker timePicker, int i, int i1) {

        if (TextUtils.isEmpty(callback))
            return;

        if (callback.equalsIgnoreCase("for_start_time")){
            // set in mTimePicker
        }
        else if (callback.equalsIgnoreCase("for_end_time")){
            // set in mTimePicker1
        }

        //Dont forgot to reset callback
        callback = "";
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date);


        mTimePicker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                callback = "for_start_time";
                DialogFragment timePicker = new TimePickerFragment();
                timePicker.show(getSupportFragmentManager(), "time picker");

            }
        });

        mTimePicker1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                callback = "for_end_time";
                DialogFragment timePicker1 = new TimePicker1Fragment();
                timePicker1.show(getSupportFragmentManager(), "time picker1");

            }

        });


        mTimePicker = (TextView) findViewById(R.id.inputstarttime);
        mTimePicker1 = (TextView) findViewById(R.id.inputendtime);

    }

您需要跟踪活动中呼叫时间选择器的位置。

答案 1 :(得分:0)

请参见以下代码。可能会对您有所帮助。

TimePickerDialog.OnTimeSetListener timepickerlistener1 = new TimePickerDialog.OnTimeSetListener() {
      @Override
      public void onTimeSet(TimePicker timePicker, int i, int i1) {
                        // this is the listener for the first timepicker
                        //get your start time from here
         }
     };

TimePickerDialog.OnTimeSetListener timepickerlistener2 = new TimePickerDialog.OnTimeSetListener() {
      @Override
      public void onTimeSet(TimePicker timePicker, int i, int i1) {
                        // this is the listener for the second timepicker
                        //get your stop time from here
         }
     };
TimePickerDialog mTimePicker = new TimePickerDialog(MainActivity.this, timepickerlistener1, 0, 0, false);//parameters ---- > context, listener for the timepicker, hour, minute, is24hours?
TimePickerDialog mTimePicker1 = new TimePickerDialog(MainActivity.this, timepickerlistener2, 0, 0, false);