Androidstudio study notes-first Android app

Source: Internet
Author: User

To bring an undergraduate to a project that has something to do with Android, so take advantage of the opportunity to learn how to write an Android program.

The first material comes from Google official, Portal: https://developer.android.com/training/basics/firstapp/index.html. Because the official routine writing is very clear, basically as long as the steps are good, so do not intend to complete the copy and paste, the main practice of my understanding and thinking.

1. Create a new project

The hierarchy of an Android program is application, many activities (can be understood as a number of interfaces), Layout + title + Menu

After the new project as will automatically generate many files, important files include:

1. The app/src/main/res/layout/activity_my.xml format is an XML layout file. As support directly through the text to modify the display interface, but also provides a GUI, can be directly dragged to complete the interface editing.

2. App/src/main/res/layout/content_my.xml The file is activity_my.xml called. A detailed description of layout and some settings.

3. App/src/main/java/com.mycompany.myfirstapp/myactivity.java Jave core code, including the actual activity class and its class functions. When the app runs, the activity class runs and calls the preceding layout file to display.

4. Configuration of the basic features of the App/src/main/androidmanifest.xml app and what components are included (activity)

5. App/build.gradle gradle scripts are used to compile and build apps for easy handling of issues such as version and dependency libraries.

6. The app/src/main/res/directory includes all the resources used in the app:

drawable-<density>/includes all picture resources except the start icon

Layout includes each activity's layout file

Menu includes activity's menu file

Mipmap including the boot icon

Values other XML files, including the definition of string and color, and so on.

2. Running the program

The program can be run on a specific Android device or on a simulator.

Before the specific Android device debugging, need to install the relevant driver of the device, compile the program to choose the version number to match, do not bother to run Hello World!

3. Create a simple user interface

The Android app's graphical interface has a hierarchical structure of viewgroup-View. ViewGroup is a non-visible container that defines how its sub-view is laid out, and the layout is a subclass of ViewGroup. The View object is a normal UI part.

1. Create a new linear layout

2. Add Text field and button

<android:id= "@+id/edit_message"    android:layout_width= "Wrap _content "    android:layout_height=" Wrap_content "    android:hint=" @ String/edit_message "/>

View's ID is used to read and manipulate the corresponding View object

The hint of the view is the prompt statement when the input text is empty

3. Adding a string resource

Add a string named Edit_message and Button_send in Res/values/strings.xml

4. Let the input box fill the screen

Add a response function to a UI part

< Button     Android:layout_width = "Wrap_content"     android:layout_height= "Wrap_content"    android:text= "@string/button_ Send "    android:onclick=" SendMessage "/>

Increase the onclick response, SendMessage as the activity's class function

In the Myactivity.javamyactivity class, add the corresponding class function, which needs to be public, the return value of void and only the view one parameter

Create a intent in the class function to open the new activity

 Public void sendMessage (view view) {  new Intent (This, displaymessageactivity.  Class);   = (EditText) Findviewbyid (r.id.edit_message);   = Edittext.gettext (). toString ();  Intent.putextra (Extra_message, MESSAGE);
StartActivity (intent);}

Intent can carry a variable extras of type dictionary, and by calling Putextra can assign a value to the dictionary

Supplementing definitions in myactivity Extra_message

 Public class extends appcompatactivity {    publicfinalstatic String extra_message = " Com.mycompany.myfirstapp.MESSAGE ";    ...}

4. Create another activity

Use as to create another blank Activity to edit its Java core code

Receive Intent

Intent Intent = Getintent ();

String message = Intent.getstringextra (myactivity.extra_message);

Create a new TextView in activity

TextView TextView = new TextView (this);

Textview.settextsize (40);

Textview.settext (message);

Add TextView to the layout in activity

Relativelayout layout = (relativelayout) Findviewbyid (r.id.content);

Layout.addview (TextView);

Congratulations, complete the basic tutorial!

Androidstudio study notes-first Android app

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.