Android [elementary tutorial] Part 1 button control and textview Control

Source: Internet
Author: User

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.

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.