问题 - 有两个日期时间选择器工作 [英] Issues - working with two datetime picker

查看:108
本文介绍了问题 - 有两个日期时间选择器工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让选择使用两个日期和时间选择器(如寄件者和To日期)的日期。如果我选择寄件者我能得到寄件者的TextView的。但是,如果我选择第二个日期选择器(TODATE)更新相同的TextView(寄件者的TextView)。

 公共类F2Activity扩展InfosoftActivity      {
  私人诠释mYear;
  私人诠释mMonth;
  私人诠释MDAY;
  私人诠释mYear2;
  私人诠释mMonth2;
  私人诠释mDay2;
  私人TextView的mDateDisplay;
  私人TextView的mDateDisplay2;
  私人按钮mPickDate;
  私人按钮mPickDate2;
  静态最终诠释DATE_DIALOG_ID = 0;
  静态最终诠释DATE_DIALOG_IDD = 0;
            公共字符串AAA;
            公共字符串BBB;    公共无效的onCreate(捆绑savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setTheme(android.R.style.Theme_Black);    的setContentView(R.layout.activity_f2);    按钮BtnView =(按钮)findViewById(R.id.BtnView);
    按钮BtnDownload =(按钮)findViewById(R.id.BtnDownload);        mDateDisplay =(的TextView)findViewById(R.id.showMyDate);
        mDateDisplay2 =(的TextView)findViewById(R.id.showMyDatee);        mPickDate =(按钮)findViewById(R.id.myDatePickerButton);
        mPickDate2 =(按钮)findViewById(R.id.myDatePickerButton2);        mPickDate.setOnClickListener(新View.OnClickListener()
        {
            公共无效的onClick(视图v){
                的ShowDialog(DATE_DIALOG_ID);
            }
        });        //获取当前日期
        最后的日历C = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(的Calendar.MONTH);
        MDAY = c.get(Calendar.DAY_OF_MONTH);        //显示当前的日期
        updateDisplay();        mPickDate2.setOnClickListener(新View.OnClickListener()
        {
            公共无效的onClick(视图v){
                的ShowDialog(DATE_DIALOG_IDD);
            }
        });
        //获取当前日期
        最终日历C2 = Calendar.getInstance();
        mYear2 = c2.get(Calendar.YEAR);
        mMonth2 = c2.get(的Calendar.MONTH);
        mDay2 = c2.get(Calendar.DAY_OF_MONTH);        //显示当前的日期
        updateDisplayTo();            私人无效updateDisplay()            {
            this.mDateDisplay.setText(新的StringBuilder()
                //月份是基于0,因此加1
            .append(mMonth + 1).append( - )
            .append(MDAY).append( - )
            .append(mYear).append());            }
         私人无效updateDisplayTo()
         {
        this.mDateDisplay2.setText(
        新的StringBuilder()
                //月份是基于0,因此加1
            .append(mMonth2 + 1).append( - )
            .append(mDay2).append( - )
            .append(mYear2).append());    }            私人DatePickerDialog.OnDateSetListener mDateSetListener =新
            DatePickerDialog.OnDateSetListener()            {
    公共无效onDateSet(查看的DatePicker,年整型,诠释monthOfYear,诠释请将dayOfMonth)            {            mYear =年;
            mMonth = monthOfYear;
            MDAY =请将dayOfMonth;
            updateDisplay();            }
        };            @覆盖
            保护对话框onCreateDialog(INT ID){
            开关(ID){
            案例DATE_DIALOG_ID:
            返回新DatePickerDialog(这一点,
                        mDateSetListener,
                        mYear,mMonth,MDAY);
           }
           返回null;
                    }            私人DatePickerDialog.OnDateSetListener mDateSetListener2 =新
                    DatePickerDialog.OnDateSetListener()
            {                @覆盖
                公共无效onDateSet(查看的DatePicker,年整型,诠释
                                    monthOfYear,
                        INT请将dayOfMonth){
                    // TODO自动生成方法存根                      mYear2 =年;
                      mMonth2 = monthOfYear;
                      mDay2 =请将dayOfMonth;
                      updateDisplayTo();                }
            };             受保护的对话框onCreateDialog2(INT ID)                       {
                      开关(ID)                      {
                       案例DATE_DIALOG_IDD:
                       返回新DatePickerDialog(这一点,
                               mDateSetListener2,
                                  mYear2,mMonth2,mDay2);
                      }                        返回null;
                       }                                           }


解决方案

如果你想显示使用的DatePicker然后在您的应用程序没有必要使用两个时间DATE_DIALOG_ID日起超过一次。你可以只用一个,满足您的要求。我在这里贴code我希望它会帮助你。

在点击按钮

  btnFromDate.setOnClickListener(clkListener);
btnToDate.setOnClickListener(clkListener);

然后

 公共OnClickListener clkListener =新OnClickListener()
    {
        @覆盖
        公共无效的onClick(视图v)
        {
            如果(V == btnFromDate)
            {
                showDateDialog(editTextFromDate,Calendar.getInstance());
            }
            如果(V == btnToDate)
            {
                showDateDialog(editTextToDate,Calendar.getInstance());
            }
                }
   };

然后

 的EditText activeDateDisplay;
日历activeDate;公共无效showDateDialog(EditText上dateDisplay,日历日)
    {
        activeDateDisplay = dateDisplay;
        activeDate =日期;
        的ShowDialog(DATE_DIALOG_ID);
    }

然后

 的CharSequence strFormate_Date;
 INT DATE_DIALOG_ID = 0;@覆盖
    保护对话框onCreateDialog(INT ID)
    {
        开关(ID)
        {
            案例DATE_DIALOG_ID:
            {
                返回新DatePickerDialog(Activity.this,mDateSetListener,activeDate.get(Calendar.YEAR),activeDate.get(的Calendar.MONTH),activeDate.get(Calendar.DAY_OF_MONTH));
            }
        }
        返回null;
    }    私人DatePickerDialog.OnDateSetListener mDateSetListener =新DatePickerDialog.OnDateSetListener()
    {
        公共无效onDateSet(查看的DatePicker,年整型,诠释monthOfYear,诠释请将dayOfMonth)
        {
            activeDate.set(Calendar.YEAR,年);
            activeDate.set(的Calendar.MONTH,monthOfYear);
            activeDate.set(Calendar.DAY_OF_MONTH,请将dayOfMonth);
            updateDisplay(activeDateDisplay,activeDate);
            unregisterDateDisplay();
        }
    };
    私人无效unregisterDateDisplay()
    {
        activeDateDisplay = NULL;
        activeDate = NULL;
        strFormate_Date = NULL;
    }    私人无效updateDisplay(EditText上dateDisplay,日历日)
    {
        时间chosenDate =新时代();
        chosenDate.set(date.get(Calendar.DAY_OF_MONTH),date.get(的Calendar.MONTH),date.get(Calendar.YEAR));
        长dtDob = chosenDate.toMillis(真);
        strFormate_Date = DateFormat.format(DD / MM / YYYY,dtDob);
        dateDisplay.setText(+ strFormate_Date);
    }

我希望这将解决您的问题。如果您对此有任何问题,你可以问我。

I'm trying to get selected dates using two date and time picker (like fromdate and To date). If I select fromdate I can get the fromdate to the textview. But if I select second datepicker (todate) update same textview (Fromdate textview).

      public class F2Activity extends InfosoftActivity 

      {
  private int mYear;
  private int mMonth;
  private int mDay;
  private int mYear2;
  private int mMonth2;
  private int mDay2;
  private TextView mDateDisplay;
  private TextView mDateDisplay2;
  private Button mPickDate;
  private Button mPickDate2;
  static final int DATE_DIALOG_ID = 0;
  static final int DATE_DIALOG_IDD = 0;
            public String AAA;
            public String BBB;



    public void onCreate(Bundle savedInstanceState) 
    {
    super.onCreate(savedInstanceState);
    setTheme(android.R.style.Theme_Black);

    setContentView(R.layout.activity_f2);

    Button BtnView=(Button) findViewById(R.id.BtnView);
    Button BtnDownload=(Button) findViewById(R.id.BtnDownload);

        mDateDisplay = (TextView) findViewById(R.id.showMyDate); 
        mDateDisplay2 = (TextView) findViewById(R.id.showMyDatee); 

        mPickDate = (Button) findViewById(R.id.myDatePickerButton);
        mPickDate2 = (Button) findViewById(R.id.myDatePickerButton2);



        mPickDate.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
        });

        // get the current date
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);

        // display the current date
        updateDisplay();



        mPickDate2.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) {
                showDialog(DATE_DIALOG_IDD);
            }
        });


        // get the current date
        final Calendar c2 = Calendar.getInstance();
        mYear2 = c2.get(Calendar.YEAR);
        mMonth2 = c2.get(Calendar.MONTH);
        mDay2 = c2.get(Calendar.DAY_OF_MONTH);

        // display the current date
        updateDisplayTo();

            private void updateDisplay() 

            {
            this.mDateDisplay.setText(new StringBuilder()
                // Month is 0 based so add 1
            .append(mMonth + 1).append("-")
            .append(mDay).append("-")
            .append(mYear).append(" "));

            }
         private void updateDisplayTo() 
         {
        this.mDateDisplay2.setText(
        new StringBuilder()
                // Month is 0 based so add 1
            .append(mMonth2 + 1).append("-")
            .append(mDay2).append("-")
            .append(mYear2).append(" "));

    }



            private DatePickerDialog.OnDateSetListener mDateSetListener = new 
            DatePickerDialog.OnDateSetListener()

            {
    public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth)

            {

            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            updateDisplay();

            }
        };

            @Override
            protected Dialog onCreateDialog(int id) {
            switch (id) {
            case DATE_DIALOG_ID:
            return new DatePickerDialog(this,
                        mDateSetListener,
                        mYear, mMonth, mDay);
           }
           return null;
                    }   



            private DatePickerDialog.OnDateSetListener mDateSetListener2 =new 
                    DatePickerDialog.OnDateSetListener() 
            {

                @Override
                public void onDateSet(DatePicker view, int year, int 
                                    monthOfYear,
                        int dayOfMonth) {
                    // TODO Auto-generated method stub

                      mYear2 = year;
                      mMonth2 = monthOfYear;
                      mDay2 = dayOfMonth;
                      updateDisplayTo();

                }
            };        



             protected Dialog onCreateDialog2 (int id)

                       {
                      switch (id)

                      {
                       case DATE_DIALOG_IDD:
                       return new DatePickerDialog(this,
                               mDateSetListener2,
                                  mYear2, mMonth2, mDay2);
                      }

                        return null;
                       }                        

                                           }

