如何从一种方法访问参数到另一种方法

时间:2017-07-12 10:13:26

标签: java android

我在这个程序中遇到了问题,我想将DatePickerFragment的参数访问到TimePickerFragment.Please帮助中。

public class datepicker extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_datepicker);

    Button btn = (Button) findViewById(R.id.datepick);
    Button btn2 = (Button) findViewById(R.id.timepick);

    //  TextView textview = (TextView) findViewById(R.id.textView1);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DialogFragment dFragment = new DatePickerFragment();

            // Show the date picker dialog fragment
            dFragment.show(getFragmentManager(), "Date Picker");
        }
    });

    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            DialogFragment dFragment = new TimePickerFragment();

            // Show the time picker dialog fragment
            dFragment.show(getFragmentManager(),"Time Picker");


        }
    });


}

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog dpd = new DatePickerDialog(getActivity(),
                AlertDialog.THEME_HOLO_DARK, this, year, month, day);


        return dpd;
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the chosen date


        Button textview = (Button) getActivity().findViewById(R.id.datepick);

        textview.setText(day + ":" + (month + 1) + ":" + year);
    }
}




public  class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener{

    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Get a Calendar instance
        final Calendar calendar = Calendar.getInstance();
        // Get the current hour and minute
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);

        TimePickerDialog tpd = new TimePickerDialog(getActivity(),
                AlertDialog.THEME_DEVICE_DEFAULT_LIGHT, this, hour, minute, false);
        return tpd;
    }

    public void onTimeSet(TimePicker timePicker, int i, int i1) {
        Button tv = (Button) getActivity().findViewById(R.id.timepick);
        tv.setText(i + ":" + i1);

        DatePickerFragment b = new DatePickerFragment();
        b.onDateSet(view ,int year,int month,int day);

    }
}}

我需要在TimePickerFragment方法中使用年,月,日。请尽快解决。谢谢。

0 个答案:

没有答案