第二个项目——计数器

实验目的

  • 掌握Android Studio集成开发环境的基本用法;

  • 了解Android工程基本结构;

  • 了解Activity的创建及布局资源的基本使用;

  • 了解Activity生命周期及其不同状态;

实验要求

  • 了解Layout Editor的基本使用;

  • 了解Layout文件的基本结构;

  • 掌握TextEditButton控件的基本用法;

  • 掌握Button控件单击事件处理方式;

  • 掌握Toast的基本用法;

实验内容

步骤一,创建工程

启动Android Studio创建名为Code02的工程,选择Empty Activity模板。

步骤二,修改activity_main.xml布局

2.1 打开activity_main.xml文件,将布局文件顶层元素由ConstraintLayout改为LinearLayout

并在LinearLayout开标签中,设置android:orientation属性。修改完成后的activity_main.xml文件代码如下所示。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp"
    android:orientation="vertical"
    ...>

</LinearLayout>

2.2activity_main.xml文件的TextView控件标签上方及下方加入两个Button控件标签,并设置Button相应属性,代码如所示。

...
    <Button
        android:id="@+id/btnShowToast"
        android:text="Show Toast"
        android:textColor="@android:color/white"
        android:background="@color/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        ...
        />

    <Button
        android:id="@+id/btnCount"
        android:text="Count"
        android:textColor="@android:color/white"
        android:background="@color/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
...

这其中Button控件标签的属性包括:

  • android:id,通用属性,作为控件的唯一标识。

  • android:text,通用属性,控件的文本,在此处为按钮的文本。

  • android:textColor,通用属性,控件的文本颜色,在此处为Button的文本颜色。

  • android:background,通用属性,控件的背景色。

  • android:layout_width,通用属性,控件的宽,可取值包括match_parentwrap_content或指定的大小(以dp为单位,例如:48dp)。

  • android:layout_height ,通用属性,同上。

2.3 设置TextView控件标签属性。如果此时通过Layout Editor(布局编辑器)查看activity_main.xml,则如图1. 通过Layout Editor预览布局所示。

修改TextView控件属性,使得TextView控件占满布局剩余的空间。主要在TextView控件标签中

加入android:layout_weight,将该属性值设为1,表示该控件将占满父容器(ViewGroup,在本例中是LinearLayout)的剩余空间。

设置TextView控件的文本对其方式、字体大小、字体颜色、背景等属性。控件文本对其方式由android:gravity属性进行控制,该属性设置控件显示内容相对于控件内部的位置,默认为在控件内空间的左侧。TextView控件属性如下代码所示。

...
    <Button
        ... />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="0"
        android:textSize="160sp"
        android:textColor="@color/colorAccent"
        />
...

步骤三,编译、部署APK

编译、部署APK,在AVD虚拟机中查看APK运行结果,如图通过AVD虚拟机运行Code02所示。

步骤四,显示Toast消息

3.1 对【SHOW TOAST】 Button控件新增onClick(View v)事件监听器。并在该方法中调用ToastmakeToast方法显示快显消息。

3.2 打开MainActivity.java源文件,在onCreate(Bundle savedInstanceState)方法中加入如下代码:

public class MainActivity extends AppCompatActivity {
    ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnShowToast = findViewById(R.id.btnShowToast);}
        btnShowToast.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "Hello World!",
                     Toast.LENGTH_SHORT).show();
            }
        });
    }
    ...
}

步骤五,实现计数功能

每点击一次【Count】 Button 控件,TextView 显示的计数值加 1 。 要实现该效果,主要思路为:

  • 使用 int count 记录计数次数;

  • 设置【Count】 Button 控件的 onClick 事件监听器,在其中更新 count 值;

  • onClick 事件监听器中设置 countTextView 控件的文本值;

public class MainActivity extends AppCompatActivity {
    private int count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       ...

        final TextView tvCount = findViewById(R.id.tvCount);

        Button btnCount = findViewById(R.id.btnCount);
        btnCount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                {tvCount.setText(Integer.toString(++count));
            }
        });
    }
}

编译、部署APK,App运行效果如图2. Code02最终运行效果所示。

实验小结

通过本次实验,你应该掌握了如下知识内容:

  • 使用LinearLayout(线性布局)进行布局管理;

  • 使用Toast显示快显消息;

  • 使用Button控件的onClick事件监听器响应按钮单击事件;

Last updated

Was this helpful?