Javaでは、DatePickerDialogを次のように使用できます。
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(getActivity(),new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
// Display Selected date in textbox
lblDate.setText(""+ dayOfMonth+" "+MONTHS[monthOfYear] + ", " + year);
}
}, year, month,day);
dpd.show();
KotlinのDatePickerDialogの使用はどのように見えますか?
次のようになります。
val c = Calendar.getInstance()
val year = c.get(Calendar.YEAR)
val month = c.get(Calendar.MONTH)
val day = c.get(Calendar.DAY_OF_MONTH)
val dpd = DatePickerDialog(activity, DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
// Display Selected date in textbox
lblDate.setText("" + dayOfMonth + " " + MONTHS[monthOfYear] + ", " + year)
}, year, month, day)
dpd.show()
これは、コードをコピーしてAndroid studioのkotlinファイルに貼り付けるだけです。これを使用することを強くお勧めします。
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val textView: TextView = findViewById(R.id.textView_date)
textView.text = SimpleDateFormat("dd.MM.yyyy").format(System.currentTimeMillis())
var cal = Calendar.getInstance()
val dateSetListener = DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
cal.set(Calendar.YEAR, year)
cal.set(Calendar.MONTH, monthOfYear)
cal.set(Calendar.DAY_OF_MONTH, dayOfMonth)
val myFormat = "dd.MM.yyyy" // mention the format you need
val sdf = SimpleDateFormat(myFormat, Locale.US)
textView.text = sdf.format(cal.time)
}
textView.setOnClickListener {
DatePickerDialog(this@MainActivity, dateSetListener,
cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH)).show()
}
}
}
Kotlin Anko Android例:
alert {
isCancelable = false
lateinit var datePicker: DatePicker
customView {
verticalLayout {
datePicker = datePicker {
maxDate = System.currentTimeMillis()
}
}
}
yesButton {
val parsedDate = "${datePicker.dayOfMonth}/${datePicker.month + 1}/${datePicker.year}"
}
noButton { }
}.show()
ボタンのクリック時に日付の選択を表示する
val cal = Calendar.getInstance()
val y = cal.get(Calendar.YEAR)
val m = cal.get(Calendar.MONTH)
val d = cal.get(Calendar.DAY_OF_MONTH)
val datepickerdialog:DatePickerDialog = DatePickerDialog(activity, DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
// Display Selected date in textbox
lblDate.setText("" + dayOfMonth + " " + MONTHS[monthOfYear] + ", " + year)
}, y, m, d)
datepickerdialog.show()
これは、ボタンのクリック時にタイムピッカーを表示するために使用されます
textView54.setOnClickListener {
val c:Calendar= Calendar.getInstance()
val hh=c.get(Calendar.HOUR_OF_DAY)
val mm=c.get(Calendar.MINUTE)
val timePickerDialog:TimePickerDialog=TimePickerDialog(this@VendorRegistration,TimePickerDialog.OnTimeSetListener { view, hourOfDay, minute ->
textView54.setText( ""+hourOfDay + ":" + minute);
},hh,mm,true)
timePickerDialog.show()