Zhao yazhi _ textview custom date

Source: Internet
Author: User

Effect:


Steps for customizing a date:

  1. Set the textview click event. The custom dialog box is displayed.
  2. Override creation dialog box
  3. Prepare the date listener and assign values to the dialog box
Date comparison steps:
  1. Obtain the textview value and determine whether it is null.
  2. Set the displayed Date Format
  3. Use the date. Before () function to determine whether the selected time is before today or whether time 1 is before time 2.

Layout layout:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.android_date"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.android_date.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

Mainactivity

Package COM. example. android_date; import Java. text. parseexception; import Java. text. simpledateformat; import Java. util. calendar; import Java. util. date; import Java. util. locale; import android. app. activity; import android. app. datepickerdialog; import android. app. datepickerdialog. ondatesetlistener; import android. app. dialog; import android. OS. bundle; import android. view. view; import android. widget. datepicker; import android. widget. textview; import android. widget. toast;/*** custom date component ** @ author yazhizhao 11:45:04 42014-10-23 */public class mainactivity extends activity {private int mmonth; private int mday; private int myear; private textview tvdate1; private textview tvdate2; private string tvdate1str; private string tvdate2str; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); tvdate1 = (textview) findviewbyid (R. id. TV _date1); tvdate2 = (textview) findviewbyid (R. id. TV _date2); // assign final calendar c = calendar to the date control. getinstance (); myear = C. get (calendar. year); mmonth = C. get (calendar. month); mday = C. get (calendar. day_of_month); tvdate1.setonclicklistener (new view. onclicklistener () {public void onclick (view v) {// todo auto-generated method stubshowdialog (1) ;}}); tvdate2.setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (view v) {// todo auto-generated method stubshowdialog (2 );}});} // override the create dialog box method protected dialog oncreatedialog (int id) {Switch (ID) {Case 1: // send the activity context, date setting listener, and date year, month, and day as parameters to the new dialog box component return New datepickerdialog (this, DSL, myear, mmonth, mday ); case 2: // send the activity context, date setting listener, and date year, month, and day as parameters to the new dialog box component return New datepickerdialog (this, ds2, myear, mmonth, mday);} return NULL;} // set the listener private ondatesetlistener DSL = new datepickerdialog. ondatesetlistener () {public void ondateset (datepicker view, int year, int monthofyear, int dayofmonth) {myear = year; mmonth = monthofyear; mday = dayofmonth; tvdate1.settext (myear + "-" + (mmonth + 1) + "-" + mday) ;}; // set the listener private ondatesetlistener ds2 = new datepickerdialog. ondatesetlistener () {public void ondateset (datepicker view, int year, int monthofyear, int dayofmonth) {myear = year; mmonth = monthofyear; mday = dayofmonth; tvdate2.settext (myear + "-" + (mmonth + 1) + "-" + mday );}}; /*** click event for date comparison ** @ author yazhizhao 20142014-10-23 11:55:11 * @ Param v */Public void click (view v) {date time1 = NULL; date time2 = NULL; // determine whether time 1 and time 2 are empty. tvdate1str = tvdate1.gettext (). tostring (). trim (); tvdate2str = tvdate2.gettext (). tostring (). trim (); If (tvdate1str = NULL | "". equals (tvdate1str) {toast. maketext (getapplicationcontext (), "Time 1 is not blank", toast. length_short ). show (); return;} else if (tvdate2str = NULL | "". equals (tvdate2str) {toast. maketext (getapplicationcontext (), "Time 2 is not blank", toast. length_short ). show (); return;} // use the "yyyy-mm-dd" format simpledateformat SDF = new simpledateformat ("yyyy-mm-dd", locale. china); try {time1 = SDF. parse (tvdate1str); time2 = SDF. parse (tvdate2str);} catch (parseexception e) {e. printstacktrace () ;}// judge date nowdate = new date () before and after the time 1, 2; If (time1.before (nowdate) {toast. maketext (getapplicationcontext (), "Please select the correct time 1", toast. length_short ). show (); return;} else if (time2.before (time1) {toast. maketext (getapplicationcontext (), "Please select the correct time 2", toast. length_short ). show (); return;} else {toast. maketext (getapplicationcontext (), "OK", toast. length_short ). show ();}}}

Running effect:


Zhao yazhi _ textview custom date

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.