从Time Picker对话框和Date Picker对话框中获取数据

时间:2016-04-01 01:12:19

标签: java android

我需要帮助。在mycode上有一个日期选择器对话框和时间选择器对话框。我的问题是,我很困惑从它们获取数据,并放在

上的int声明
int year
int month
int day
int year
int month 
int day

并替换dpDate和dpTime的功能。

package org.etauhid.newalarm;

import android.app.AlarmManager;
import android.app.DatePickerDialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;

import java.text.DateFormat;
import java.util.Calendar;

import java.util.GregorianCalendar;


public class MainActivity extends AppCompatActivity {

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

        Toast.makeText(getApplicationContext(), "HAI", Toast.LENGTH_LONG).show();
        OnClickListener setClickListener = new OnClickListener() {
            DateFormat fmtDateAndTime =
                    DateFormat.getDateTimeInstance();

            Calendar dateAndTime = Calendar.getInstance();
            DatePickerDialog.OnDateSetListener d =
                    new DatePickerDialog.OnDateSetListener() {

                        public void onDateSet(DatePicker view, int year, int month, int day) {

                            dateAndTime.set(Calendar.YEAR, year);
                            dateAndTime.set(Calendar.MONTH, month);
                            dateAndTime.set(Calendar.DAY_OF_MONTH, day);
// updateLabel();
                        }
                    };
            TimePickerDialog.OnTimeSetListener t =
                    new TimePickerDialog.OnTimeSetListener() {

                        public void onTimeSet(TimePicker view, int jam, int menit) {

                            dateAndTime.set(Calendar.HOUR_OF_DAY, jam);
                            dateAndTime.set(Calendar.MINUTE, menit);
                            //updateLabel();
                        }
                    };

            @Override
            public void onClick(View v) {
                /** This intent invokes the activity DemoActivity, which in turn opens the AlertDialog window */
                Intent i = new Intent(getBaseContext(), DemoActivity.class);

                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                /** Creating a Pending Intent */
                PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 0, i, 0);

                ;

                /** Getting a reference to the System Service ALARM_SERVICE */
                AlarmManager alarmManager = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE);

                /** Getting a reference to DatePicker object available in the MainActivity */
                DatePicker dpDate = (DatePicker) findViewById(R.id.dp_date);

                /** Getting a reference to TimePicker object available in the MainActivity */
                TimePicker tpTime = (TimePicker) findViewById(R.id.tp_time);



                int year = dpDate.getYear();
                int month = dpDate.getMonth();
                int day = dpDate.getDayOfMonth();

                int hour = tpTime.getCurrentHour();
                int minute = tpTime.getCurrentMinute();

                /** Creating a calendar object corresponding to the date and time set by the user */
                GregorianCalendar calendar = new GregorianCalendar(year, month, day, hour, minute);

                /** Converting the date and time in to milliseconds elapsed since epoch */
                long alarm_time = calendar.getTimeInMillis();

                /** Setting an alarm, which invokes the operation at alart_time */
                alarmManager.set(AlarmManager.RTC_WAKEUP, alarm_time, operation);

                /** Alert is set successfully */
                Toast.makeText(getBaseContext(), "Alarm is set successfully", Toast.LENGTH_SHORT).show();

            }
        };

        OnClickListener quitClickListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        };

        Button btnSetAlarm = (Button) findViewById(R.id.btn_set_alarm);
        btnSetAlarm.setOnClickListener(setClickListener);


        Button btnQuitAlarm = (Button) findViewById(R.id.button);
        btnQuitAlarm.setOnClickListener(quitClickListener);

    }




}

所以在对话框打开后,用户设置数据。 int声明可以从以前用户设置的数据中获取数据,并且可以由GreorgianCalendar提取。

提前致谢!

0 个答案:

没有答案