Android Entry notes (i)

Source: Internet
Author: User
Tags android sdk manager

The first part, the building of Android development environment

1. Go to http://www.oracle.com/technetwork/java/javase/downloads/index.html to download the latest version of the JDK and install, configure environment variables.

2. Go to http://www.eclipse.org/downloads/to download the latest version of Eclipse and install it.

3. Download the Android SDK and install it, HTTP://PAN.BAIDU.COM/S/1I3GGWHN

4. Download ADT and install, http://pan.baidu.com/s/1dDhA097, download a zip file, launch eclipse after downloading, menu bar help→install new software→add→name column Enter "ADT ", then click Archive, select the downloaded zip file and click OK, then select the item to install, click Finish. Restart eclipse after installation, you can see two more buttons on the menu bar (as shown in the red box)

5. Then launch the Android SDK Manager, download and install the necessary platform, tools, etc.

Part II, HelloWorld program

Start eclipse,file→new→project→android application Project, you can create a new, empty Hello World app, and start the app screen to show only Hello world. You can connect an Android phone to your computer via USB and then run it on this phone, provided it turns on the USB debug mode and installs apps that allow an unknown source to be installed, or you can use AVD Manager to create a new virtual machine and then let the developed app run on the virtual machine.

The third part, a few simple controls

We want to make the following interface, a text input box, a button.

Open the Activity_main.xml in the Res/layout directory, and the code is modified to resemble the following:

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Horizontal">    <EditTextAndroid:id= "@+id/message"Android:layout_weight= "1"Android:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:hint= "@string/enter_message" />    <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/button_send"Android:onclick= "SendMessage" /></LinearLayout>

The LinearLayout label represents a linear layout, where all controls are distributed vertically or horizontally in a linear layout. When the Android:orientation property value is vertical, all controls are lined up vertically, and all controls are lined up horizontally when the Android:orientation property value is horizontal. In addition to the linear layout, there are framelayout (frame layout), Absolutelayout (absolute layout), relativelayout (relative layout), tablelayout (table layout) of the various layouts, which are not detailed here. It uses a linear layout, and all controls are horizontally distributed, lined up.

Android:layout_width= "match_parent" means that the width of the control is consistent with the parent container width, android:layout_height= "match_parent" means that the control height is consistent with the parent container. So the linearlayout is covered with the whole screen. In previous versions of Android2.2 (API8), Fill_parent was used to indicate that the control was consistent with the parent control's width (height), and Fill_parent was replaced by Match_parent, starting with API8.

The entire layout, there is a edittext (text input box), a button (buttons), they line up horizontally, their height (android:layout_height) are wrap_content (Fit content), how is the width controlled? The weight attribute android:layout_weight is used above. If you set EditText's Android:layout_weight=1,button android:layout_weight=1, the EditText and the button each have a row of 1/ 2; If you set the android:layout_weight=2 of the EditText Android:layout_weight=1,button, the width of the 1/3,button of the edittext width as the free space is 2/3 of the available space. If you do not set the Android:layout_weight property to the control, the default weight of the control is zero, the minimum width of the content is adjusted, and the remaining space is filled by other controls that have the Android:layout_weight property greater than 0. The button is not set to the Android:layout_weight property, and the EditText property is set to 1, so the width of the button will fit the width of the text on the button, and the width of the edittext will fill the remaining space. More strictly, the width of the edittext should be set to 0, if set to Wrap_content, the system accounting content length.

android:hint= "@string/enter_message" indicates the default text for the Set text box.

@string/enter_message indicates that references to resources Res/values/strings.xml name= "Enter_message" in String,strings.xml are described in the following:

<?XML version= "1.0" encoding= "Utf-8"?><Resources>    <stringname= "App_name">HelloWorld</string>    <stringname= "Hello_world">Hello world!</string>    <stringname= "Action_settings">Settings</string>    <stringname= "Enter_message">Enter a string</string>    <stringname= "Button_send">Send</string>    <stringname= "Title_activity_display">DisplayMessage</string></Resources>

As for the android:id, it will be mentioned in the later expansion.

Part IV, initiating another activity and data transfer

Create another activity, called displayactivity, with only one line of text (TextView) to display the contents entered in the Mainactivity Chinese box. The Activity_display.xml code is as follows:

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.levice.helloworld.DisplayActivity" >    <TextViewAndroid:id= "@+id/textview"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/hello_world" /></Relativelayout>

Then, to add a click event to the button in Mainactivity, add the Android:onclick property to the button, set the value to "SendMessage", and add the following code to the Mainactivity.java:

 Public void sendMessage (view view) {       = (EditText) Findviewbyid (r.id.message);        = Edittext.gettext (). toString ();        New Intent (This, displayactivity.  Class);        New Bundle ();       Bundle.putstring ("MSG", message);       Intent.putextras (bundle);       StartActivity (intent);}

