Android Development Note 1 HelloWorld

Source: Internet
Author: User

A: New project

File-new-android Application Project

Figure: Helloandroid's Project

src folder

SRC: Store the project source code, in the SRC folder, the system automatically created for US Mainactivity.java

Package Com.example.helloandroid;import Android.os.bundle;import android.app.activity;import android.view.Menu ; Import Android.widget.TextView; Public classMainactivity extends Activity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); TextView T=NewTextView ( This); T.settext ("Hello world!");    Setcontentview (t); } @Override Publicboolean Oncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; }    }

Two classes of android.app.Activity and Android.os.bundle,import Android.view.Menu were imported; Import Android.widget.TextView; is added under TextView, where the two methods of overriding the parent class

@Override

When overriding the oncreate of a parent class, adding a @override system in front of the method can help you check the correctness of the method. For example, public void onCreate (bundle savedinstancestate) {...} This is the correct wording if you write the public void OnCreate (bundle savedinstancestate) {...} so the compiler returns the following error--the method OnCreate (Bundle) of type HelloWorld must override or implement a Supertype method to ensure that you correctly rewrite Onc Reate method. (because oncreate should be oncreate)

If you do not add @override, the compiler will not detect errors, but will assume that you have defined a new method OnCreate.

Android.app.Activity class: For almost all activities (activities) is to interact with the user, you can use the method to setContentView(View) put your own UI inside, there are two ways that almost all activity subclasses are implemented:

    1. onCreate(Bundle):初始化你的活动(Activity),比如完成一些图形的绘制。最重要的是,在这个方法里你通常将用布局资源(layout resource)调用setContentView(int)方法定义你的UI,和用findViewById(int)在你的UI中检索你需要编程地交互的小部件(widgets)。setContentView指定由哪个文件指定布局(main.xml),可以将这个界面显示出来,然后我们进行相关操作,我们的操作会被包装成为一个意图,然后这个意图对应有相关的activity进行处理。
    2. onPause(): Deal with what you have to do when you leave your activity. Most importantly, all changes made by the user should be committed here (usually ContentProvider saving the data).
Gen Folder

There are two Java files under it. The R.java file defines a class--r,r class that contains many static classes, and the name of the static class corresponds to a name in Res, which is the R class that defines the index of all resources for the project

 PublicFinalclassR { Public StaticFinalclassattr {} Public StaticFinalclassDimen {/** Default screen margins, per the Android Design guidelines. Customize dimensions originally defined in res/values/dimens.xml (such as screens margins) for SW720DP devices (e.g .             "Tablets" in landscape. */         Public StaticFinalintactivity_horizontal_margin=0x7f040000;  Public StaticFinalintactivity_vertical_margin=0x7f040001; }     Public StaticFinalclassdrawable { Public StaticFinalintIc_launcher=0x7f020000; }     Public StaticFinalclassID { Public StaticFinalintaction_settings=0x7f080000; }     Public StaticFinalclassLayout { Public StaticFinalintactivity_main=0x7f030000; }     Public StaticFinalclassMenu { Public StaticFinalintmain=0x7f070000; }     Public StaticFinalclass string {         Public StaticFinalintaction_settings=0x7f050001;  Public StaticFinalintApp_name=0x7f050000;  Public StaticFinalintHello_world=0x7f050002; }     Public StaticFinalclassStyle {/** Base application theme, dependent on API level.                This theme are replaced by Appbasetheme from Res/values-vxx/styles.xml on newer devices. Theme customizations available in newer API levels can go in res/values-vxx/styles.xml, while customizations re                lated to Backward-compatibility can go. Base application theme for API 11+.     This theme completely replaces appbasetheme from Res/values/styles.xml on API 11+ devices.         API One-theme customizations can go here. Base application theme for API 14+.  This theme completely replaces appbasetheme from BOTH Res/values/styles.xml and Res/values-v11/styles.xml on     API 14+ devices.          API theme customizations can go to here. */         Public StaticFinalintAppbasetheme=0x7f060000; /** Application Theme.          All customizations, that is, specific to a particular api-level can go. */         Public StaticFinalintApptheme=0x7f060001; }}

Through R.java we can quickly find the resources we need, in addition Yi will also check the R.java list of resources are used, the unused resources will not be compiled Yi into the software, this can reduce the use of mobile phone space occupied.

Android Private Libraries Folder

The folder includes Android-support-v4.jar, a Java archive file that contains all of the Android SDK libraries (such as views, Controls) and APIs required to build the application. Bind your app to the Android SDK and Android Emulator with Android-support-v4.jar, which allows you to use all of your Android libraries and packages, and enable your application to be debugged in the right environment, For example, the class library used by the Mainactivity.java used above

Import Android.os.bundle;import android.app.activity;import android.view.menu;import Android.widget.TextView;

It's all from the top Android-support-v4.jar.

Assets folder

Contains files such as MP3, video classes that the application needs to use.

Res folder

The resource directory that contains the resource files in your project and will be compiled into the application. When you add a resource to this directory, it is automatically recorded by R.java. Create a new project, the Res directory will have three subdirectories: Drawabel, layout, values.

Androidmanifest.xml

The total configuration file for the project, documenting the various components used in the application. File lists the features that the application provides, in which you can specify the services that the application uses (such as telephony, Internet services, SMS, GPS services, and so on). In addition, when you add a new activity, you need to configure it in this file, only after the configuration, you can call this activity. Androidmanifest.xml will contain the following settings: Application permissions, activities, intent filters, and so on.

If you were born or learned like me, you would find that Androidmanifest.xml is similar to the Web. config file and can be interpreted in the same way as the Web. config file.

If you are not, you can understand--well known as XML is a data interchange format, Androidmanifest.xml is used to store some data, but this data when the configuration data about the Android project.

<?xml version="1.0"encoding="Utf-8"? ><manifest xmlns:android="http://schemas.android.com/apk/res/android" Package="com.example.helloandroid"Android:versioncode="1"Android:versionname="1.0"> <uses-SDK Android:minsdkversion="8"android:targetsdkversion=" -"/> <Application Android:allowbackup="true"Android:icon="@drawable/ic_launcher"Android:label="@string/app_name"Android:theme="@style/apptheme"> <Activity Android:name="com.example.helloandroid.MainActivity"Android:label="@string/app_name"> <intent-filter> <action android:name="Android.intent.action.MAIN"/> <category android:name="Android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application></manifest>
Default.properties

Document the environmental information required in the project, such as the Android version

Two: Configure simulator windows→android Virtual Device Manager

Once the virtual device is configured, you can compile and run our first HelloWorld program.

Run As→android appilcation

Reference documents
    • Http://www.cnblogs.com/skynet/archive/2010/04/13/1711479.html

Android Development Note 1 HelloWorld

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.