【Android学习笔记】 Intent (2) - 发送消息到另一个页面

来源:初次学习,建议直接看这个官方原版,我这个是总结出来的要点,供自己日后复习,参考

文章目录

    • 1. 效果图 - Anctivity之间传递信息
    • 2. 步骤
      • 第一步:输入载体 Add an EditText to the MainActivity layout
      • 第二步:添加 Add a string to the Intent extras (Intent 附加物)
        • 2.1 声明成员变量
          • 1.Open MainActivity.
          • 2.Add a public constant at the top of the class to define the key for the Intent extra:
          • 3. Add a private variable at the top of the class to hold the EditText
        • 2.2 获取信息
          • 4. In the onCreate() method, use findViewById() to get a reference to the EditText and assign it to that private variable:
          • 5. In the launchSecondActivity() method, just under the new Intent, get the text from the EditText as a string:
          • 6. Add that string to the Intent as an extra with the EXTRA_MESSAGE constant as the key and the string as the value:
      • 第三步 :接受载体 在第二个 Activity 中打开 message
      • 第四步: 获取&显示 Modify SecondActivity to get the extras and display the message
        • 4.1 get 到Intent
        • 4.2 使用键值对中的键或者信息
        • 4.3 hold 住一个 TextView
        • 4.4 向 hold的 `TextView`中 set 信息 (`setText`)

1. 效果图 - Anctivity之间传递信息

此文前接上一篇博客

【Android学习笔记】 Intent (2) - 发送消息到另一个页面_第1张图片

2. 步骤

第一步:输入载体 Add an EditText to the MainActivity layout

添加输入信息的EditText


<android.support.constraint.ConstraintLayout 
省略
    
        android:id="@+id/editText_main"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="36dp"
        android:ems="10"
        android:hint="@string/editText_main"
        android:inputType="textLongMessage"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/button_main"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"/>
android.support.constraint.ConstraintLayout>

第二步:添加 Add a string to the Intent extras (Intent 附加物)

2.1 声明成员变量

1.Open MainActivity.
2.Add a public constant at the top of the class to define the key for the Intent extra:
public static final String EXTRA_MESSAGE = 
                   "com.example.a1104.android.twoactivities.extra.MESSAGE";
                   //自己的地址
3. Add a private variable at the top of the class to hold the EditText
private EditText mMessageEditText;
// 先 hold 住这个位置

2.2 获取信息

4. In the onCreate() method, use findViewById() to get a reference to the EditText and assign it to that private variable:

在onCreate()方法中,使用findViewById()获取对EditText的引用并将其分配给该私有变量

mMessageEditText = (EditText)findViewById(R.id.editText_main); // 发现 find
5. In the launchSecondActivity() method, just under the new Intent, get the text from the EditText as a string:

String message = mMessageEditText.getText().toString(); // 获取,以字符串形式传给 message
6. Add that string to the Intent as an extra with the EXTRA_MESSAGE constant as the key and the string as the value:
intent.putExtra(EXTRA_MESSAGE, message);  // EXTRA_MESSAGE 是键,message是值,构成键值对

完整代码形式:
在 onCreate() 中

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mMessageEditText = (EditText)findViewById(R.id.editText_main);
}

在launchSecondActivity () 中

public void launchSecondActivity(View view) {
        Log.d(LOG_TAG, "Button clicked!");
        Intent intent = new Intent(this, SecondActivity.class);
        String message = mMessageEditText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
}

第三步 :接受载体 在第二个 Activity 中打开 message

Add a TextView to SecondActivity for the message
XML如下:

     
    <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=".activity_second">
    
        <TextView
            android:id="@+id/text_header"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="16dp"
            android:text="@string/text_header"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"
            android:textStyle="bold"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:id="@+id/text_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/text_header" />
    
    android.support.constraint.ConstraintLayout>

第四步: 获取&显示 Modify SecondActivity to get the extras and display the message

Open SecondActivity to add code to the onCreate() method.

4.1 get 到Intent

Intent intent = getIntent();

4.2 使用键值对中的键或者信息

Get the string containing the message from the Intent extras using the MainActivity.EXTRA_MESSAGE static variable as the key:

String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

4.3 hold 住一个 TextView

Use findViewByID() to get a reference to the TextView for the message from the layout:

TextView textView = findViewById(R.id.text_message);

4.4 向 hold的 TextView中 set 信息 (setText

Set the text of the TextView to the string from the Intent extra:

textView.setText(message);

完整的重写的 onCreate 类:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    TextView textView = findViewById(R.id.text_message);
    textView.setText(message);
}

Run


ps:完整代码



<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.a1104.twoactivities">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            intent-filter>
        activity>
        
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.a1104.twoactivities.MainActivity"/>
        activity>
    application>

manifest>

MainAcitivity

package com.example.a1104.twoactivities;

import android.content.Intent;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    private static final String LOG_TAG =
            MainActivity.class.getSimpleName();
    public static final String EXTRA_MESSAGE=
            "com.example.a1104.android.twoactivities.extra.MESSAGE";
    private EditText mMessageEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mMessageEditText = findViewById(R.id.editText_main);
    }

    public void launchSecondActivity(View view) {
        Log.d(LOG_TAG, "Button clicked!");
        Intent intent = new Intent(this,SecondActivity.class);
        String message =mMessageEditText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }
}

activity_main.xml


<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=".MainActivity">


    <Button
        android:id="@+id/button_main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="16dp"
        android:text="@string/button_main"
        android:onClick="launchSecondActivity"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginEnd="16dp" />
    <EditText
        android:id="@+id/editText_main"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:ems="10"
        android:hint="@string/editText_main"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@string/button_main"
        app:layout_constraintStart_toStartOf="parent"
        />

android.support.constraint.ConstraintLayout>

SecondActivity

package com.example.a1104.twoactivities;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
        TextView textView = findViewById(R.id.text_message);
        textView.setText(message);
    }
}

activity_second.xml


<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=".SecondActivity">

    <TextView
        android:id="@+id/text_header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="16dp"
        android:text="@string/text_header"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <TextView
        android:id="@+id/text_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"

        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/text_header"
        android:layout_marginLeft="8dp" />
android.support.constraint.ConstraintLayout>

你可能感兴趣的:(Android)