The Findviewbyid () function is used to find the desired control by ID;

Intent for starting displayactivity;

Bundles are used to carry data in conjunction with intent to enable the transfer of data between activity.

In Displayactivity.java, the following code is used to extract the information passed and display:

String message = Getintent (). Getextras (). getString ("msg"= (TextView) Findviewbyid (R.id.textview); Textview.settext (message);

Expand One: About @id , @+id , @android: ID

[Email protected]+id/test, indicating a new resource ID;

[email protected]/test, which refers to the resource ID that already exists, usually this ID is defined in the Ids.xml file in the values directory, like this:

<? XML version= "1.0" encoding= "Utf-8" ?> < Resources >         <  type= "id"  name= "Test">false</ Item > </ Resources >

[Email protected]/android:list to refer to the resource ID that already exists in the Android system;

[email protected]:id/list also means referencing the resource ID that already exists in the Android system, usually using the notation above (@id/android:list) instead of using this notation.

More specifically, you can refer to the following content (the content is quoted from http://blog.csdn.net/primer_programer/article/details/23028293).

Components in Android need to be represented by a value of type int, which is the value of the id attribute in the component tag. The id attribute can only accept values of the resource type, which is the value that must begin with @, for example, @id/abc, @+id/xyz, and so on.

Using "+" after @ means that when a layout file has been modified and saved, the corresponding int type variable is automatically generated in the R.java file. The variable name is the value after "/", for example, @+id/xyz generates int xyz = value in the R.java file, where value is a hexadecimal number. If XYZ already has a variable of the same name in R.java, no new variable is generated, and the component uses the value of the existing variable.

That is, if you use the @+id/name form, the component uses the value of the variable as its identity when there is a variable named name in R.java. If the variable does not exist, a new variable is added and the corresponding value is assigned to the variable (no duplicates).

Since the ID property of a component is a resource ID, you can naturally set any resource ID values that already exist, such as @drawable/icon, @string/ok, @+string/you, and so on. Of course, you can also set the existing resource ID on the Android system, for example, the landlord proposed @id/android:list, then what does this Android mean, actually, This android is the package that the system's R class (in the R.java file) resides in. We can enter android.r.id in the Java Code Editor, and the corresponding resource ID will be listed, for example, you can also set the ID property value to @id/android:message.

<android:id= "@+id/android:message"android:layout_width= "Wrap_ Content "android:layout_height=" wrap_content "/>

There is another way to view the ID defined in the system, go to the <android SDK installation directory >\platforms\android-1.5\data\res\values directory, find the Ids.xml file, open, the content is as follows:

<?XML version= "1.0" encoding= "Utf-8"?><Resources><Itemtype= "id"name= "Price_edit">False</Item><Itemtype= "id"name= "Amount_edit">False</Item></Resources>

If you define an ID in Ids.xml, you can define @id/price_edit in layout as follows, otherwise @+id/price_edit.

expand two: About Android handling of Click events

There are generally four ways to handle click events.

The first kind,

Button BTN =  // get pushbutton btn.setonclicklistener (new  Button.onclicklistener () {     publicvoid  OnClick (view view) {    //// the action to be taken after writing the click button here     }}) ;

The second kind,

Button BTN =  // get buttons  new  Button.onclicklistener () {    public  void  OnClick (view view) {    // the action to be taken after writing the click button here     }});

Third, this method is provided by the SDK above Android 1.6 (API 4). In the layout file, add the onclick attribute to the control to which you want to add the click event, for example:

< Button     Android:id = "@+id/button1"     android:onclick= "Button1_onclick"    />

Then add a function Button1_onclick (view view) to the corresponding class file, as follows:

 Public void   Button1_onclick (view view) {    // Click to execute code }

The above is the general three kinds of writing, in fact, the first and the second can be regarded as the same method, but the second type is divided into two pieces in the write, the first merger of the writing, the principle is the same. The third way is what I like more.

In addition, if you add the onclick event to TextView in the third Way, add the android:clickable= "true" attribute to the TextView's OnClick property, otherwise it will be invalid.

Correct example:

< TextView     Android:id = "@+id/textview1"     android:clickable= "true"    android:onclick= "Button1_onclick"   />

Fourth, let the activity implement the Onclicklistener interface, and then use Button.setonclicklistener (this) to go back to the callback, this method is only theoretical feasible, do not recommend this writing, This writing is not practical.

Android Entry notes (i)

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.