Android实训报告

Android实训报告

目录

一、实训背景及目的要求......................................................................................................................... 3

1.1背景简介...................................................................................................................................... 3

1.2实训目的及要求........................................................................................................................... 3

二、设计思路........................................................................................................................................... 3

2.1设计题目...................................................................................................................................... 3

2.2功能分析...................................................................................................................................... 4

2.3模块划分...................................................................................................................................... 4

三、设计实现及代码分析......................................................................................................................... 4

3.1第一个Activity.............................................................................................................................. 4

3.1.1布局文件:main.xml:........................................................................................................ 4

3.1.2源代码文件:MainActivity.java............................................................................................. 7

3.2第二个Activity.............................................................................................................................. 8

3.2.1布局文件:result.xml.......................................................................................................... 8

3.2.2源代码文件:ResultActivity.java........................................................................................... 9

3.3字符串资源文件:strings.xml...................................................................................................... 12

3.4程序清单文件:AndroidManifest.xml............................................................................................ 13

四、程序功能测试及截图........................................................................................................................ 14

4.1工程项目目录............................................................................................................................. 14

4.2程序运行界面:......................................................................................................................... 14

4.3未填身高提示:......................................................................................................................... 15

4.4计算示例:................................................................................................................................ 15

五、课程设计(实训)总结.................................................................................................................... 16

一、实训背景及目的要求

1.1背景简介

