点击时不会打开Datepicker对话框

时间:2015-10-06 01:43:23

标签: java android

当我运行并单击该按钮时,它不会打开数据贴纸对话框。我无法找出不正确的内容。请,任何人都可以检查并指导我在程序中的错误代码在哪里。

   {
      //Declaration for class
        ButtonViews views;
        dpListener dpListenerView;
        // Declartion for member vairables
        int day, month, x_year;
        int hour;
        int minute;
        Calendar calendar;

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

            views = new ButtonViews();
            dpListenerView = new dpListener();

            //ButtonListener
            views.button_date.setOnClickListener(this);
            views.button_time.setOnClickListener(this);
            //

           // pick up the default date using Calender class
             calendar =  GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.getDefault());

            day = calendar.get(Calendar.DAY_OF_MONTH);
            month = calendar.get(Calendar.MONTH);
            x_year = calendar.get(Calendar.YEAR);
            Log.i("DAY in Num....."," "+ month);

            hour = calendar.get(Calendar.HOUR_OF_DAY);
            minute = calendar.get(Calendar.MINUTE);

            setupDate(day, month, x_year);
            setupTime(hour, minute);
       }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.button_date:
                    showDatePickerDialog();
                    break;
                case R.id.button_time:
                      showTimePickerDialog();
                    break;
            }
        }


        private void setupTime(int hours, int minutes) {
            views.button_time.setText(hours + ":" + minutes);
        }

        private void setupDate(int day, int month, int year) {
            String strMonth = ((month+1) <=9) ? ("0" + (month+1)) : String.valueOf(month+1);
            views.button_date.setText(String.valueOf(day) + "/" + strMonth + "/" + String.valueOf(year));
        }

        private void showDatePickerDialog() {
            DatePickerDialog datepickerdialog = new DatePickerDialog
                    (
                            this,
                             dpListenerView,
                           /* new DatePickerDialog.OnDateSetListener() {
                                @Override
                                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                } },*/
                            //this,

                            x_year,
                            month,
                            day
                    );


        }


     /*   private OnDateSetListener dpListener = new OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                   *//* day = dayOfMonth;
                    month = monthOfYear;
                    x_year = year;*//*
                    setupDate(dayOfMonth,monthOfYear,year);
                }

            };*/


        public void showTimePickerDialog() {

          TimePickerDialog   timePickerDialog = new TimePickerDialog(
                    DateTimePickerActivity.this,
                    this,
                    hour,
                    minute,
                    true
             );
            calendar.set(Calendar.HOUR, hour);
            calendar.set(Calendar.MINUTE, minute);

            timePickerDialog.show();
        }

    //    @Override
    //    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    //        setupDate(dayOfMonth,monthOfYear,year);
    //    }

        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            setupTime(hourOfDay, minute);
        }


       class ButtonViews {

            Button button_time;
            Button button_date;

            public ButtonViews() {
                button_date = (Button) findViewById(R.id.button_date);
                button_time = (Button) findViewById(R.id.button_time);
            }
        }

      class dpListener implements OnDateSetListener

      {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                   /* day = dayOfMonth;
                    month = monthOfYear;
                    x_year = year;*/

                setupDate(dayOfMonth,monthOfYear,year);
            }

        }
    }

1 个答案:

答案 0 :(得分:4)

 @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_date:
                showDialog(999);// Method for Show Dialog. You can use any Int
                break;
            case R.id.button_time:
                  showDialog(99);
                break;
        }
    }

并从int。

为Identified对话框定义此方法
@Override
protected Dialog onCreateDialog(int id) {
    // TODO Auto-generated method stub
    if (id == 999) {
        return new DatePickerDialog(this, myDateListener, yr, month, day);
    } else if (id == 99) {
        return new TimePickerDialog(this, myTimeListener, h, m, true);
    }
    return null;
}

还有听众

// For Date Picker
private DatePickerDialog.OnDateSetListener myDateListener = new OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        // TODO Auto-generated method stub
        day = dayOfMonth;
        month = monthOfYear + 1;
        yr = year;
        Toast.makeText(
                MainActivity.this,
                new StringBuilder().append(day).append("-").append(month)
                        .append("-").append(yr), Toast.LENGTH_LONG).show();
    }
};
// For TimePicker
private TimePickerDialog.OnTimeSetListener myTimeListener = new OnTimeSetListener() {

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // TODO Auto-generated method stub
        h = hourOfDay;
        m = minute;
        Toast.makeText(MainActivity.this,
        new StringBuilder().append(h).append(":").append(m),
        Toast.LENGTH_LONG).show();
    }
};