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 から選択した時間をより便利なアクティビティに戻す方法を理解できるはずです。(最初の例からいくつかの変更を加えました。)

タイムピッカーフラグメント:

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 に答える