TimePicker在每个TextView中显示相同的时间

时间:2016-03-29 08:01:56

标签: android dialog timepicker android-timepicker

我正在开发一个模块,我必须允许用户在不同的文本视图中设置3个不同的时间,因为我使用的是TimePicker。

但每当我在TimePicker中选择时间时,它会在其他两个TextView中显示相同的时间。

那可能是什么原因?

private String textViewTime1, textViewTime2, textViewTime3;
private String mTime1, mTime2, mTime3;

// On clicking Time picker
    public void setTime(View v){
        Calendar now = Calendar.getInstance();
        TimePickerDialog tpd = TimePickerDialog.newInstance(
                this,
                now.get(Calendar.HOUR_OF_DAY),
                now.get(Calendar.MINUTE),
                false
        );
        tpd.setThemeDark(false);
        tpd.show(getFragmentManager(), "Timepickerdialog");
    }

// Obtain time from time picker
    @Override
    public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) {
        mHour = hourOfDay;
        mMinute = minute;
        if (minute < 10) {
            mTime1 = hourOfDay + ":" + "0" + minute;
            mTime2 = hourOfDay + ":" + "0" + minute;
            mTime3 = hourOfDay + ":" + "0" + minute;
        } else {
            mTime1 = hourOfDay + ":" + minute;
            mTime2 = hourOfDay + ":" + minute;
            mTime3 = hourOfDay + ":" + minute;
        }
        textViewTime1.setText(mTime1);
        textViewTime2.setText(mTime2);
        textViewTime3.setText(mTime3);
    }

3 个答案:

答案 0 :(得分:2)

您可能错过了应该使用回调定位的人,因此您可以覆盖所有文本视图。你应该使用类似的东西:

if(view belongs to textViewTime1)
    textViewTime1.setText(mTime1);
if(view belongs to textViewTime2)
    textViewTime2.setText(mTime2);
if(view belongs to textViewTime3)
    textViewTime3.setText(mTime3);

如果没有view实际是什么以及它是如何连线的信息,那么如果条件允许我就无法帮助你。

我对ReminderAddActivityTimePickerDialog进行了快速审核,您需要将TimePickerDialog应用于每个独立的文字视图。最后使用多次控制而不是将它们一起黑客攻击。如果不重写您正在使用的库,它就不会以这种方式工作。

答案 1 :(得分:0)

  

但每当我在TimePicker中选择时间时,它在其他时间显示相同的时间   两个TextViews。

因为,您正在同时设置它们!

@Override
    public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) {
        mHour = hourOfDay;
        mMinute = minute;
        if (minute < 10) {
            /// Time set to mTime1
            mTime1 = hourOfDay + ":" + "0" + minute;
            /// Time set to mTime2 same
            mTime2 = hourOfDay + ":" + "0" + minute;
            /// Time set to mTime3 same
            mTime3 = hourOfDay + ":" + "0" + minute;
        } else {
            /// Time set to mTime1
            mTime1 = hourOfDay + ":" + minute;
            /// Time set to mTime2 same
            mTime2 = hourOfDay + ":" + minute;
            /// Time set to mTime3 same
            mTime3 = hourOfDay + ":" + minute;
        }
        textViewTime1.setText(mTime1);
        textViewTime2.setText(mTime2);
        textViewTime3.setText(mTime3);
    }

答案 2 :(得分:0)

//从时间选择器中获取时间

 @Override
    public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) {
        mHour = hourOfDay;
        mMinute = minute;

因为每次在一个方法中设置所有三次并在文本视图中设置它。 仔细检查并重新编码。

    if (minute < 10) {
            mTime1 = hourOfDay + ":" + "0" + minute;
            mTime2 = hourOfDay + ":" + "0" + minute;
            mTime3 = hourOfDay + ":" + "0" + minute;
        } else {
            mTime1 = hourOfDay + ":" + minute;
            mTime2 = hourOfDay + ":" + minute;
            mTime3 = hourOfDay + ":" + minute;
        }
        textViewTime1.setText(mTime1);
        textViewTime2.setText(mTime2);
        textViewTime3.setText(mTime3);
    }