建立我的第一个程序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/WAN_EXE/article/details/83142634

教你建立一个简单的程序,你会创建一个用户界面,接收用户的输入,然后显示在第二个界面上。

源码在appfundamentals 目录里面。

1.概述

在开始写代码之前,你应该有下面的基础认识,程序是由多个部件组合而成的,activities是负责提供用户界面的部件,其它的部件还有broadcast receivers, services, 允许你在后台运行任务。

程序需要适应不同的设备,你需要为不同的屏幕尺寸创建不同的布局,系统决定使用哪个布局。如果你的程序需要特殊的硬件功能,比如相机功能,你可以在程序运行的时候动态的查询设备是否有此项功能。

2.建立一个工程

下载最新的android studio编辑器,创建一个名为MyFirstApp的程序,IDE会自动地生成下面重要的文件:

app > java > com.android.guide.appfundamentals > MyFirstApp

程序的入口的部件,当你点击屏幕的时候,系统会启动这个文件。

app > res > layout > my_first_app.xml

入口的布局文件。

app > manifests > AndroidManifest.xml

定义程序的基本特性,以及定义每个部件。

build.gradle

你会看到两个相同文件名的文件,一个是对应程序的,一个是对应"app"模块的。

3.运行你的程序

程序可以在手机和模拟器上运行,手机上运行需要打开调试开关。

4.建立一个简单的用户界面

使用IDE的布局编辑器编辑一个含有文本输入和按钮的用户界面,

布局文件

用户界面使用的容器和组件的形式进行布局,以下是原文:

The user interface for an Android app is built using a hierarchy of layouts (ViewGroup objects) and widgets (View objects). Layouts are containers that control how their child views are positioned on the screen. Widgets are UI components such as buttons and text boxes.

UI布局

刚接触这些名词可能会感觉生涩,布局编辑器支持拖动,通过拖动组件和设置相关的属性就可以建立一个漂亮的UI界面,也可以使用代码进行布局。

<?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">

    <EditText
        android:id="@+id/editText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:ems="10"
        android:hint="@string/edit_message"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toStartOf="@+id/button"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:onClick="sendMessage"
        android:text="@string/button_send"
        app:layout_constraintBaseline_toBaselineOf="@+id/editText"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/editText" />
</android.support.constraint.ConstraintLayout>

以上是布局文件,可以运行程序查看效果。

5.启动另外一个activity部件

可以设置按钮的属性onClick,并在activity里面实现相关功能,当进行点击的时候就会执行。通常使用Intent和程序里面的其它部件建立联系,intent表示程序想要做什么的意图。

/** Called when the user taps the Send button */
    public void sendMessage(View view) {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.editText);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }

创建第二个DisplayMessageActivity文件,接收其它部件传递过来的信息。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);
    
    // Get the Intent that started this activity and extract the string
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    // Capture the layout's TextView and set the string as its text
    TextView textView = findViewById(R.id.textView);
    textView.setText(message);
}

运行程序,在文本框中输入文字,然后点击按钮,就会在第二个界面显示。

猜你喜欢

转载自blog.csdn.net/WAN_EXE/article/details/83142634