Android(读音:['ændr?id])是一种以Linux为基础的开放源码操作系统,主要使用于便携设备,目前尚未有统一中文名称,中国大陆地区较多人使用安卓或安致。Android操作系统最初由Andy Rubin创办[5],最初只支持手机。20##年由Google收购注资,并拉拢多家制造商组成开放手机联盟(Open Handset Alliance)开发改良,逐渐扩展到到平板电脑及其他领域上[6]。 20##年末数据显示,仅正式推出两年的操作系统的Android已经超越称霸十年的诺基亚Symbian系统,跃居全球最受欢迎的智慧手机平台。采用Android系统手机厂商包括HTC、Samsung、Motorola、Lenovo、LG、Sony Ericsson等。

1.2实训目的及要求

Android以Linux为核心的Android行动平台,使用Java作为编程语言。本实训是在学习java语言程序设计的基础上进行的一次综合实践。通过综合训练,要求学生掌握java语言程序设计的基本技能和Android编程的应用,并较系统地掌握JAVA语言程序设计开发方法以及帮助文件的使用等,使学生通过本次实训,能够进行独立的Android应用程序开发,能够在实际操作中得到进一步的提高,为以后的学习和工作打下良好的基础。

目的:

1、培养学生运用所学课程Java语言程序设计的理论知识和技能,分析解决计算机实际应用中的问题的能力。

2、培养学生在Java语言程序设计的基础上,开发Android应用程序的思想和方法。

3、培养学生调查研究、查阅技术文献、资料、手册以及编写技术文献的能力。

通过课程设计,要求学生在指导教师的指导下,独立完成课程设计的全部内容,包括:

1、确定开发的程序,收集和调查有关技术资料。

2、按软件工程步骤进行程序设计。

3、对完成的程序进行测试和完善。

4、完成课程设计报告。

二、设计思路

2.1设计题目

以Android系统的UI界面开发为基础,设计一个可以简单计算标准体重的应用程序,要求以2个Acitivity实现,第一个Activity作为输入界面,第二个Activity作为结果输出界面,具体实现细节自行设计。

2.2功能分析

该设计题目要求实现可计算输出标准体重功能的应用程序。通过查阅资料可知,按照世界卫生组织推荐的计算标准体重的方法,需要获知的输入信息有性别、身高。故可在第一屏设置有单选框以确定性别,输入框以获取身高。另,为了增加程序的实用性,可再设一可选输入框,用来得到实际体重,与标准体重对比,给出用户一些健康提议。第二屏设置有结果输出显示区域与健康提示显示区域。

2.3模块划分

通过程序功能分析,可将程序划分为2个模块,即2个Activity:

第一个Activity:两个单选框(RadioButton)获取性别,一个输入框(EditText)获取身高,一个可选输入框(EditText)获取实际体重,一个按钮(Button)及一些提示文本。

第二个Activity:一个文本显示区(TextViw)显示计算结果,一个可选文本显示区(TextView)显示提示信息。

三、设计实现及代码分析

3.1第一个Activity

3.1.1布局文件:main.xml:

采用绝对布局,以实现控件精准显示;

RadioGroup包含两个RadioButton,以实现性别男女选择;

两个供输入的EditText限定了输入类型为numberDecimal,以确保只接收数字型数据。

main.xml完整代码如下:

<?xml version="1.0" encoding="utf-8"?>

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<TextView 

    android:id="@+id/tv1"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="@string/tv1_t"

    android:textSize="20sp"

    android:layout_x="50px"

    android:layout_y="25px"

    />

<TextView

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="@string/tv_sex"

    android:layout_x="50px"

    android:layout_y="100px"

    />

<TextView

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="@string/tv_tall"

    android:layout_x="50px"

    android:layout_y="150px"

    />

<RadioGroup

    android:id="@+id/rg"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:orientation="horizontal"

    android:layout_x="95px"

    android:layout_y="90px"

    >

        <RadioButton

            android:id="@+id/rb_male"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"          

            android:text="@string/male"   

            />

        <RadioButton

            android:id="@+id/rb_female"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="@string/female"   

            />

      </RadioGroup>

<EditText

    android:id="@+id/et_tall"

    android:layout_width="100px"

    android:layout_height="40px"

    android:layout_x="100px"

    android:layout_y="140px"

    android:inputType="numberDecimal"

    />

<TextView

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_x="200px"

    android:layout_y="145px"

    android:textSize="20sp"

    android:text="cm"

    />

<TextView

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_x="50px"

    android:layout_y="200px"

    android:text="@string/tv_real"

    />

<EditText

    android:id="@+id/et_real"

    android:layout_width="100px"

    android:layout_height="40px"

    android:layout_x="100px"

    android:layout_y="230px"

    android:inputType="numberDecimal"

    />

<TextView

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_x="200px"

    android:layout_y="235px"

    android:textSize="20sp"

    android:text="kg"

    />

<Button

    android:id="@+id/compute"  

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_x="100px"

    android:layout_y="280px"

    android:textSize="20sp"

    android:text="@string/compute"

    /> 

</AbsoluteLayout>

3.1.2源代码文件:MainActivity.java

使用main.xml布局文件,为计算按钮(Button)注册事件监听,添加事件响应代码;

实现未填身高提示:

Toast.makeText(MainActivity.this, R.string.tall_hint, Toast.LENGTH_SHORT).show();

实现Activity之间跳转设置:

intent.setClass(MainActivity.this, ResultActivity.class);

实现Activity之间数据封装传输:

bundle.putDouble("height", height);

bundle.putString("sex", sex);

bundle.putString("real", real);

intent.putExtras(bundle);

MainActivity.java完整代码如下:

package com.halcyon;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.RadioButton;

import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

   

    private RadioButton rb1 = null;

    private RadioButton rb2 = null;

    private EditText et = null;

    private EditText et_real = null;

    private Button bt = null;

   

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

       

        rb1 = (RadioButton)findViewById(R.id.rb_male);

        rb2 = (RadioButton)findViewById(R.id.rb_female);

        rb1.setChecked(true);             //默认为男性选中

       

        et = (EditText)findViewById(R.id.et_tall);

        et_real = (EditText)findViewById(R.id.et_real);

        bt = (Button)findViewById(R.id.compute);

       

        bt.setOnClickListener(this);     //注册点击事件监听

    }

    @Override

    public void onClick(View v) {

        // TODO Auto-generated method stub

       

        /**事件响应代码,获取性别、身高,判断是否输入了实际体重

         * 新建Intent对象用于Activity之间跳转,及传输输入的数据

         * **/

        if(et.getText().toString().length()==0){      //身高未填提示

            Toast.makeText(MainActivity.this, R.string.tall_hint, Toast.LENGTH_SHORT).show();

        }

        else {

                double height = Double.parseDouble(et.getText().toString());

                String real = et_real.getText().toString();

                String sex = "";

                if(rb1.isChecked()){

                    sex = "M";

                }

                else{

                    sex = "F";

                }

                Intent intent = new Intent();

                intent.setClass(MainActivity.this, ResultActivity.class);

               

                Bundle bundle = new Bundle();       //存放数据,以封装传输

                bundle.putDouble("height", height);

                bundle.putString("sex", sex);

                bundle.putString("real", real);

                intent.putExtras(bundle);

               

                this.startActivity(intent);

        }

    }

}

