zhiyinjixu

  博客园  :: 首页  ::  :: 联系 ::  :: 管理
“Hello, World”程序指的是只在计算机屏幕上输出“Hello, World!”(意为“世界,你好!”)这行字符串的计算机程序。一般来说,这是每一种计算机编程语言中最基本、最简单的程序,亦通常是初学者所编写的第一个程序。它还可以用来确定该语言的编译器、程序开发环境,以及运行环境是否已经安装妥当。
 

工具/原料

  • 个人电脑一台,已经配置好安装开发环境
     
 

步骤/方法

    1. 新建工程
      选择Android project
      将会出现如下信息Priject name:工程名字
      Contents:单选框一个工程还是导入现有工程
      Build Target:选择使用那一个JDK 
      Application name:应用程序名字
      Package name: 创建包
      Create Activity:创建一个Activity 如果你是J2EE程序员这个就相当于struts的Action类
      Min SDK Version: JDK版本 
      创建完毕:目录介绍:
      Src: java源文件即我们写的java后缀名的文件代码 在里面有我们之前所填写创建的一个MainActivity.java文件
      Gen:并没有创建gen这个目录 但是为什么出现此目录呢?没错这个是Android给我们自动生成的一个目录 并且还在次目录下创建了一个R文件(此R文件后面会讲到)
      Android 1.5:如果你是java程序员 就应该很熟悉 这个就书库文件 即 Android的核心文件
      Assets: 没有用到过
      Res.:放置资源文件的目录
      Res.drawable:一般用来存储相关应用的图片以及mp3播放文件等
      Res.layout:用来存储布局信息 如果你是j2ee程序员那么此目录下的文件相当于jsp文件即html文件,只是Android是以xml方式进行布局的
      Res.values:存储的相关的样式文件(CSS)以及经常用到的字符串信息的声明,但是也是以xml进行封装的
      AndroidManifest.xml:工程描述文件,相当于j2ee的web.xml文件 ,它可以设置第一启动的Activity文件(即j2ee的Action类)
       
    2. 程序编写
      AndroidManifest.xml(工程描述文件)
      <?xml version="1.0" encoding="utf-8"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.zxkj.luowei"
      android:versionCode="1"
      android:versionName="1.0">
      <application android:icon="@drawable/icon" android:label="@string/app_name">
      <activity android:name=".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>
      <uses-sdk android:minSdkVersion="3" />
      </manifest> 
      我们主要关注的就是application里面的配置信息:
      android:icon:
      指此应用程序的图片 在模拟器里面可以看到 : 点击家的按钮接着拉开抽屉可以看到:此图片就在:
      Res.drawable(般用来存储相关应用的图片以及mp3播放文件等)
      目录下
      @drawable/icon
      @代表在当前应用找
      android:label
      android:label="@string/app_name"
      即在My_one_And显示的名字
      这里会在我们打开values/Strings.xml文件
      <?xml version="1.0" encoding="utf-8"?>
      <resources>
      <string name="hello">Hello World, MainActivity!</string>
      <string name="app_name">My_One_Android_Project</string>
      </resources>
      会发现一个name为app_name的String声明并且其值于我们之前模拟器所显示的标题一样
      <activity android:name=".MainActivity">
      声名一个Activity类 此类在 .MainActivity下其中点代表com.zxkj.luowei
      即之前配置的package="com.zxkj.luowei"
      <activity android:label="@string/app_name">
      代表My_One_Android_Project
      <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
      <intent-filter>代表一个Action能做些什么事情 这里代表此Activity是第一启动项
      <action android:name="android.intent.action.MAIN" />
      一般情况下此name是可以任意改动的 但是除此之外 因为sdk后台会根据这个名字来调如果你改动则找不到了
      <category android:name="android.intent.category.LAUNCHER" />
      标志为第一启动项
      接下来进入
      MainActivity.java类
      package com.zxkj.luowei;
      import android.app.Activity;
      import android.os.Bundle;
      public class MainActivity extends Activity {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      }
      }
      会发现我们之前填写的MainActivity 类 继承自 Activity类
      并且重写了此类的onCreate(Bundle savedInstanceState)方法
      此方法会在实例化此类的时候一并调用(建议了解下Activity的生命周期)
      import android.os.Bundle 用于映射字符串的值 可以在Android之间进行通讯
      super.onCreate(savedInstanceState); 代表调用父类的方法并且将savedInstanceState传给父类
      setContentView(R.layout.main);
      现在打开R文件
      package com.zxkj.luowei;
      public final class R {
      public static final class attr {
      }
      public static final class drawable {
      public static final int icon=0x7f020000;
      }
      public static final class layout {
      public static final int main=0x7f030000;
      }
      public static final class string {
      public static final int app_name=0x7f040001;
      public static final int hello=0x7f040000;
      }
      }
      此目录保存了res目录下的所有资源 并且给它们一个标识码 好让程序直接访问

      public static final class string {
      public static final int app_name=0x7f040001;
      public static final int hello=0x7f040000;
      }
      代表Strings.xml文件下生命的String变量
      此类是不可以被修改的并且当你更新
      res此目录下的文件也同时被更新
      如你向drawable丢进去一个文件
      会发现:
      public static final class drawable {
      public static final int icon=0x7f020000;
      public static final int qkss=0x7f020001;
      }
      多出一个qkss
      然而
      setContentView(R.layout.main);
      则代表
      public static final class layout {
      public static final int main=0x7f030000;
      }<?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      >
      <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/hello"
      />
      </LinearLayout>
      LinearLayout:这是一个布局信息 标志它所包含的View都是线性布局是
      android:orientation:
      android:orientation="vertical" 
      可以改成:
      android:orientation="horizontal"
      vertical此属性代表View是以垂直进行排序
      horizontalvertical此属性代表View是以横向进行排序
      android:layout_width:
      android:layout_width="fill_parent"
      可以改成:
      android:layout_width="wrap_content"
      fill_parent横向全部填充
      wrap_content横向顺其改变(如图片是多大就显示多大)
      当然我们还可以为它设置大小如:
      android:layout_width="61px"
      android:layout_height与android:layout_width类似
      android:text:
      android:text="@string/hello"
      这里text代表是显示什么内容
      @string/hello 代表在values/Strings.xml文件里面读取
      我们打开values/Strings.xml文件
      <?xml version="1.0" encoding="utf-8"?>
      <resources>
      <string name="hello">Hello World, MainActivity!</string>
      <string name="app_name">My_One_Android_Project</string>
      </resources>
      会发现一个name为hello 的String声明并且其值于我们之前模拟器所显示的内容一样
      可能还有些人对于
      LinearLayout 
      布局中的
      android:orientation="vertical" 
      android:orientation="horizontal"
      这2者不是很了解 好的现在我做一个列子:
      将main.xml进行修改部分 
      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      >
      <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/hello"
      />
      <Button
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="此为线性布局的垂直布局"
      />
      <Button
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="此为线性布局的垂直布局"
      />
      <Button
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="此为线性布局的垂直布局"
      />
      </LinearLayout>
      这里我多添加了几个Button 即按钮 运行起来:可以看倒这就是线性垂直的效果
      再将
      android:orientation 改成:
      android:orientation="horizontal"为什么只看见一个Button呢 按下 Ctrl 不放接着按下F12就知道原因了
      现在我们将
      <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/hello"
      />改成
      <TextView 
      android:id="@+id/text_Id"
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/hello"
      />
      意思就是为TextView声明一个标识符(Id)
      并且名字是text_Id
      查看R文件:
      public static final class id {
      public static final int text_Id=0x7f050000;
      }
      会发现多了一个text_Id并且还分配给它一个识别码 
      此识别码直接指向TextView 
      现在我们修改MainActivity 的OnCreate方法
      package com.zxkj.luowei;
      import android.app.Activity;
      import android.os.Bundle;
      import android.widget.TextView;
      public class MainActivity extends Activity {
      /** Called when the activity is first created. */
      private TextView one_Text;
      @Override
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      one_Text=(TextView)findViewById(R.id.text_Id);
      one_Text.setText("Hello Word");
      }
      }
      private TextView one_Text;
      声明一个TextView起名叫one_Text
      one_Text=(TextView)findViewById(R.id.text_Id);
      找到R.id.text_Id标识码的View
      在Android里面所有的视图都继承自View这个类
      因此在这里我们需要强制转换
      one_Text.setText("Hello Word");
      为one_Text设置显示内容为Hello Word
       
    3. 程序运行
      右键设置Build Path,选中SDK版本
      将鼠标移动到工程名右击:
      选择……此时应用程序将运行起来并弹出dos界面 即 模拟器在此信息栏可以看到模拟器的运行过程:可能由于等待过久 模拟器将处于省电状态 这个时候我们点击MENU可以使它运行我们的程序:
      这个时候将出现如下字:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
posted on 2011-12-13 20:50  zhiyinjixu  阅读(276)  评论(0)    收藏  举报