Time selector for scheduled SMS apps (1) and SMS app Selector

Source: Internet
Author: User

Time selector for scheduled SMS apps (1) and SMS app Selector
DatePicker class diagram

 

Main Method

public void init(int year,                 int monthOfYear,                 int dayOfMonth,                 DatePicker.OnDateChangedListener onDateChangedListener)
Class TimePicker class diagram

setOnTimeChangedListenerpublic void setOnTimeChangedListener(TimePicker.OnTimeChangedListener onTimeChangedListener)

 

This article mainly implements a time Selector Used by the combination of DatePicker and TimePicker, and provides a foreshadowing for the scheduled sending time of subsequent text messages.

Let's talk about the code.

 

First look at the main. xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >        <Button        android:id="@+id/dateButton"        android:layout_width="match_parent"        android:layout_height="40dip"        android:layout_marginTop="20dip"        android:text="year-month-day"        android:textSize="18dip"        android:background="@drawable/bg"        android:onClick="pickSendDate"        />        <Button        android:id="@+id/timeButton"        android:layout_below="@id/dateButton"        android:layout_width="match_parent"        android:layout_height="40dip"        android:layout_marginTop="1dip"        android:text="hour:minute"        android:textSize="18dip"        android:background="@drawable/bg"        android:onClick="pickSendTime"        />        <DatePicker        android:id="@+id/datePicker"        android:layout_below="@id/timeButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_marginTop="20dp"        android:startYear="2014"        android:endYear="2020"        android:visibility="gone"        />            <TimePicker        android:id="@+id/timePicker"        android:layout_below="@id/timeButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_marginTop="20dp"         android:visibility="gone"        /></RelativeLayout>

The overall layout is relative. Be sure to use android: layout_centerHorizontal = "true" for horizontal control ". Instead of android: layout_gravity = "center_vertical" (This method is effective in LinearLayout layout ). DatePicker can be set to the minimum and maximum years, which is very convenient. The setOnclickButtonListener in the Code is not used for Button control monitoring. android: onClick is used directly, followed by the method name. (Both methods are supported)

Hard coding exists in the Code. Pay attention to it in actual projects. Please ignore it here.

 

Main Interface MainActivity

MainActivity. java

Package com. beny. pickdate; import java. text. simpleDateFormat; import java. util. date; import android. app. activity; import android. OS. bundle; import android. view. menu; import android. view. view; import android. widget. button; import android. widget. datePicker; import android. widget. datePicker. onDateChangedListener; // listens for date changes import android. widget. timePicker; import android. widget. timePicker. onTimeChangedListener; // listener time change public class MainActivity extends Activity {private int year; private int month; private int day; private int hour; private int minute; private TimePicker timePicker = null; private DatePicker datePicker = null; private Button timeButton = null; private Button dateButton = null; protected String [] time = null; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); timeButton = (Button) findViewById (R. id. timeButton); dateButton = (Button) findViewById (R. id. dateButton); timePicker = (TimePicker) findViewById (R. id. timePicker); timePicker. setIs24HourView (true); // set the default time to the current system time./* You can also use the getCurrentHour () and getCurrentMinute () Methods of TimePicker to obtain the current time */time = getCurrentTime (); timePicker. setCurrentHour (Integer. valueOf (time [0]); timePicker. setCurrentMinute (Integer. valueOf (time [1]); // set date datePicker = (DatePicker) findViewById (R. id. datePicker); datePicker. setCalendarViewShown (false); // sets the default datetime timeButton on the button. setText (timePicker. getCurrentHour () + ":" + timePicker. getCurrentMinute (); dateButton. setText (datePicker. getYear () + "year" + (datePicker. getMonth () + 1) + "month" + datePicker. getDayOfMonth () + "day"); // set the date selector to be visible to datePicker by default. setVisibility (View. VISIBLE); dateButton. setBackgroundResource (R. color. button_pressed_bg); // sets the listener timePicker. setOnTimeChangedListener (new SendTimeChangedListener (); datePicker. init (datePicker. getYear (), datePicker. getMonth () + 1, datePicker. getDayOfMonth (), new SendDateChangedListener ();} public String [] getCurrentTime () {// obtain the current hour minute to SimpleDateFormat curTimeFormat = new SimpleDateFormat ("HH-mm "); string curTime = curTimeFormat. format (new Date (); String [] time = new String [2]; time = curTime. split ("-"); return time;} public String [] getCurrentDate () {// obtain the year, month, and day of the current date SimpleDateFormat curDateFormat = new SimpleDateFormat ("yyyy-MM-dd"); String curDate = curDateFormat. format (new Date (); String [] date = new String [3]; date = curDate. split ("-"); return date;} public void pickSendDate (View view) {datePicker. setVisibility (View. VISIBLE); timePicker. setVisibility (View. INVISIBLE); timeButton. setBackgroundResource (R. color. button_bg); dateButton. setBackgroundResource (R. color. button_pressed_bg);} public void pickSendTime (View view) {datePicker. setVisibility (View. INVISIBLE); timePicker. setVisibility (View. VISIBLE); timeButton. setBackgroundResource (R. color. button_pressed_bg); dateButton. setBackgroundResource (R. color. button_bg);} // TODO // set whether the date is earlier than the current date private boolean isBeforeCurDate () {return false ;} // TODO // set whether the time is earlier than the current time private boolean isBeforeCurTime () {return false;} class SendTimeChangedListener implements OnTimeChangedListener {@ Override public void onTimeChanged (TimePicker view, int hourOfDay, int minute) {timeButton. setText (hourOfDay + ":" + minute); MainActivity. this. hour = hourOfDay; MainActivity. this. minute = minute;} class SendDateChangedListener implements OnDateChangedListener {@ Override public void onDateChanged (DatePicker view, int year, int month, int day) {dateButton. setText (year + "year" + (month + 1) + "month" + day + "day"); MainActivity. this. year = year; MainActivity. this. month = month; MainActivity. this. day = day ;}// create Menu @ Override public boolean onCreateOptionsMenu (menu) {// Inflate the Menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}

Note that the month must be added with 1. Otherwise, the month is incorrect.

 

Color. xml

<?xml version="1.0" encoding="utf-8"?><resources>    <color name="button_pressed_bg">#DD5D04</color>    <color name="button_bg">#F7F7F7</color></resources>

 

Bg. xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" >    <item android:state_pressed="true"        android:drawable="@color/button_pressed_bg"        />    <item        android:drawable="@color/button_bg"        /></selector>

 

1. By default, the date button is selected by default to display the DatePicker view. The current date of the system is loaded by default.

 

 

2. Click the Time Button Control and switch the view to TimePicker. By default, the system time is loaded.

 

 

3. when the date or time is selected up or down, the text information in the date Button control or the Time Button control changes accordingly, thanks to the onDateChanged (DatePicker view, int year, int month, int day) and onTimeChanged (TimePicker view, int hourOfDay, int minute) methods.

 

 


IPHONE timed text messaging APP? The simpler the login, the better.

Has the landlord escaped from jail? It should be difficult or almost impossible to avoid jailbreak.
After all, IOS and Android are different from closed-source systems.
After jailbreak, you can use biteSMS to solve this problem.
Hope to help you ~

An Android app that regularly sends text messages

Go SMS.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.