学习进度笔记

发布时间 2024-01-12 20:39:16作者: 那年晚风可期

今天我花了几个小时学习了关于安卓应用开发的基础知识,包括应用程序结构、Android Studio中的工具和界面设计。接下来,我开始创建我的第一个安卓应用程序。

首先,我创建了一个新项目并设置了应用程序的名称和最低SDK版本。然后,我添加了一个主要的活动(activity),以便在应用程序启动时显示该活动。

接下来,我设计了应用程序的用户界面。我创建了一个布局文件,并向其中添加了一些视图元素,例如文本框和按钮。我还使用了约束布局,以确保这些元素可以正确地排列和调整大小。

在完成界面设计之后,我编写了一些Java代码来实现应用程序的功能。我为按钮添加了一个单击事件监听器,以便当用户单击按钮时执行某些操作。在这种情况下,我将文本框的文本更改为“Hello, World!”。

最后,我使用模拟器运行了我的应用程序,并检查了它是否按预期工作。一切看起来都很好,所以我准备继续开发我的应用程序。

这是我的Java代码:

javaCopy Code
package com.example.myapp;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.textView);
        button = findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView.setText("Hello, World!");
            }
        });
    }
}

这是我的布局文件:

xmlCopy Code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello, World!"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.5" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click me!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintVertical_bias="0.5" />

</androidx.constraintlayout.widget.ConstraintLayout>