Day30 - Intent传讯息

昨天已经学会新增页面和跳页功能了
但如果单纯跳页好像没什麽用
势必要传一点讯息过去另一页
今天就来学
从第一页传你输入的字到第二页
用TextView显示出来
Let's 走

开始

我们直接拿昨天的专案
第一页:新增EditText
第二页:新增TextView

第一页

activity_main.xml

拉一个EditText出来

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

    <EditText
        android:id="@+id/et_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        app:layout_constraintBottom_toTopOf="@+id/btn_totwo"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
    <Button
        android:id="@+id/btn_totwo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳第二页"
        android:textSize="30dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

这里要比昨天多一个程序码
intent.putExtra();

intent.putExtra("send",et_input.getText().toString());

完整程序:

package com.example.intent;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    Button btn_totwo;
    EditText et_input;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_totwo = findViewById(R.id.btn_totwo);
        btn_totwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,Two.class);
                et_input = findViewById(R.id.et_input);
                intent.putExtra("send",et_input.getText().toString());
                startActivity(intent);
            }
        });
    }
}

第二页

activity_two.xml

拉一个TextView显示传过来的结果

<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=".Two"
    android:background="@color/black">

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:textSize="30dp"
        android:textColor="@color/white"
        app:layout_constraintBottom_toTopOf="@+id/btn_back"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
    <Button
        android:id="@+id/btn_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳回去"
        android:textSize="30dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Two.java

第一页传了一个讯息
那这一页势必要接刚刚传过来的讯息
intent.getXXXExtra(); XXX是你刚刚传过来的资料型别名称
刚刚是传字串过来
所以这边用String
接收的程序码:

Intent intent = getIntent();
String send = intent.getStringExtra("send");//send为上一页设的name

完整程序:

package com.example.intent;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextClock;
import android.widget.TextView;

public class Two extends AppCompatActivity {
    Button btn_back;
    TextView tv_result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);
        tv_result = findViewById(R.id.tv_result);
        btn_back = findViewById(R.id.btn_back);
        Intent intent = getIntent();
        String send = intent.getStringExtra("send");
        tv_result.setText(send);
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
}

执行结果:

  • 初始画面:(输入:handsome boyhan)
    https://ithelp.ithome.com.tw/upload/images/20211005/20141769cyN26lgq8y.png
  • 点跳第二页後
    https://ithelp.ithome.com.tw/upload/images/20211005/20141769nvLjQe9KQ4.png

Intent其实有非常多功能
开启其他APP,也是用Intent的喔

30天挑战终於结束了
Android Studio还有很多功能可以学习
有兴趣的网路上有一堆教学影片或文章
可以供大家学习
就在这边跟大家说:
掰掰搂
/images/emoticon/emoticon29.gif/images/emoticon/emoticon29.gif


<<:  Day 27. Zabbix 实际报警案例分享 - 执行绪异常飙高

>>:  企划实现(26)

Day-28 : Model 多对多

今天我们要来讲多对多 什麽是多对多 举个例子: 商店has_many商品,商品也has_many商店...

Day 0xE UVa10812 Beat the Spread!

Virtual Judge ZeroJudge 题意 输入比赛的分数总和及分差,输出两队分数 需要...

[28] 30 天从 Swift 学会 Objective-C:Swift friendly 的 API Swift name

在 Objective-C 与 Swift 的命名有明显的区别,虽然 Swift interface...

防止自动锁屏

缘由: UIUX提出说想让App使用者的手机可以一直亮着,不会自动锁屏,虽然心里知道是有可能的,但没...

[DAY4]K8S里面的小小兵-POD

图片来源 我们可以从上面的架构图清楚看出,POD是K8S中最小可被部署的构物。 图片来源 一个k8...