其他分享
首页 > 其他分享> > android – 如何读取TimePicker选择的值?

android – 如何读取TimePicker选择的值?

作者:互联网

我遵循了这个指南: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
            }
        }

解决方法:

改写
我结合了我们讨论过的开发人员指南中的两个例子.现在,您应该能够理解如何将用户从TimePickerFragment中选择的时间发送回更有用的Activity. (我从第一个例子做了一些改变.)

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;
}

标签:android,timepicker
来源: https://codeday.me/bug/20190715/1468718.html