3.2第二个Activity

3.2.1布局文件:result.xml

绝对布局,三个TextView,内容用紫色显示,提示标号用绿色显示,以达到较醒目的感觉。

result.xml完整代码如下:

<?xml version="1.0" encoding="utf-8"?>

<AbsoluteLayout

  xmlns:android="http://schemas.android.com/apk/res/android"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  android:layout_margin="10px"

<TextView

    android:id="@+id/tv_1"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_x="20px"

    android:layout_y="100px"

    android:textSize="20sp"

    android:textColor="#8855ff"

    />

<TextView

    android:id="@+id/tv_2"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_x="20px"

    android:layout_y="200px"

    android:textSize="20sp"

    android:textColor="#00ff00"

    />

<TextView

    android:id="@+id/tv_3"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_x="20px"

    android:layout_y="240px"

    android:textSize="20sp"

    android:textColor="#8855ff"

    />

</AbsoluteLayout>

3.2.2源代码文件:ResultActivity.java

使用result.xml布局文件,获取intent,取得传入的数据信息,以

男性:(身高cm-80)×70﹪=标准体重

女性:(身高cm-70)×60﹪=标准体重

的标准来计算标准体重,方法

private String getWeight(double height, String sex)

实现此功能;

可根据用户是否输入了实际体重,来选择是否显示健康提示信息,方法

private void healthHint(double weight)

实现此功能;

方法private String form(double d)实现数据格式化。

ResultActivity.java完整代码如下:

package com.halcyon;

import java.text.NumberFormat;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.widget.TextView;

public class ResultActivity extends Activity{

   

     private TextView tv_1 = null;

     private TextView tv_2 = null;

     private TextView tv_3= null;

     private Intent intent = null;

     private Bundle bundle = null;

     private double height = 0;

     private String sex = "";    

   

