入门:第一个App

2021-03-19  本文已影响0人  人间入画

这一节将学习LinearLayout、EditText、string资源、Button、weight属性的基本使用,以及使用intent启动Activity并传递值、在xml文件中定义oncliick事件并实现、不在xml而使用代码定义控件。

创建线性布局


建立新的project,建立完成后打开activity_main.xml。此 XML 文件定义 MainActivity 的布局。它包含默认的“Hello World”文本视图,删除所有内容并插入以下 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:orientation="horizontal">
</LinearLayout>

LinearLayout 是一个视图组(ViewGroup 的子类),它会按照 android:orientation 属性的指定,将子视图设置为垂直或水平方向布局。LinearLayout 的每个子视图都会按照它们各自在 XML 中的出现顺序显示在屏幕上。

其他两个属性 android:layout_width 和 android:layout_height 则是所有视图的必备属性,用于指定它们的尺寸。

LinearLayout 是布局中的根视图,因此应将宽度和高度设置为 "match_parent",从而填满可供应用使用的整个屏幕区域。 该值表示视图应扩大其宽度或高度,以匹配父视图的宽度或高度

添加TextView


在 activity_main.xml 文件的 <LinearLayout> 元素内,添加以下 <EditText> 元素:

<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:orientation="horizontal">
    <EditText android:id="@+id/edit_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />
</LinearLayout>

这会为视图赋予唯一的标识符,您可以使用该标识符从应用代码中引用对象,例如读取和操作对象(
从 XML 引用任何资源对象时,都需要使用 @ 符号。 请在该符号后依次输入资源类型、斜杠和资源名称 (edit_message)。
只有在第一次定义资源 ID 时,才需要在资源类型之前加一个加号 (+)。 当您编译应用时,SDK 工具会使用 ID 名称在项目的 R.java 文件中新建一个引用 EditText 元素的资源 ID。一旦以此方式声明资源 ID,其他对该 ID 的引用皆无需使用加号。 只有在指定新资源 ID 时才必须使用加号,对于字符串或布局等具体资源则不必如此。

"wrap_content" 值并不规定宽度和高度的具体大小,而是指定根据需要缩放视图,使其适合视图的内容。 如果您要改用 "match_parent",则 EditText 元素将填满屏幕,因为它会匹配父 LinearLayout 的大小。

这是文本字段为空时显示的默认字符串。"@string/edit_message" 并非使用硬编码字符串作为其值,而是引用另一个文件中定义的一个字符串资源。 由于它引用的是一个具体资源(而不仅仅是标识符),因此不需要加号。

添加字符串资源


在strings.xml 中添加两个新字符串。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">My First App</string>
    <string name="edit_message">Enter a message</string>
    <string name="button_send">Send</string>
</resources>

对于用户界面中的文本,务必将每个字符串都指定为资源。 字符串资源允许您在单一位置管理所有 UI 文本,从而简化文本的查找和更新。 此外,将字符串外部化还可让您为每个字符串资源提供替代定义,从而将您的应用本地化为不同的语言。

添加按钮


返回到 activity_main.xml 文件并在 <EditText> 后添加一个按钮。

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <EditText android:id="@+id/edit_message"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:hint="@string/edit_message" />
        <Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/button_send" />
</LinearLayout>

使输入框填满屏幕宽度


修改 <EditText>,使用 android:layout_weight 属性来指定占比,使EditText填满屏幕中除了Button的部分。

<EditText android:id="@+id/edit_message"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message" />

完整的 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:orientation="horizontal"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
    <EditText android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send" />
</LinearLayout>

响应Send按钮


将 android:onClick 属性添加到 <Button> 元素:、

<Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/button_send"
      android:onClick="sendMessage" />

每次用户点击按钮时,此属性均会提示系统调用 Activity 中的 sendMessage() 方法。

在代码中添加 sendMessage() 方法:

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

    /** Called when the user clicks the Send button */
    public void sendMessage(View view) {
        // Do something in response to button
    }
}

要让系统将此方法与为 android:onClick 指定的方法名称匹配,签名必须与所示内容完全相同。具体而言,该方法必须:

构建一个 Intent

在 MainActivity.java 中,将如下所示代码添加到 sendMessage():

public class MainActivity extends AppCompatActivity {
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

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

Intent 构造函数采用两个参数:

putExtra() 方法将 EditText 的值添加到 Intent。Intent 能够以名为 extra 的键值对形式携带数据类型。您的键是一个公共常量 EXTRA_MESSAGE,因为下一个 Activity 将使用该键来检索文本值。为 Intent extra 定义键时最好使用应用的软件包名称作为前缀。这可以确保在您的应用与其他应用交互过程中这些键始终保持唯一。

startActivity() 方法将启动 Intent 指定的 DisplayMessageActivity 实例。现在,您需要创建类。

创建第二个 Activity


1.在 Project 窗口中,右键点击 app 文件夹并选择 New > Activity > Empty Activity。
2.在 Configure Activity 窗口中,为 Activity Name 输入 “DisplayMessageActivity”,然后点击 Finish。

Android Studio 自动执行三项操作:

显示消息


在 DisplayMessageActivity.java 中,向 onCreate() 方法添加下列代码:

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

   Intent intent = getIntent();
   String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
   TextView textView = new TextView(this);
   textView.setTextSize(40);
   textView.setText(message);

   ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message);
   layout.addView(textView);
}

1.调用 getIntent() 采集启动 Activity 的 intent。无论用户如何导航到目的地,每个 Activity 都由一个 Intent 调用。 调用 getStringExtra() 将检索第一个 Activity 中的数据。
2.您可以编程方式创建 TextView 并设置其大小和消息。
3.您可将 TextView 添加到 R.id.activity_display_message 标识的布局。您可将布局投射到 ViewGroup,因为它是所有布局的超类且包含 addView() 方法。

上一篇 下一篇

猜你喜欢

热点阅读