Android开发笔记1之HelloWorld

一:新建项目

    File-New-Android Application Project

image

图:HelloAndroid的项目

src文件夹

src:存放项目的源码,在src文件夹中,系统为我们自动创建了MainActivity.java

package com.example.helloandroid;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView t=new TextView(this);
        t.setText("hello world!");
        setContentView(t);
    }


    @Override
    public boolean 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;
    }
    
}

其中导入了两个类android.app.Activity和android.os.Bundle,import android.view.Menu; import android.widget.TextView; 是在TextView下添加的,在其中重写了父类的两个方法

@Override

在重写父类的onCreate时,在方法前面加上@Override 系统可以帮你检查方法的正确性。例如,public void onCreate(Bundle savedInstanceState){…….}这种写法是正确的,如果你写成public void oncreate(Bundle savedInstanceState){…….}这样编译器回报如下错误——The method oncreate(Bundle) of type HelloWorld must override or implement a supertype method,以确保你正确重写onCreate方法。(因为oncreate应该为onCreate)

而如果你不加@Override,则编译器将不会检测出错误,而是会认为你新定义了一个方法oncreate。

 

android.app.Activity类:为几乎所有的活动(activities)都是与用户交互的,你可以用方法setContentView(View)将自己的UI放到里面,有两个方法是几乎所有的Activity子类都实现的:

  1. onCreate(Bundle):初始化你的活动(Activity),比如完成一些图形的绘制。最重要的是,在这个方法里你通常将用布局资源(layout resource)调用setContentView(int)方法定义你的UI,和用findViewById(int)在你的UI中检索你需要编程地交互的小部件(widgets)。setContentView指定由哪个文件指定布局(main.xml),可以将这个界面显示出来,然后我们进行相关操作,我们的操作会被包装成为一个意图,然后这个意图对应有相关的activity进行处理。
  2. onPause():处理当离开你的活动时要做的事情。最重要的是,用户做的所有改变应该在这里提交(通常ContentProvider保存数据)。

编写java代码的目录,遵循java的命名规范

gen文件夹

image

其下有两个java文件。R.java文件中定义了一个类——R,R类中包含很多静态类,且静态类的名字都与res中的一个名字对应,即R类定义该项目所有资源的索引

public final class R {
    public static final class attr {
    }
    public static final class dimen {
        /**  Default screen margins, per the Android Design guidelines. 

         Customize dimensions originally defined in res/values/dimens.xml (such as
         screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here.
    
         */
        public static final int activity_horizontal_margin=0x7f040000;
        public static final int activity_vertical_margin=0x7f040001;
    }
    public static final class drawable {
        public static final int ic_launcher=0x7f020000;
    }
    public static final class id {
        public static final int action_settings=0x7f080000;
    }
    public static final class layout {
        public static final int activity_main=0x7f030000;
    }
    public static final class menu {
        public static final int main=0x7f070000;
    }
    public static final class string {
        public static final int action_settings=0x7f050001;
        public static final int app_name=0x7f050000;
        public static final int hello_world=0x7f050002;
    }
    public static final class style {
        /** 
        Base application theme, dependent on API level. This theme is 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 related to
            backward-compatibility can go here.
        

        Base application theme for API 11+. This theme completely replaces
        AppBaseTheme from res/values/styles.xml on API 11+ devices.
    
 API 11 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 14 theme customizations can go here. 
         */
        public static final int AppBaseTheme=0x7f060000;
        /**  Application theme. 
 All customizations that are NOT specific to a particular API-level can go here. 
         */
        public static final int AppTheme=0x7f060001;
    }
}

通过R.java我们可以很快地查找我们需要的资源,另外编绎器也会检查R.java列表中的资源是否被使用到,没有被使用到的资源不会编绎进软件中,这样可以减少应用在手机占用的空间。

包含了android的资源文件的标识符,不需要程序员维护,自动添加

Android Private Libraries文件夹

该文件夹下包括android-support-v4.jar,一个Java 归档文件,其中包含构建应用程序所需的所有的Android SDK 库(如Views、Controls)和APIs。通过android-support-v4.jar,将自己的应用程序绑定到Android SDK和Android Emulator,这允许你使用所有Android的库和包,且使你的应用程序在适当的环境中调试,例如上文中使用的MainActivity.java使用的类库

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

都来自上面的android-support-v4.jar

assets文件夹

   包含应用系统需要使用到的诸如mp3、视频类的文件。

原生资源文件:保存应用的资源文件例如音频文件或者视频文件、或者不经常被用户修改的文件

res文件夹

   资源目录,包含你项目中的资源文件并将编译进应用程序。向此目录添加资源时,会被R.java自动记录。新建一个项目,res目录下会有三个子目录:drawabel、layout、values。

drawable-xxdpi、包含android应用中的图片资源文件,按照清晰度:高清、清晰度低、比较清晰、超高清、非常超高清
    layout:布局文件,完成UI控件的堆放
    menu:菜单
    android的应用菜单
    values:字符资源文件,通常使用在手机的国际化

AndroidManifest.xml

   项目的总配置文件,记录应用中所使用的各种组件。个文件列出了应用程序所提供的功能,在这个文件中,你可以指定应用程序使用到的服务(如电话服务、互联网服务、短信服务、GPS服务等等)。另外当你新添加一个Activity的时候,也需要在这个文件中进行相应配置,只有配置好后,才能调用此Activity。AndroidManifest.xml将包含如下设置:application permissions、Activities、intent filters等。

如果你跟我一样是ASP.NET出生或者学过,你会发现AndroidManifest.xml跟web.config文件很像,可以把它类同于web.config文件理解。

如果你不是,你可以这样理解——众所周知xml是一种数据交换格式,AndroidManifest.xml就是用来存储一些数据的,只不过这些数据时关于android项目的配置数据。

清单文件的结构:
1.包名
2.apk的版本
3.apk的图片,说明
4.相关的授权

<?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="18" />

    <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

    记录项目中所需要的环境信息,比如Android的版本等

二:配置模拟器

windows→Android Virtual Device Manager

image配置好虚拟器后就可以编译运行我们的的第一个HelloWorld程序了

run as →Android Appilcation

image

参考文献

posted @ 2014-12-27 15:26  草旅虫  阅读(210)  评论(0编辑  收藏  举报