Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement a date-time Selector imitating iPhone by Android

Shulou Source: shulou.com Published: 2022-05-31 22:50:53 09月21日 Update

This article introduces the relevant knowledge of "how to achieve Android imitating iPhone date and time selector". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Take a look at the effect picture first.

How to use

Import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity {WheelMain wheelMain; EditText txttime; DateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd") Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.main); txttime = (EditText) findViewById (R.id.txttime); Calendar calendar = Calendar.getInstance (); txttime.setText (calendar.get (Calendar.YEAR) + "-" + (calendar.get (Calendar.MONTH) + 1) + "-" + calendar.get (Calendar.DAY_OF_MONTH) + "); Button btnselecttime = (Button) findViewById (R.id.button1) Btnselecttime.setOnClickListener (new OnClickListener () {@ Override public void onClick (View arg0) {LayoutInflater inflater = LayoutInflater. From (MainActivity.this); final View timepickerview = inflater.inflate (R.layout.timepicker, null); ScreenInfo screenInfo = new ScreenInfo (MainActivity.this); wheelMain = new WheelMain (timepickerview, true); wheelMain.screenheight = screenInfo.getHeight (); String time = txttime.getText (). ToString (); Calendar calendar = Calendar.getInstance () If (JudgeDate.isDate (time, "yyyy-MM-dd")) {try {calendar.setTime (dateFormat.parse (time));} catch (ParseException e) {/ / TODO Auto-generated catch block e.printStackTrace ();}} int year = calendar.get (Calendar.YEAR); int month = calendar.get (Calendar.MONTH); int day = calendar.get (Calendar.DAY_OF_MONTH) Int h = calendar.getTime (). GetHours (); int m = calendar.getTime (). GetMinutes (); wheelMain.initDateTimePicker (year, month, day, h, m) New AlertDialog.Builder (MainActivity.this) .setTitle ("Select time") .setView (timepickerview) .setPositiveButton ("OK", new DialogInterface.OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {txttime.setText (wheelMain.getTime () ) .setNegativeButton ("cancel", new DialogInterface.OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {}) .NegativeButton (). Show ();}});}} "how Android implements an iPhone-like date and time selector" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Time selection date content more knowledge practicality achievement next dilemma reality situation effect effect article case editing website industry process Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn OPPO Reno NVidia Redmi Microsoft