Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Android to implement date and time selection dialog box

Shulou Source: shulou.com Published: 2022-06-03 15:31:51 11月14日 Update

This article mainly explains "how to use Android to achieve date and time selection dialog box", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to use Android to achieve date and time selection dialog box"!

The use of date / time selection dialogs (DatePickerDialog and TimePickerDialog) for your reference, the details are as follows

Public class MainActivity extends Activity {/ / instantiate controls private Button dateButton; private Button timeButton; private EditText editText; private DatePickerDialog dateDialog; private TimePickerDialog timeDialog; private int year, monthOfYear, dayOfMonth, hourOfDay, minute; @ Override protected void onCreate (Bundle savedInstanceState) {/ / TODO Auto-generated method stub super.onCreate (savedInstanceState); setContentView (R.layout.main); / / find controls dateButton = (Button) findViewById (R.id.button1) through findViewById; timeButton = (Button) findViewById (R.id.button2) EditText = (EditText) findViewById (R.id.edit); / / obtain the information of year, month, day, hour and minute through Calendar object Calendar calendar = Calendar.getInstance (); year = calendar.get (calendar.YEAR); monthOfYear = calendar.get (calendar.MONTH); dayOfMonth = calendar.get (calendar.DAY_OF_MONTH); hourOfDay = calendar.get (calendar.HOUR_OF_DAY); minute = calendar.get (calendar.MINUTE) / * * instantiate DatePickerDialog * / dateDialog = new DatePickerDialog (this, new OnDateSetListener () {@ Override public void onDateSet (DatePicker arg0, int year, int monthOfYear, int dayOfMonth) {/ / display the acquired date in the text box, and the month is counted from 0, so add 1 String text = year + "-" (monthOfYear + 1) + "-" + dayOfMonth; editText.setText (text);}}, year, monthOfYear, dayOfMonth) / / the next three parameters correspond to the above year, month, day / * set the listening event on the date selector button * / dateButton.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View arg0) {/ / the date selector button displays the date dialog box dateDialog.show ();}}) / * * instantiate TimePickerDialog * / timeDialog = new TimePickerDialog (this, new OnTimeSetListener () {@ Override public void onTimeSet (TimePicker view, int hourOfDay, int minute) {/ / TODO Auto-generated method stub Toast.makeText (MainActivity.this, hourOfDay + ":" + minute, Toast.LENGTH_LONG). Show ();}, hourOfDay, minute, true) / / whether the last parameter setting is a 24-hour system / * set listening events on the time selector button * / timeButton.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View arg0) {/ / the time selector button displays the time dialog box timeDialog.show ();}}) }} at this point, I believe you have a deeper understanding of "how to use Android to achieve date and time selection dialog box". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Date selection time dialog box dialogue button content instance event parameter control time display learning monitoring practical deeper three information interest practicality Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Information NVidia Docker vpn