20169211 2016-2017-2 《移动平台开发实践》 第十周实验总结

实验一:简易计算器

实验要求

实现一个简易计算器Calc,支持+ - * / 和%运算, 从命令行传入计算数据,比如:

java Calc 2 + 3     结果为 2 + 3 = 5
java Calc 8 - 3     结果为 8 - 3 = 5
java Calc 2 * 3     结果为2 * 3 = 6
java Calc 10 / 2     结果为10 / 2 = 5
java Calc 10 % 3     结果为10 % 3 = 1

实现代码

代码连接

实验截图

实验二:mini dc

实验要求

实现Linux下dc的功能,计算后缀表达式的值。

实现代码

代码连接

实验截图

实验三:第一个Android Studio程序

修改res目录下的activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.study.helloandroid.MainActivity">

<TextView
    android:layout_width="131dp"
    android:layout_height="64dp"
    android:layout_marginBottom="353dp"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.549"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="20169211"
    tools:layout_editor_absoluteX="139dp"
    tools:layout_editor_absoluteY="212dp" />

</android.support.constraint.ConstraintLayout>

运行效果

实现四:修改代码让MainActivity启动ThirdActivity

知识点说明

Android应用程序的主活动,是当用户从主屏幕选择App图标的时候,通过系统自身而启动的。在拥有多个活动的应用程序中,有可能会启动另一个活动。实际上,从一个活动启动另一个活动,可以通过调用startActivity方法直接做到。,如下所示:

startActivity(intent);

其中intent是android.content.Intent类的一个实例。

实验操作

项目创建两个活动,MainActivity和ThirdActivity。MainActivity包含了一个按钮,单击该按钮的时候,就会启动ThirdActivity。

实验代码

package com.example.secondactivitydemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class MainActivity extends Activity implements
    OnTouchListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv = (TextView) findViewById(R.id.textView1);
        tv.setOnTouchListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onTouch(View arg0, MotionEvent event) {
        Intent intent = new Intent(this, ThirdActivity.class);
        intent.putExtra("message", "20169211");
        startActivity(intent);
        return true;
    }
}

实验截图

实验五:修改代码让Toast消息中显示自己的学号信息

知识点说明

Toast是一个小的弹出对话框,用于显示一条消息作为给用户的反馈。Toast并不会代替当前的活动,并且只是占据了一条消息那么大的空间。android.widget.Toast类是创建Toast的模板。要创建一个Toast,调用它唯一的构造方法,接收Context作为一个参数:

public Toast(android.content.Context context) ;

在默认情况下,一个Toast会显示在靠近激活活动的底部。但是,也可以在调用其show方法之前调用其setGravity方法从而修改其显示的位置。

实验截图

实验六:修改布局

知识点说明

界面的布局有以下几种形式,传统的说是有五大布局,分别为:

线性布局  Linearlayout
相对布局   RelativeLayout
表格布局 TableLayout(现在已经过时了)
绝对布局    AbsolutelyLayout
帧布局           FrameLayout

常用的布局类型主要是: 线性布局和网格布局(GrideLayout)。

实验代码

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
     <!-- 手机号码标签 -->
     <TextView
        android:id="@+id/tv_phone"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/tv_phone" />
     <!-- 手机号码编辑框 -->
     <EditText
        android:id="@+id/et_phone"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_phone"
        android:hint="@string/tip_phone"
        android:phoneNumber="true" />
     <!-- 信息内容标签 -->
     <TextView
        android:id="@+id/tv_content"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/et_phone"
        android:text="@string/tv_content" />
     <!-- 信息内容编辑框 -->
     <EditText
        android:id="@+id/et_content"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_content"
        android:hint="@string/tip_content"
        android:minLines="3" />
     <!-- 发送按钮 -->
     <Button
        android:id="@+id/btn_Send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_content"
        android:text="@string/btn_send" />
     <!-- 取消按钮 -->
     <Button
        android:id="@+id/btn_Cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/btn_Send"
        android:layout_toRightOf="@id/btn_Send"
        android:text="@string/btn_cancel" />
</RelativeLayout>

实验截图

参考资料

- 《Android Programming: The Big Nerd Ranch Guide(Android 编程:权威指南)》;

- 《Head First Android Development(深入浅出Android 开发)》;

- 《Android 开发艺术探索》;

- 《Android 设计模式源码分析》;

- 《Android 开发精要》;

- 《App 研发录》。
posted @ 2017-05-07 17:53  20169211_李超  阅读(297)  评论(1编辑  收藏  举报