Getting Started with Android

Source: Internet
Author: User

Learning Android, excerpt from the first line of code--android

Layout management

Layout management through an XML file.

Android:id= "@+id/button_1" defines a unique identifier for the current element, @+id for the definition, @id for reference; Android:text for its contents; Android:layout_width and Android_ Height controls its width and height

Setcontentview ()

Loads the layout, passing in the ID of a layout file. such as Setcontentview (R.layout.first_layout)

Registering in the Androidmanifest file

The registration statement for the event should be placed in the <application> tab and registered through the <activity> tag. Use Android:name to specify which activity to register specifically, and use Android:label to specify the contents of the title bar in the activity. To have an activity as the main activity for a program, you need to add <action android:name= "Android.intent.action.MAIN"/> and <category android:name= " Android.intent.category.LAUNCHER "/>.

Programs that do not have a primary activity are generally used as third-party services for other applications to invoke internally.

Hide title bar

Join Requestwindowfeature (Window.feature_no_title) before Setcontentview ()

Settings for buttons

Button button1 = (button) Findviewbyid (r.id.button_1); //Generate a button object by a defined r.id.button_1. Findviewbyid () Returns the View object

Menus in the event

Build the./res/menu, under which the Android XML file is set. Create a menu item from the <item> tab (Android:id assigns a unique identifier to the menu item, Android:title assigns a name to the menu item)

by Getmenuinflater (). Inflate (A, b) to create a menu for the current activity, the first parameter is used to specify which resource file to create the menu from, and the second parameter is used to specify which menu object our item will be added to, returning true (public Boolean Oncreateoptionsmenu (menu), which indicates that the menus created are allowed to be displayed.

Destruction activities

Finish (), the effect is the same as pressing the back key

Use intent to travel between activities

The usage of intent is divided into explicit intent and implicit intent.

Explicit: The first parameter is context, the second parameter is the target activity

Intent Intent = new Intent (firstactivity.this, Secondactivity.class);
StartActivity (Intent);

Implicit: Specify a series of more abstract actions (which indicate which action the current activity can respond to) and category (additional information), and leave it to the system to analyze the intent and help us find the right activity to start. By configuring <intent-filter> under (androidmanifest) <activity> tags, you can specify the action and category that the current activity responds to. This activity should intent only if the contents of <action> and <category> match the action and category specified in intent. You can specify only one action per intent, but you can specify multiple category.

Androidmanifest.xml

<activity android:name= ". Secondactivity ">
<intent-filter>
<action android:name= "Com.example.activitytest.ACTION_START"/>
<category android:name= "Android.intent.category.DEFAULT/>
</intent-filter>
</activity>

Firstactivity.java

Intent Intent = new Intent ("Com.example.activitytest.ACTION_START");
StartActivity (Intent);

Using implicit intent, you can start activities for other programs

Passing data between activities

Pass data down: The parent Activity Intent.putextra (,), the first parameter is the key, and the second parameter is a value.

Sub-activity: Intent Intent = Getintent (); Gets the intent that is used to start the activity

String str = Intent.getstringextra (); parameter is key

Pass data up: Parent activity Startactivityforresult (intent,) the second parameter is a request code

Child activity: Call Setresult (,) the first parameter is used to return processing results to the previous activity, typically using only RESULT_OK and result_canceled, and the second parameter passes the data

So in the parent activity to add onactivityresult () to get the returned data

When returning with back, you can override Onbackpressed () {}

Getting Started with Android

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.