将 MySQL 数据库的默认日期选择器值设置为 NULL

我正在使用日期选择器在 Android Studio 中选择出生日期,但我想确保如果用户不选择 DOB,则 NULL 值将输入到我的数据库中,而不是某个任意日期,甚至不是当前日期. 有没有办法将日期选择器的默认值设置为 NULL?


XML 文件:


<Button

 android:id="@+id/btnChangeDate"

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:text="Date of Birth" />

JAVA代码


public void addListenerOnButton() {


   ChangeDate = (Button) findViewById(R.id.ChangeDate;


   ChangeDate.setOnClickListener(new OnClickListener() {


        @Override

        public void onClick(View v) {

            showDialog(DATE_DIALOG_IDi);

        }

    });

}


@Override

protected Dialog onCreateDialog(int id) {

    switch (id) {

        case DATE_DIALOG_ID:

            // set date picker as current date

            return new DatePickerDialog(this, datePickerListener,

                    year, month,day);

           ;

    }

    return null;

}


private DatePickerDialog.OnDateSetListener datePickerListener

        = new DatePickerDialog.OnDateSetListener() {


    // when dialog box is closed, below method will be called.

    public void onDateSet(DatePicker view, int selectedYear,

                          int selectedMonth, int selectedDay) {

        year = selectedYear;

        month = selectedMonth;

        day = selectedDay;

    }

};


阿波罗的战车
浏览 232回答 1
1回答

慕村225694

尝试将输出变量放在 if 语句中。将默认日期设置为某个值,例如&nbsp; &nbsp;public void setSomeDateOnView() {&nbsp; &nbsp; &nbsp; &nbsp; String mday = "11";&nbsp; &nbsp; &nbsp; &nbsp; String mmonth="11";&nbsp; &nbsp; &nbsp; &nbsp; String myear="2111";&nbsp; &nbsp; &nbsp; &nbsp; //convert them to int&nbsp; &nbsp; &nbsp; &nbsp; day&nbsp; =Integer.valueOf(mday);&nbsp; &nbsp; &nbsp; &nbsp; month =Integer.valueOf(mmonth);&nbsp; &nbsp; &nbsp; &nbsp; year=Integer.valueOf(myear);&nbsp; &nbsp; }然后在 if 语句中使用此值检索 asp 或 php 代码中的值,然后再将其放入数据库例如if ($dateofbirth =="2111-11-11"){&nbsp; &nbsp; &nbsp; $dateofbirth1 = null;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java