本文介绍了Android的日期选择器监听器不工作一次以上..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我有两个日期选择器的活动,起初我用这些语句初始化它们

 公共类生日延伸活动{日历C = Calendar.getInstance();
    INT currentYear = c.get(Calendar.YEAR);
    INT currentMonth = c.get(的Calendar.MONTH);
    INT CURRENTDAY = c.get(Calendar.DAY_OF_MONTH);
  公共无效的onCreate(捆绑savedInstanceState)
{ super.onCreate(savedInstanceState); 的setContentView(R.layout.birthdate);    DatePicker的birthDayDatePicker,periodDatePicker;
            birthDayDatePicker =(的DatePicker)findViewById(R.id.DatePickerBirthDay);
            periodDatePicker =(的DatePicker)findViewById(R.id.DatePickerPeriod);
             periodDatePicker.init(currentYear,currentMonth,CURRENTDAY,新OnDateChangedListener()
            {                @覆盖
                公共无效onDateChanged(的DatePicker为arg0,ARG1 INT,INT ARG2,诠释ARG3){
                    // TODO自动生成方法存根
                    birthDateCalculations();
                }
            });            birthDayDatePicker.init(currentYear,currentMonth,CURRENTDAY,新OnDateChangedListener(){                @覆盖
                公共无效onDateChanged(的DatePicker为arg0,ARG1 INT,INT ARG2,诠释ARG3){
                    // TODO自动生成方法存根                    periodDateCalculations();                    }
            });    }}

我意识到,那些听众只发生过一次,好像是我在日期选择器再次点击,,报表将不会被应用..
我想办法,我点击日期选择器每次有一个监听器是活动的。

在此先感谢!


解决方案

  birthDayDatePicker =(的DatePicker)findViewById(R.id.DatePickerBirthDay);
        periodDatePicker =(的DatePicker)findViewById(R.id.DatePickerPeriod);        //初始化期间日期选择器与当前的日期和日期变化监听
        periodDatePicker.init(currentYear,currentMonth,CURRENTDAY,新OnDateChangedListener()
        {            @覆盖
            公共无效onDateChanged(DatePicker的periodDatePicker,诠释currentYear,诠释currentMonth,诠释CURRENTDAY){
                // TODO自动生成方法存根
                数= 1;
                birthDayDatePicker.init(currentYear,currentMonth,CURRENTDAY,NULL);
                birthDateCalculations();            }
        });        //初始化出生日期选择器与当前的日期和日期变化监听
        birthDayDatePicker.init(currentYear,currentMonth,CURRENTDAY,新OnDateChangedListener(){            @覆盖
            公共无效onDateChanged(DatePicker的birthDayDatePicker,诠释currentYear,诠释currentMonth,诠释CURRENTDAY){
                // TODO自动生成方法存根
                数= 2;
                periodDatePicker.init(currentYear,currentMonth,CURRENTDAY,NULL);
                periodDateCalculations();                }
        });}>大段引用

I have an activity in which I have two date pickers, at first I initialise them by those statements

    public class birthDate extends Activity{

Calendar c = Calendar.getInstance();
    int currentYear = c.get(Calendar.YEAR); 
    int currentMonth = c.get(Calendar.MONTH);
    int currentDay = c.get(Calendar.DAY_OF_MONTH);
  public void onCreate(Bundle savedInstanceState) 
{

 super.onCreate(savedInstanceState);  

 setContentView(R.layout.birthdate);

    DatePicker birthDayDatePicker,periodDatePicker;
            birthDayDatePicker = (DatePicker)findViewById(R.id.DatePickerBirthDay);
            periodDatePicker = (DatePicker)findViewById(R.id.DatePickerPeriod);
             periodDatePicker.init(currentYear, currentMonth, currentDay, new OnDateChangedListener()
            {

                @Override
                public void onDateChanged(DatePicker arg0, int arg1, int arg2,int arg3) {
                    // TODO Auto-generated method stub
                    birthDateCalculations();
                }
            });

            birthDayDatePicker.init(currentYear, currentMonth, currentDay, new OnDateChangedListener() {

                @Override
                public void onDateChanged(DatePicker arg0, int arg1, int arg2, int arg3) {
                    // TODO Auto-generated method stub

                    periodDateCalculations();

                    }
            });

    }}

I realised that those listeners only happened once, as if I clicked again on the date picker,, the statements won't be applied .. I want a way to have a listener that is active every time I click on the date picker.

Thanks in advance !

解决方案
 birthDayDatePicker = (DatePicker)findViewById(R.id.DatePickerBirthDay);
        periodDatePicker = (DatePicker)findViewById(R.id.DatePickerPeriod);

        //initialize period date picker with current date and on date change listener 
        periodDatePicker.init(currentYear, currentMonth, currentDay, new OnDateChangedListener()
        {

            @Override
            public void onDateChanged(DatePicker periodDatePicker, int currentYear, int currentMonth,int currentDay) {
                // TODO Auto-generated method stub
                Number = 1;
                birthDayDatePicker.init(currentYear, currentMonth, currentDay, null);
                birthDateCalculations();

            }
        });

        //initialize birth date picker with current date and on date change listener 
        birthDayDatePicker.init(currentYear, currentMonth, currentDay, new OnDateChangedListener () {

            @Override
            public void onDateChanged(DatePicker birthDayDatePicker, int currentYear, int currentMonth, int currentDay) {
                // TODO Auto-generated method stub
                Number = 2;
                periodDatePicker.init(currentYear, currentMonth, currentDay, null);
                periodDateCalculations();

                }
        });

}

> Blockquote

这篇关于Android的日期选择器监听器不工作一次以上..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 07:28