I have always wanted to write something, but I don't know what to write. I have been learning Android development. I just took this opportunity to practice writing. Well, let's say, today I want to learn about the android button control and textview control. What ?? Do you still have to build an android development platform? Please go to Baidu or Google.
The button control has event listening. If you want to handle click events, you need to register a listener for the button control. Now, let's take a look at today's code, first of all main. xml.
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <textview Android: id = "@ + ID/text" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "@ string/Hello"/> <button Android: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: text = "button" Android: Id = "@ + ID/button"> </button> </linearlayout>
A button control is added and an ID (Android: Id = "@ + ID/button") is set for it. What? Why do I need to set the ID? This is to facilitate the discovery of the control in Main. XML in the activity and create events or processes for the corresponding control. You don't want to click the control when there is nothing at all, right? Now let's take a look at the code in the activity:
Import Java. util. date; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. textview; public class buttondemoactivity extends activity implements onclicklistener {private textview text = NULL;/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // find main by ID. textview control text = (textview) findviewbyid (R. id. text); // find main by ID. button button = (button) findviewbyid (R. id. button); // Add and click the listener button for the button control. setonclicklistener (this);}/*** for Main. click to listen to all controls in XML. Of course, you must register a listener for the control. Example: button. setonclicklistener (this); * // @ overridepublic void onclick (view v) {updatetime ();} private void updatetime () {// set the text in the text control. settext (new date (). tostring ());}}
OK. After the code is written, let the simulator run it. Click each button to see if the textview above has changed? Is it easy? From today on, we have officially learned about Android development.