Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report