Android Development Learning notes-defining events for a button

Source: Internet
Author: User

Learn the first day of Android, learn about the various layouts, then draw a button yourself and then give the button defined an event that pops up a message that says "I succeeded!" The following is the exact process:

1, modify the layout file Activity_main.xml add the following statement (I deleted the original text box)

<button android:id= "@+id/button1"
android:onclick= "Button_Click"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/hello_world"
/>

Here also to modify a file is a character file, in order to better manage the development to use the characters, Android put all the character set into a file inside, android:text= "@string/hello_world" This is the expression to use the character file in a variable "Hello_world", that is, res/values/string.xml this file with a <string name= "Hello_world" > OK </ String>

2, the above sentence android:onclick= "Button_Click" This is the event to define the response to this button, and then I added a method named "Button_Click" in Mainactivity.java,

One thing to note here is that the parameter of this method is a view-type class. This method code is given below:

public void Button_Click (View v)
{
Button temp = (button) v;

Toast.maketext (This, "I succeeded! ", Toast.length_short). Show ();

}

It says "I did it!" on the screen. "Although only to show a word, but see this sentence when very excited, finally wrote the first own Android application, although did not realize any function, hey."

Android Development Learning notes-defining events for a button

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.