     public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.result);

           

            tv_1 =  (TextView)findViewById(R.id.tv_1);

            tv_2 = (TextView)findViewById(R.id.tv_2);

            tv_3 = (TextView)findViewById(R.id.tv_3);

           

            intent = this.getIntent();

            bundle = intent.getExtras();

            height = bundle.getDouble("height");

            sex = bundle.getString("sex");

            

            tv_1.setText(this.getWeight(height,sex));

           

     }

     /**男性:(身高cm-80)¡Á70﹪=标准体重

      * 女性:(身高cm-70)¡Á60﹪=标准体重

      * **/

    private String getWeight(double height, String sex) {

        double weight = 0;

        String result = "";

        if(sex.equals("M")){

            weight = (height-80)*0.7;

            result = "这位先生,\n你的标准体重为"+form(weight)+"公斤。\n";          

        }

        else {

            weight = (height-70)*0.6;

            result = "这位女士,\n妳的标准体重为"+form(weight)+"公斤。\n";

        }

        healthHint(weight);

        return result;

    }

    private void healthHint(double weight) {

       

        /*根据用户是否输入了实际体重来判断是否添加显示提示信息

         * 如果输入了实际体重,按以下原则给用户以健康提示

         * 标准体重正负10﹪为正常体重

         * 标准体重正负10﹪~ 20﹪为体重过重或过轻

         * 标准体重正负20﹪以上为肥胖或体重不足

         * */

        String result = "";

        String real = bundle.getString("real");

        if(real.equals("")){

            result = "";

        }

        else {

            double r = Double.parseDouble(real);

            double rate = (r-weight)/weight;

            if(rate>0.2){                         //过胖

                result = this.getString(R.string.very_fat);

            }

            else if(rate>=0.1&&rate<=0.2){      //胖

                result = this.getString(R.string.fat);

            }

            else if(rate>-0.1&&rate<0.1){       //正常

                result = this.getString(R.string.normal);

            }

            else if(rate>=-0.2&&rate<=-0.1){   //瘦

                result = this.getString(R.string.thin);

            }

            else if(rate<-0.2){                  //过瘦

                result = this.getString(R.string.very_thin);

            }

            tv_2.setText(getString(R.string.hint));

        }

        tv_3.setText(result);

    }

   

    private String form(double d){      //格式化数据输出

        String str = NumberFormat.getInstance().format(d);

        return str;

    }

}

3.3字符串资源文件:strings.xml

在一个Android工程中,我们可能会使用到大量的字符串作为提示信息。这些字符串都可以作为字符串资源声明在配置文件中,从而实现程序的可配置性。

在代码中我们使用Context.getString()方法,通过传递资源ID参数来得到该字符串,也可以在其他资源文件中引用字符串资源,引用格式为:"@string/字符串资源名称。

本程序所引用的strings.xml内容为:

<?xml version="1.0" encoding="utf-8"?>

<resources>   

    <string name="app_name">NomalWeight</string>

    <string name="result_name">计算结果</string>

   

    <string name="tv1_t">计算你/妳的标准体重!</string>

   

    <string name="tv_sex">性别:</string>

    <string name="tv_tall">身高:</string>

    <string name="tv_real">当前体重(可选):</string>

   

    <string name="male">男的</string>

    <string name="female">女的</string>

   

    <string name="compute">计算</string>

    <string name="tall_hint">你没填身高哦!</string>

   

    <string name="hint">温馨提示:</string>

    <string name="normal">体重适中,要保持哟!</string>

    <string name="thin">"有点偏瘦哦,应该再多吃些东西啊!"</string>

    <string name="very_thin">"一定是受谁的虐待了,要好好补补身体的,身体要强壮才行滴!"</string>

    <string name="fat">"有点胖哦,要多锻炼啊!"</string>

    <string name="very_fat">"不能太胖啊,要为自己制作运动减肥表了!"</string>

</resources>

3.4程序清单文件:AndroidManifest.xml

每一个Android项目都包含一个清单(Manifest)文件--AndroidManifest.xml,它存储在项目层次中的最底层。清单可以定义应用程序及其组件的结构和元数据。

它包含了组成应用程序的每一个组件(活动、服务、内容提供器和广播接收器)的节点,并使用Intent过滤器和权限来确定这些组件之间以及这些组件和其他应用程序是如何交互的。

因本程序使用了2个Activity,故对AndroidManifest.xml有一定的修改,内容如下:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

      package="com.halcyon"

      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>

       

        activity android:name=".ResultActivity"

                  android:label="@string/result_name">

        </activity

    </application>

</manifest>

四、程序功能测试及截图

4.1工程项目目录

 

4.2程序运行界面:

4.3未填身高提示:

4.4计算示例:

 

五、课程设计(实训)总结

通过本次实训:

1)练习、巩固了所学Java理论知识,提示了编程技巧;

2)体验到利用网络查找文档、解决问题的便利;

3)有幸了解到Android开发平台已及Android程序设计思想与方法;

4)开拓了视野,了解到计算机应用的广泛性,为用计算机解决实际问题打下了基础。

相关推荐