这是我的 kotlin 文件
class MainActivity : AppCompatActivity() {
var datePicker: DatePickerDialog? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val c = Calendar.getInstance()
val year = c.get(Calendar.YEAR)
val month = c.get(Calendar.MONTH)
val day = c.get(Calendar.DAY_OF_MONTH)
calen_box.setOnClickListener {
datePicker = DatePickerDialog(this, DatePickerDialog.OnDateSetListener { view: DatePicker?, year: Int, month: Int, dayOfMonth: Int ->
daytext.setText(String.format("%2d", day))
monthtext.setText(String.format("%2d", (month+1)))
yeartext.setText(String.format("%d", day))
}, year, month, day)
datePicker!!.datePicker.maxDate = c.timeInMillis
datePicker!!.show()
}
}
我的xml文件
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/colorAccent"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/calen_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/rect">
<TextView
android:id="@+id/daytext"
android:layout_width="90dp"
android:layout_height="match_parent"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/line"
android:layout_gravity="center"/>
<TextView
android:id="@+id/monthtext"
android:layout_width="130dp"
android:layout_height="match_parent"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/line"
android:layout_gravity="center"/>
<TextView
android:id="@+id/yeartext"
android:layout_width="130dp"
android:layout_height="match_parent"
/>
</LinearLayout>
当我点击 datepicker 时,它只选择今天的日期,不能设置未来的日期,而且它会以个位数获取日期。我需要日期为“YYYY-MM-DD”格式无法找出我在那段代码中做错了什么,你能帮我解决我的问题吗