解决方案

If you want to display more than one time of date using DatePicker in your application then no need to use two time DATE_DIALOG_ID. You can use only one and fulfill your requirements. I posted here code I hope it will helps you.

In button click

btnFromDate.setOnClickListener(clkListener);
btnToDate.setOnClickListener(clkListener);

Then

public OnClickListener clkListener = new OnClickListener()
    {
        @Override
        public void onClick(View v) 
        {
            if(v == btnFromDate)
            {
                showDateDialog(editTextFromDate, Calendar.getInstance());
            }
            if(v == btnToDate)
            {
                showDateDialog(editTextToDate, Calendar.getInstance());
            }
                }
   };

Then

EditText activeDateDisplay ;
Calendar activeDate;

public void showDateDialog(EditText dateDisplay, Calendar date) 
    {
        activeDateDisplay = dateDisplay;
        activeDate = date;
        showDialog(DATE_DIALOG_ID);
    }

Then

 CharSequence strFormate_Date;
 int DATE_DIALOG_ID = 0;

@Override
    protected Dialog onCreateDialog(int id) 
    {
        switch (id) 
        {
            case DATE_DIALOG_ID:
            {
                return new DatePickerDialog(Activity.this, mDateSetListener, activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
            }
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() 
    {
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) 
        {
            activeDate.set(Calendar.YEAR, year);
            activeDate.set(Calendar.MONTH, monthOfYear);
            activeDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            updateDisplay(activeDateDisplay, activeDate);
            unregisterDateDisplay();
        }
    };
    private void unregisterDateDisplay() 
    {
        activeDateDisplay = null;
        activeDate = null;
        strFormate_Date = null;
    }

    private void updateDisplay(EditText dateDisplay, Calendar date) 
    {
        Time chosenDate = new Time();
        chosenDate.set(date.get(Calendar.DAY_OF_MONTH), date.get(Calendar.MONTH), date.get(Calendar.YEAR));
        long dtDob = chosenDate.toMillis(true);
        strFormate_Date = DateFormat.format("dd/MM/yyyy", dtDob);
        dateDisplay.setText(""+strFormate_Date);
    }

I hope it will solve your problem. If you have any problem regarding this then you can ask me.

这篇关于问题 - 有两个日期时间选择器工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