4

我遵循了本指南: http: //developer.android.com/guide/topics/ui/controls/pickers.html 我不太确定这个(我对 android dev 很陌生)我有一个包我的mainActivity 类和 timepicker 类。我希望在设置时间时将其写入 mainactivity 的变量,或者相反,这样做“onTimeSet”将值保存为类变量并从我的 mainActivity 中读取这些变量。我怎么做?

这是我的片段类:

package com.example.remoteswitch;

import java.util.Calendar;

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;

public class TimePickerFragment extends DialogFragment
                            implements TimePickerDialog.OnTimeSetListener {

    int hour, min;
    @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, true);
    }

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // Do something with the time chosen by the user
        hour = hourOfDay;
        min = minute;

    }
}

在我的 MainActivity 类中,我有一个在按下按钮时调用的函数:

public void showTimePickerDialog(View v) {
            //DialogFragment newFragment = new TimePickerFragment();
            TimePickerFragment newFragment = new TimePickerFragment();
            newFragment.show(getFragmentManager(), "timePicker"); 

            //todo: read values from timePicker
            int pickerHour = 0, pickerMin = 0;
            //some manipulations on this data
            }
        }
4

2 回答 2

14

重写
我结合了我们讨论的开发人员指南中的两个示例。现在您应该能够了解如何将用户从 TimePickerFragment 中选择的时间发送回更有用的 Activity。(我对第一个示例进行了一些更改。)

时间选择器片段:

public class TimePickerFragment extends DialogFragment {
    // Notice I removed "implements OnTimeSetListener" and changed the variables

    private Activity mActivity;
    private OnTimeSetListener mListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = activity;

        // This error will remind you to implement an OnTimeSetListener 
        //   in your Activity if you forget
        try {
            mListener = (OnTimeSetListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnTimeSetListener");
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        final Calendar c = Calendar.getInstance();
        hour = c.get(Calendar.HOUR_OF_DAY);
        minute = c.get(Calendar.MINUTE);

        // Create a new instance of TimePickerDialog and return it

        // I made a couple changes here!
        return new TimePickerDialog(mActivity, mListener, hour, minute,
                DateFormat.is24HourFormat(mActivity));
    }
}

主要活动:

// add "implements OnTimeSetListener" to "public class MainActivity ..."
private int pickerHour = 0;
private int pickerMin = 0;

// onCreate() and your other methods...

public void showTimePickerDialog(View v) {
    TimePickerFragment newFragment = new TimePickerFragment();
    newFragment.show(getFragmentManager(), "timePicker"); 
}    

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    // Do something with the time chosen by the user
    pickerHour = hourOfDay;
    pickerMin = minute;
}
于 2013-02-22T16:05:36.673 回答
0

使用下面的答案。这对我有用。

接口类:

public interface OnTimeSetListener {
    void onTimeSet(String time);
}

活动类 onClick:

        DialogFragment newFragment = new TimePickerFragment(getActivity(), this);
        newFragment.show(getActivity().getSupportFragmentManager(), "timePicker");

覆盖接口监听器方法:

    @Override
    public void onTimeSet(String time) {
        // TODO Auto-generated method stub
//do something here to set the values
    }
于 2015-08-28T12:02:55.483 回答