验证"从日期和QUOT;和"到目前为止"在android系统 [英] Validating "from date" and "to date" in android

查看:135
本文介绍了验证"从日期和QUOT;和"到目前为止"在android系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在哪个用户有权利选择两个日期的申请即─从日期以日期。无论领域不应该接受任何日期小于当前date.I通过在本网站上一些职位已经过去了,用单日期选择器对话框,并单ondatesetListener实现了这一功能。

我的问题是,我无法验证的条件日期应该从日比总是大于或等于 。请帮助我,我很无力捕捉到的从日期作为活动的日期是按活动结束日期超越控制在advance.Following由于是code:

 公共类Leave_form扩展活动
{
    私有静态的EditText tvDislpayResult;
    私人按钮startPickDate;
    私人按钮endPickDate;
    私人日历的startDate;
    私人日历结束日期;
    //私人的EditText startDateDisplay;
    //私人的EditText endDateDisplay;
    静态最终诠释DATE_DIALOG_ID = 0;
    私人TextView的startDateDisplay;
    私人TextView的endDateDisplay;
    私人TextView的activeDateDisplay;
    私人日历activeDate;
    私人日历的currentdate;
    私人日历寄件者;    保护无效的onCreate(捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_leave_form);        / *捕捉我们的开始日期功能视图元素* /
        startDateDisplay =(EditText上)findViewById(R.id.e_from_date);
        startPickDate =(按钮)findViewById(R.id.Set_date1);        / *获取tday日期* /        的currentdate = Calendar.getInstance();
        Log.d(的currentdate \",\"\"+(currentDate.get(Calendar.DAY_OF_MONTH))+(currentDate.get(Calendar.MONTH)+1)+(currentDate.get(Calendar.YEAR)));        / *获取当前日期* /
        的startDate = Calendar.getInstance();
        Log.d(STARTDATE \",\"\"+(startDate.get(Calendar.DAY_OF_MONTH))+(startDate.get(Calendar.MONTH)+1)+(startDate.get(Calendar.YEAR)));        / *添加一个点击监听器按钮* /
        startPickDate.setOnClickListener(新View.OnClickListener()
        {
            公共无效的onClick(视图v)
            {
                showDateDialog(startDateDisplay,的startDate);
                Log.d(的startDate上点击\",\"\"+(startDate.get(Calendar.DAY_OF_MONTH))+(startDate.get(Calendar.MONTH)+1)+(startDate.get(Calendar.YEAR)));            }
        });        / *捕捉我们为结束日期功能视图元素* /
        endDateDisplay =(EditText上)findViewById(R.id.e_to_date);
        endPickDate =(按钮)findViewById(R.id.Set_date2);        / *获取当前日期* /
        结束日期= Calendar.getInstance();
        Log.d(结束日期\",\"\"+(endDate.get(Calendar.DAY_OF_MONTH))+(endDate.get(Calendar.MONTH)+1)+(endDate.get(Calendar.YEAR)));        / *添加一个点击监听器按钮* /
        endPickDate.setOnClickListener(新View.OnClickListener()
        {
            公共无效的onClick(视图v)
            {
                showDateDialog(endDateDisplay,结束日期);
                Log.d(结束日期-on click\",\"\"+(endDate.get(Calendar.DAY_OF_MONTH))+(endDate.get(Calendar.MONTH)+1)+(endDate.get(Calendar.YEAR)));
            }
        });        / *显示当前的日期(此方法如下)* /
       // updateDisplay(startDateDisplay,的startDate);
       // updateDisplay(endDateDisplay,结束日期);
    }    私人无效updateDisplay(TextView的dateDisplay,日历日)
    {
        dateDisplay.setText(
                新的StringBuilder()
                    //月份是基于0,因此加1
                    .append(date.get(Calendar.DAY_OF_MONTH)).append( - )
                    .append(date.get(的Calendar.MONTH)+1).append( - )
                    。.append(date.get(Calendar.YEAR))附加());        Log.d(\"msg\",\"date:\"+(date.get(Calendar.DAY_OF_MONTH))+(date.get(Calendar.MONTH)+1)+(date.get(Calendar.YEAR)));        寄件者=日期;
    }    公共无效showDateDialog(TextView的dateDisplay,日历日)
    {
        Log.d(SDD,dateDisplay.getText()的toString());
        Log.d(STARTDATE \",\"\"+(currentDate.get(Calendar.DAY_OF_MONTH))+(currentDate.get(Calendar.MONTH)+1)+(currentDate.get(Calendar.YEAR)));
       activeDateDisplay = dateDisplay;
        activeDate =日期;
        的ShowDialog(DATE_DIALOG_ID);
    }    私人OnDateSetListener dateSetListener =新OnDateSetListener()
    {
        @燮pressWarnings(德precation)
        @覆盖
        公共无效onDateSet(查看的DatePicker,年整型,诠释monthOfYear,诠释请将dayOfMonth){
            activeDate.set(Calendar.YEAR,年);
            activeDate.set(的Calendar.MONTH,monthOfYear);
            activeDate.set(Calendar.DAY_OF_MONTH,请将dayOfMonth);
            Log.d(activeDate上dateset \",\"\"+(activeDate.get(Calendar.DAY_OF_MONTH))+(activeDate.get(Calendar.MONTH)+1)+(activeDate.get(Calendar.YEAR)));            如果(currentDate.after(activeDate)|(currentDate.equals(activeDate)))
            {
                吐司面包= Toast.makeText(Leave_form.this,请选择一个有效的日期,Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER,0,0);
                toast.show();
            }
            其他
            {
                if((currentDate.after(activeDate)|(currentDate.equals(activeDate)))&&(startDateDisplay.getText().toString()!=null))
                {
                    Log.d(的startDate上点击\",\"\"+(startDate.get(Calendar.DAY_OF_MONTH))+(startDate.get(Calendar.MONTH)+1)+(startDate.get(Calendar.YEAR)));
                    如果(startDate.before(activeDate))
                    {
                        updateDisplay(activeDateDisplay,activeDate);
                    }                    其他
                    {
                        Toast.makeText(getApplicationContext()中输入有效日期,Toast.LENGTH_SHORT).show();
                        / * AlertDialog alertDialog =新AlertDialog.Builder(Leave_form.this).create();                        alertDialog.setMessage(选择有效日期);                        alertDialog.setButton(OK,新DialogInterface.OnClickListener(){                            @覆盖
                            公共无效的onClick(DialogInterface对话,诠释它){
                                // TODO自动生成方法存根                            }
                        });
                        alertDialog.show(); * /
                    }
                }
                其他
                {
                    updateDisplay(activeDateDisplay,activeDate);
                }           }            unregisterDateDisplay();
        }
    };    私人无效unregisterDateDisplay()
    {
        Log.d(STARTDATE \",\"\"+(currentDate.get(Calendar.DAY_OF_MONTH))+(currentDate.get(Calendar.MONTH)+1)+(currentDate.get(Calendar.YEAR)));
        Log.d(自日期, - + activeDate.DAY_OF_MONTH + activeDate.MONTH + activeDate.YEAR);
        activeDateDisplay = NULL;
        activeDate = NULL;
    }    @覆盖
    保护对话框onCreateDialog(INT ID){
        开关(ID){            案例DATE_DIALOG_ID:
                Log.d(寄件者,消息);
                Log.d(ID,+ DATE_DIALOG_ID);
                返回新DatePickerDialog(这一点,dateSetListener,activeDate.get(Calendar.YEAR),activeDate.get(的Calendar.MONTH),activeDate.get(Calendar.DAY_OF_MONTH));
        }
        返回null;
    }    @覆盖
    在prepareDialog(INT ID,对话的对话){保护无效
        super.on prepareDialog(ID,对话);
        开关(ID){
            案例DATE_DIALOG_ID:
                Log.d(ID,+ DATE_DIALOG_ID);
                ((DatePickerDialog)对话框).updateDate(activeDate.get(Calendar.YEAR),activeDate.get(的Calendar.MONTH),activeDate.get(Calendar.DAY_OF_MONTH));
                打破;
        }
    }    公共无效离开(查看视图)
    {
        意图int​​ent1 =新意图(这一点,Rough.class);
        的setContentView(R.layout.activity_rough);
        startActivity(intent1);
    }
    公共无效注销(查看视图)
    {
        意图int​​ent2 =新意图(这一点,MainActivity.class);
        的setContentView(R.layout.activity_main);
        startActivity(intent2);
    }
}


解决方案

这是更好地改变这种codeS:

<$p$p><$c$c>if((currentDate.after(activeDate)|(currentDate.equals(activeDate)))&&(startDateDisplay.getText().toString()!=null))

要多,如果像块:

  IF((currentDate.after(activeDate)||(currentDate.equals(activeDate))
    如果(!startDateDisplay.getText()。的toString()。等于(NULL))

请注意仔细区别!

另外,也要看看这个 这里回答

I an application in which user has to choose two dates namely- from date and to date.Both the fields should not accept any date less than current date.I have gone through some posts in this website and achieved this functionality by using single datepicker dialog and single ondatesetListener.

my problem is that i am unable to validate the condition that to date should be always greater than or equal to from date.please help me.I am unable to capture the from date as the active date is overide by the active end date Thanks in advance.Following is the code:

public class Leave_form extends Activity 
{       
    private static EditText tvDislpayResult;
    private Button startPickDate;
    private Button endPickDate;
    private Calendar startDate;
    private Calendar endDate;
    //private EditText startDateDisplay;
    //private EditText endDateDisplay;
    static final int DATE_DIALOG_ID = 0;
    private TextView startDateDisplay;
    private TextView endDateDisplay;
    private TextView activeDateDisplay;
    private Calendar activeDate;
    private Calendar currentDate;
    private Calendar fromDate;

    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leave_form);

        /*  capture our View elements for the start date function   */
        startDateDisplay = (EditText) findViewById(R.id.e_from_date);
        startPickDate = (Button) findViewById(R.id.Set_date1);

        /*get tday date  */   

        currentDate=Calendar.getInstance();
        Log.d("currentDate ",""+(currentDate.get(Calendar.DAY_OF_MONTH))+(currentDate.get(Calendar.MONTH)+1)+(currentDate.get(Calendar.YEAR)));

        /* get the current date */
        startDate = Calendar.getInstance();
        Log.d("startdate ",""+(startDate.get(Calendar.DAY_OF_MONTH))+(startDate.get(Calendar.MONTH)+1)+(startDate.get(Calendar.YEAR)));

        /* add a click listener to the button   */
        startPickDate.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v) 
            {
                showDateDialog(startDateDisplay, startDate);
                Log.d("startDate-on click ",""+(startDate.get(Calendar.DAY_OF_MONTH))+(startDate.get(Calendar.MONTH)+1)+(startDate.get(Calendar.YEAR)));

            }
        });

        /* capture our View elements for the end date function */
        endDateDisplay = (EditText) findViewById(R.id.e_to_date);
        endPickDate = (Button) findViewById(R.id.Set_date2);

        /* get the current date */
        endDate = Calendar.getInstance();
        Log.d("endDate ",""+(endDate.get(Calendar.DAY_OF_MONTH))+(endDate.get(Calendar.MONTH)+1)+(endDate.get(Calendar.YEAR)));

        /* add a click listener to the button   */
        endPickDate.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                showDateDialog(endDateDisplay, endDate);
                Log.d("endDate -on click",""+(endDate.get(Calendar.DAY_OF_MONTH))+(endDate.get(Calendar.MONTH)+1)+(endDate.get(Calendar.YEAR)));
            }
        });

        /* display the current date (this method is below)  */
       // updateDisplay(startDateDisplay, startDate);
       // updateDisplay(endDateDisplay, endDate);
    }

    private void updateDisplay(TextView dateDisplay, Calendar date) 
    {
        dateDisplay.setText(
                new StringBuilder()
                    // Month is 0 based so add 1
                    .append(date.get(Calendar.DAY_OF_MONTH) ).append("-")
                    .append(date.get(Calendar.MONTH)+1).append("-")
                    .append(date.get(Calendar.YEAR)).append(" "));

        Log.d("msg","date:"+(date.get(Calendar.DAY_OF_MONTH))+(date.get(Calendar.MONTH)+1)+(date.get(Calendar.YEAR)));

        fromDate=date;
    }

    public void showDateDialog(TextView dateDisplay, Calendar date)
    {
        Log.d("SDD", dateDisplay.getText().toString());
        Log.d("startdate ",""+(currentDate.get(Calendar.DAY_OF_MONTH))+(currentDate.get(Calendar.MONTH)+1)+(currentDate.get(Calendar.YEAR)));
       activeDateDisplay = dateDisplay;
        activeDate = date;
        showDialog(DATE_DIALOG_ID);
    }

    private OnDateSetListener dateSetListener = new OnDateSetListener()
    {
        @SuppressWarnings("deprecation")
        @Override
        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);
            Log.d("activeDate-on dateset ",""+(activeDate.get(Calendar.DAY_OF_MONTH))+(activeDate.get(Calendar.MONTH)+1)+(activeDate.get(Calendar.YEAR)));

            if(currentDate.after(activeDate)|(currentDate.equals(activeDate)))
            {
                Toast toast=Toast.makeText(Leave_form.this, "Please select a valid date", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();


            }
            else
            {
                if((currentDate.after(activeDate)|(currentDate.equals(activeDate)))&&(startDateDisplay.getText().toString()!=null))
                {
                    Log.d("startDate-on click ",""+(startDate.get(Calendar.DAY_OF_MONTH))+(startDate.get(Calendar.MONTH)+1)+(startDate.get(Calendar.YEAR)));
                    if(startDate.before(activeDate))
                    {
                        updateDisplay(activeDateDisplay, activeDate);
                    }

                    else
                    {
                        Toast.makeText(getApplicationContext(), "enter valid date", Toast.LENGTH_SHORT).show();
                        /*AlertDialog alertDialog=new AlertDialog.Builder(Leave_form.this).create();

                        alertDialog.setMessage("Choose a valid date");

                        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub

                            }
                        });
                        alertDialog.show();*/
                    }
                }
                else
                {
                    updateDisplay(activeDateDisplay, activeDate);
                }

           }

            unregisterDateDisplay();
        }
    };

    private void unregisterDateDisplay() 
    {
        Log.d("startdate ",""+(currentDate.get(Calendar.DAY_OF_MONTH))+(currentDate.get(Calendar.MONTH)+1)+(currentDate.get(Calendar.YEAR)));
        Log.d("from date:","-"+activeDate.DAY_OF_MONTH+activeDate.MONTH+activeDate.YEAR);
        activeDateDisplay = null;
        activeDate = null;
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {

            case DATE_DIALOG_ID:
                Log.d("fromdate","msg");
                Log.d("id",""+DATE_DIALOG_ID);
                return new DatePickerDialog(this, dateSetListener, activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
        }
        return null;
    }

    @Override
    protected void onPrepareDialog(int id, Dialog dialog) {
        super.onPrepareDialog(id, dialog);
        switch (id) {
            case DATE_DIALOG_ID:
                Log.d("id",""+DATE_DIALOG_ID);
                ((DatePickerDialog) dialog).updateDate(activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
                break;
        }
    }

    public void leave(View view)
    {
        Intent intent1=new Intent(this,Rough.class);
        setContentView(R.layout.activity_rough);
        startActivity(intent1);
    }
    public void logout(View view)
    {
        Intent intent2=new Intent (this,MainActivity.class);
        setContentView(R.layout.activity_main);
        startActivity(intent2);
    }
}

解决方案

It is better to change such codes:

if((currentDate.after(activeDate)|(currentDate.equals(activeDate)))&&(startDateDisplay.getText().toString()!=null))

to multiple if blocks like:

if((currentDate.after(activeDate)||(currentDate.equals(activeDate))
    if(!startDateDisplay.getText().toString().equals(null))

Note carefully the differences!

Also look at this answer here

这篇关于验证&QUOT;从日期和QUOT;和&QUOT;到目前为止&QUOT;在android系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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