Android将TimePicker Fragment的结果发送到Activity的自定义对话框

时间:2014-06-02 10:36:24

标签: android android-fragments

大家好我需要将TimePicker Fragment的结果发送到我自己的Activity自定义对话框。我的自定义对话框有一个edittext,我需要在该edittext中显示结果。有没有办法做到这一点?

这是我迄今为止所尝试过的:

TimePickerFragment

public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        // use the current time as the default values for the picker
        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
        return new TimePickerDialog( getActivity(), this, hour, _minute, DateFormat.is24HourFormat(getActivity()) ); 
    }

    TheListener listener;

    public interface TheListener{
        public void returnTime(String time);
    }

    @SuppressLint("SimpleDateFormat")
    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute)
    {   

        String am_pm;

        if (hourOfDay > 12) {
            hour = hourOfDay - 12;
            am_pm = "PM";
        } else {
            hour = hourOfDay;
            am_pm = "AM";
        }

        String time = + hour + ":" + minute + " " + am_pm;

        Log.e("TimePicker", "Time = " + hour + ":" + minute + " " + am_pm);

        if (listener != null) 
        {
          listener.returnTime(time); 

        }

        //EditText activityButton = (EditText)getActivity().findViewById(R.id.etActualTimeOfArrival);
        //activityButton.setText( time );

    }

MainActivity

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    // TODO onItemClick

            // custom dialog
            final Dialog dialog = new Dialog(this);
            dialog.setContentView(R.layout.routingdetail_update);
            dialog.setTitle("Update Routing Detail");

            // To restrict users to dismiss dialog without choosing an event
            dialog.setCancelable(false);
            etActualTimeOfArrival = (EditText) dialog.findViewById (R.id.etActualTimeOfArrival);

            Button dialogUpdate = (Button) dialog.findViewById(R.id.btUpdate);
            Button dialogCancel = (Button) dialog.findViewById(R.id.btCancel);



            etActualTimeOfArrival.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    showTimePickerForArrivalTime();
                    etActualTimeOfArrival.setText(strArrivalTime);

                }
            });

            // if button is clicked, close the custom dialog
            dialogUpdate.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    dialog.dismiss();

                }
            });


            dialogCancel.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });


            dialog.show();



}

@Override
public void returnTime(String time) {
    // TODO Auto-generated method stub
    strArrivalTime = time;

    Log.e("Set Dialog", time);
}

我已经尝试过界面但它无法正常工作。有任何想法吗?我们将不胜感激。感谢。

0 个答案:

没有答案