Android - 来自片段

时间:2017-03-24 15:29:16

标签: android android-fragments android-activity

我对TimePickerDialog有疑问。 我使用了一个扩展RelativeLayout我自己的视图(MyViewClass)的类。在这个课程中,我有一个名为&#34的按钮;选择时间"以及显示所选日期的TextView。单击该按钮后,我在mTimePickerFragment.show(((Activity) context).getFragmentManager(), "TAG");中使用MyViewClass显示提到的TimePickerDialog。 MyViewClass包含一个名为setTimeLabel()的方法,用于设置我的TextView。

这是我的TimePickerDialog类:

import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.widget.TimePicker;
import java.util.Calendar;

public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
private int mHour;
private int mMinute;

public TimePickerDialog mTimePickerDialog;


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Calendar c = Calendar.getInstance();

    if (mTimePickerDialog == null) {
        mHour = c.get(Calendar.HOUR_OF_DAY);
        mMinute = c.get(Calendar.MINUTE);
    }

    mTimePickerDialog = new TimePickerDialog(getActivity(), this, mHour, mMinute, DateFormat.is24HourFormat(getActivity()));
    return mTimePickerDialog;
}

public static TimePickerFragment newInstance() {
    return new TimePickerFragment();
}

@Override
public void onTimeSet(TimePicker timePicker, int hour, int minute) {
    init(hour, minute);

/* 
I want to call method which update my TextView
*/
}

public void init(int hour, int minute) {
    mHour = hour;
    mMinute = minute;

    if (mTimePickerDialog != null) {
        mTimePickerDialog.updateTime(mHour, mMinute);
    }
}

public int getHour() {
    return mHour;
}

public int getMinute() {
    return mMinute;
}
}

我需要的是在时间改变时调用方法(置于MyViewClass) - onTimeSet()方法。我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

您可以在onTimeSet中设置时间,例如,如果弹出TimePickerDialog并且用户选择时间,则调用 onTimeSet 方法

 @Override
public void onTimeSet(TimePicker timePicker, int hour, int minute) {
//so you can do anything with the selected time here
//for example
// textViewTime.setText(hour.getText().toString());
// Toast.makeText(getApplicationContext(),"Selected Minute"+minute.getText().toString(),Toast.LengthShort);
init(hour, minute);  

答案 1 :(得分:0)

您无需编写这些内容,例如,当用户单击按钮时

Drawable background = layerDrawable.getDrawable(0);
background = DrawableCompat.wrap(background);
DrawableCompat.setTint(background.mutate(), color);

“设定时间”用它来选择时间。

    new_year = cal.get(Calendar.YEAR);
    new_month = cal.get(Calendar.MONTH);
    new_day = cal.get(Calendar.DAY_OF_MONTH);
    new_hour = cal.get(Calendar.HOUR_OF_DAY);
    new_minute = cal.get(Calendar.MINUTE);
 }

对于datePicker

    TimePickerDialog timePickerDialog = new TimePickerDialog(this,
                    new TimePickerDialog.OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay,
                                              int minute) {
                            _time.setText(hourOfDay+" : "+minute);
                        }
                    }, new_hour, new_minute, false);
            timePickerDialog.show();

您可以在活动中使用new_hour,new_minute,new_day ....