50 Android Development Tips (09 Avoid validating dates with Edittext)
Source: Internet
Author: User
<span id="Label3"></p><span style=""><span style="">as we all know, validating data in a form is boring and easy error.<br>(original address: http://blog.csdn.net/vector_yi/article/details/24424713)<br></span></span>Imagine that there are several types of input in a form that require a date type, and you don't want to do a date type verification of the input information, what would you do? Here's a way to fix this,<span style="color:#CC0000"><span style="color:#CC0000">let the user feel that they are operating in a edittext, but is actually a button, click on the button will pop up a datepicker. </span></span>In order for this to work, we need to change the background of this button so that the user thinks it is a edittext. In fact very easy:<pre code_snippet_id="310086" snippet_file_name="blog_20140424_1_3269118" name="code" class="html"><pre code_snippet_id="310086" snippet_file_name="blog_20140424_1_3269118" name="code" class="html"><button android:id= "@+id/details_date" android:layout_width= "wrap_content" android:layout_height= "wrap_content "android:gravity=" center_vertical "android:background=" @android:d rawable/edit_text "/><!-- Change the background of the button to EditText background--</pre></pre><span style="font-family:Courier; font-size:10px; color:#010101"><span style="font-family:Courier; font-size:10px; color:#010101"><span style="font-size:8pt"><br></span></span></span><span style="font-family:Courier; color:#010101"><span style="font-size:13px">next, just add the Button's click event to the Activity. Let it pop a datepicker and set the selected date to the text of the Button.<p><p></p></p><p><p><br></p></p><pre code_snippet_id="310086" snippet_file_name="blog_20140424_2_7108338" name="code" class="java">Package Com.manning.androidhacks.hack009;import Java.util.calendar;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.view.view.onclicklistener;import Android.widget.button;import Android.widget.datepicker;public class Mainactivity extends Activity {private static Final int date_dialog_id = 0;private int myear;private int mmonth;private int mday;private Ondatesetlistener mDateSetListe Ner;private Button mdatebutton; @Overridepublic void onCreate (Bundle Savedinstancestate) {super.oncreate ( savedinstancestate); Setcontentview (r.layout.main);//get The current datefinal Calendar C = calendar.getinstance (); Myear = c.get (calendar.year); mmonth = c.get (calendar.month); mday = c.get (calendar.day_of_month); MDateSetListener = new Datepickerdialog.ondatesetlistener () {@Overridepublic void ondateset (DatePicker view, int year, int Monthofyear,int dAyofmonth) {mdatebutton.settext (getString (r.string.picked_date_format,monthofyear + 1, dayofmonth, year));}}; Mdatebutton = (Button) Findviewbyid (r.id.details_date); mdatebutton.setonclicklistener (new onclicklistener () {@ overridepublic void OnClick (View v) {showDialog (date_dialog_id);}}); @Overrideprotected Dialog oncreatedialog (int id) {switch (id) {case date_dialog_id:return new Datepickerdialog (this, mdatesetlistener, myear,mmonth-1, mday);} return Super.oncreatedialog (id);}}</pre><br><br><span style="font-family:Courier; color:#010101"><span style="font-family:Courier; color:#010101"><span style="font-size:13px"><br></span></span></span><span style="font-family:Courier; font-size:14px; color:#010101"><span style="font-family:Courier; font-size:14px; color:#010101"><span style="font-size:13px">you may think that edittext and button are inherited from Textview. Why not create a edittext and join Onclicklistener directly?</span></span></span><span style="font-family:Courier; font-size:14px; color:#010101"><span style="font-family:Courier; font-size:14px; color:#010101"><span style="font-size:13px"> <span style="color:#CC0000">however, Button does not agree with the user directly edit the displayed text, obviously make the verification more secure</span> . </span></span></span><span style="font-family:Courier; font-size:14px; color:#010101"><span style="font-family:Courier; font-size:14px; color:#010101"><span style="font-size:13px"><br></span></span></span><span style="font-family:Courier; font-size:14px; color:#010101"><span style="font-family:Courier; font-size:14px; color:#010101"><span style="font-size:13px">you may still be insisting that you can set the editable property of EditText to False. </span></span></span><span style="font-family:Courier; font-size:14px; color:#010101"><span style="font-family:Courier; font-size:14px; color:#010101"><span style="font-size:13px"> <span style="color:#FF0000">But since the meaning of button existence is to be able to click and not change the text Displayed. Why do you want to impose on edittext some attributes that are not appropriate for it to turn it into a button</span>? </span></span></span><span style="font-family:Courier; font-size:14px; color:#010101"><span style="font-family:Courier; font-size:14px; color:#010101"><span style="font-size:13px"><br></span></span></span><span style="font-family:Courier; color:#010101"><span style="font-family:Courier; color:#010101"> <span style="font-size:14px">You can also use Textwatcher to verify the input of edittext, but it's not just boring and time consuming</span> . </span></span> <p><p>50 Android Development Tips (09 Avoid validating dates with Edittext)</p></p></span></span></span>
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