前言

  由于昨天刚搭建好Android使用环境,非常想体验下Android开发过程,今天下班便在网上找了一些资料,根据资料DEMO做一个小程序来熟悉其开发过程。首先推荐一个网站http://www.inandroid.cn/bbs/forum-15-1.html。里面的内容对于初学还是不错的。为了开发方便API文档是不可少的,但是目前网上还没有中文的,所以只能硬着头皮看SDK下的文档了。在SDK安装文件下的docs里面,如果没有可以通过SDK或eclipse从官网上安装一个,打开eclipse->window->Android sdk and AVD manager->Available Packages,然后展开右边的树目录,勾上docs文档下载即可。

  先看看一个简单的效果图吧

    

androidstuido log等级 android deam_android

  这是一个通过身高,体重计算健康状态的小程序。(模拟器操作就跟手机一模一)

正文

  1 项目结构图

  

androidstuido log等级 android deam_java_02

  2 详细说明(copy自)   

:存放java源代码。

:存放编译器自动生成的java代码,这个目录下的文件是系统自动维护的。

:存放在这个目录下的文件,无论是mp3还是图片,都会被打包到发布包中。

:资源文件目录,添加到这个目录下的文件都会在gen下的R.ava文件中显示出来;如果res下存放的资源在应用中没有使用到,那么在gen下的R.java的文件中就不会显示,   那么打包的时候就不会将这部分资源打包,减少了应用的发布文件大小。

:存放图片文件,注意图片名称必须是[a-z0-9_.]组成;drawable目录下不能分子级目录,只能通过命名方法来加以区分。

:存放与UI相关的布局文件,都是xml文件。

:直接复制到设备中的任意文件,不会被编译,可以放数据库文件。

:存放字符串,颜色,数组等常量数据。可以任意命名,但是通常命名:color.xml、strings.xml、array.xml、styles.xml、dimens.xml(定义尺寸值:dimension           value)

:任意的XML文件,在运行时可以通过调用Resources.getXML()读取。

3 代码

  界面UI代码是layout文件下的main.xml文件

  




androidstuido log等级 android deam_移动开发_03

androidstuido log等级 android deam_androidstuido log等级_04

代码


<? 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/height" >
</ TextView >
< EditText android:id ="@+id/height"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:numeric ="integer"
android:text ="" >
</ EditText >
< TextView
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:text ="@string/weight" >
</ TextView >
< EditText android:id ="@+id/weight"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:numeric ="integer"
android:text ="" >
</ EditText >
< Button android:id ="@+id/submit"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:text ="@string/bmi_btn" >
</ Button >
< TextView android:id ="@+id/result"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:text ="" >
</ TextView >
< TextView android:id ="@+id/suggest"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:text ="" >
</ TextView >
</ LinearLayout >


 

   上面用到了3个widget分别是 TextView,Button和EditText。Android的所有控件的属性都是通过android:形式来设置的,关于控件属性的详细介绍可以看API文档。代码中最外层的LinearLayout是一种线型布局,其中指定android:orientation="vertical"表示垂直布局。通过@[类型]/[识别符号]如@string/height通过R.java做中介可以读取values下的xml文档。

所有Res下的xml文档都会在R.java中自动生成相应的代码

R.java       
/* AUTO-GENERATED FILE. DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found. It
 * should not be modified by hand.
 */

package com.demo.android.bmi;

public final class R {
 public static final class attr {
 }
 public static final class drawable {
 public static final int icnotallow=0x7f020000;
 }
 public static final class id {
 public static final int height=0x7f050000;
 public static final int result=0x7f050003;
 public static final int submit=0x7f050002;
 public static final int suggest=0x7f050004;
 public static final int weight=0x7f050001;
 }
 public static final class layout {
 public static final int main=0x7f030000;
 }
 public static final class string {
 public static final int advice_average=0x7f040001;
 public static final int advice_heavy=0x7f040002;
 public static final int advice_light=0x7f040000;
 public static final int app_name=0x7f040004;
 public static final int bmi_btn=0x7f040007;
 public static final int height=0x7f040005;
 public static final int hello=0x7f040003;
 public static final int weight=0x7f040006;
 }
}  
 
     
strings.xml       
<?     xml versinotallow="1.0" encoding="utf-8"     ?>     
     <     resources     > 
    
     <     string      name 
    ="hello" 
    > 
    Hello World, Bmi! 
    </ 
    string 
    > 
    
     <     string      name 
    ="app_name" 
    > 
    BMI 
    </ 
    string 
    > 
    
     <     string      name 
    ="height" 
    > 
    身高(CM) 
    </ 
    string 
    > 
    
     <     string      name 
    ="weight" 
    > 
    体重(KG) 
    </ 
    string 
    > 
    
     <     string      name 
    ="bmi_btn" 
    > 
    计算MBI值 
    </ 
    string 
    > 
    
     </     resources     >

  在源代码中可以使用R.类型.识别标识来获取值。通过这种方式将界面main.xml和描述信息strings.xml有效的分离出来。到目前为止,一个如第一张图所示的界面已经出来了。关于逻辑实现部分等下次在补充(比较晚了困哦)。

PS:其实我觉得Android的UI界面标记语言跟Sliverlight很接近。大家有什么好的资料拿出来一起分享哦!睡了睡了每天熬夜对身体不好啊!

补充:

  结果图:

  

androidstuido log等级 android deam_移动开发_05

逻辑代码:

  

代码

package      com.demo.android.bmi;

     import      java.text.DecimalFormat;

     import      android.app.Activity;
     import      android.os.Bundle;
     import      android.view.View;
     import      android.view.View.OnClickListener;
     import      android.widget.Button;
     import      android.widget.EditText;
     import      android.widget.TextView;

     public           class      Bmi  
    extends 
     Activity {
     /**      Called when the activity is first created.      */     
 @Override
     public           void      onCreate(Bundle savedInstanceState) {
     super     .onCreate(savedInstanceState);
 setContentView(R.layout.main);

     //     通过findViewById方法获取控件     
      Button submitBtn      =      (Button)findViewById(R.id.submit);
     //     为按钮添加鼠标单击事件     
      submitBtn.setOnClickListener(calBMI);
 }

     /*     
 * 声明事件
     */     
     private      OnClickListener calBMI      =       
    new 
     OnClickListener() {

 @Override
     public           void      onClick(View v) {
 DecimalFormat nf      =           new      DecimalFormat( 
    " 
    0.00 
    " 
    );
 EditText fieldHeight      =      (EditText)findViewById(R.id.height);
 EditText fieldWeight      =      (EditText)findViewById(R.id.weight);
     //     通过getText()方法获取控件内容     
           double      height      = 
     Double.parseDouble(fieldHeight.getText().toString()) 
    / 
    100 
    ;
     double      weight      =      Double.parseDouble(fieldWeight.getText().toString());
     double      BMI      =      weight 
    / 
    (height 
    * 
    height);

 TextView result      =      (TextView)findViewById(R.id.result);
     //     通过setText()方法设置控件内容     
      result.setText(     "     Your BMI is      " 
      
    + 
     nf.format(BMI));

 TextView fieldSuggest      =      (TextView)findViewById(R.id.suggest);
     if     (BMI     >     25 
    ){
 fieldSuggest.setText(R.string.advice_heavy);
 }     else           if     (BMI 
    < 
    20 
    ){
 fieldSuggest.setText(R.string.advice_light);
 }     else     {
 fieldSuggest.setText(R.string.advice_average);
 }
 }
 };
}