Day9 Android - Intent(换页)的基础上->A页面传值(bundle)至B页面

继昨天讲完了Intent(换页),今天主要要讲bundle(传值)的部分,接续昨天设计完的程序再来加以设计。

Bundle

昨天讲的intent用一个比喻来说的话,比较像是交通工具,就像是我今天从A地搭飞机至B地,飞机是交通工具,也可以把她想成intent,而准备的行李,就可以把他想成bundle,也就是bundle这个应用,intent是必要的,那麽就开始设计bundle的部分。

那麽今天就来设计一个简单的注册、登入,首先先宣告三个资料:使用者名称、帐号、密码,首先先看到布局的图。

布局

https://ithelp.ithome.com.tw/upload/images/20210814/201392591AroJDGU5a.png
注册页-程序码:

<?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:id="@+id/ConstraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="159dp"
        android:layout_height="48dp"
        android:background="@color/purple_200"
        android:text="注册"
        android:textSize="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.543" />

    <EditText
        android:id="@+id/editTextTextPersonName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="152dp"
        android:layout_marginTop="116dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editTextTextEmailAddress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="152dp"
        android:layout_marginTop="196dp"
        android:ems="10"
        android:inputType="textEmailAddress"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editTextTextPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="152dp"
        android:layout_marginTop="272dp"
        android:ems="10"
        android:inputType="textPassword"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="134dp"
        android:layout_height="39dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="120dp"
        android:gravity="center"
        android:text="使用者名称:"
        android:textSize="18dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="112dp"
        android:layout_height="39dp"
        android:layout_marginStart="40dp"
        android:layout_marginTop="200dp"
        android:gravity="center"
        android:text="帐号:"
        android:textSize="20dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="112dp"
        android:layout_height="39dp"
        android:layout_marginStart="40dp"
        android:layout_marginTop="276dp"
        android:gravity="center"
        android:text="密码:"
        android:textSize="20dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

https://ithelp.ithome.com.tw/upload/images/20210814/20139259VziA2ldvFk.png
登入页-程序码:

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

    <EditText
        android:id="@+id/editTextTextEmailAddress2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="108dp"
        android:layout_marginTop="140dp"
        android:ems="10"
        android:inputType="textEmailAddress"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editTextTextPassword2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="108dp"
        android:layout_marginTop="244dp"
        android:ems="10"
        android:inputType="textPassword"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="68dp"
        android:layout_height="25dp"
        android:layout_marginTop="148dp"
        android:layout_marginEnd="4dp"
        android:gravity="center"
        android:text="帐号:"
        android:textSize="20dp"
        app:layout_constraintEnd_toStartOf="@+id/editTextTextEmailAddress2"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="68dp"
        android:layout_height="25dp"
        android:layout_marginTop="252dp"
        android:layout_marginEnd="4dp"
        android:gravity="center"
        android:text="密码:"
        android:textSize="20dp"
        app:layout_constraintEnd_toStartOf="@+id/editTextTextPassword2"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="139dp"
        android:layout_height="48dp"
        android:layout_marginStart="136dp"
        android:layout_marginTop="340dp"
        android:text="登入"
        android:textSize="20dp"
        android:background="@color/purple_200"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

程序-MainActivity

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText u = findViewById(R.id.editTextTextPersonName);
        EditText a = findViewById(R.id.editTextTextEmailAddress);
        EditText p = findViewById(R.id.editTextTextPassword);
        Button B = findViewById(R.id.button);
        B.setOnClickListener(new View.OnClickListener() {    //因为我的Button名称宣告是B,所以是监听按钮B
            @Override
            public void onClick(View v) {
                String username=u.getText().toString();//取得资料(getText)转为string型态
                String account=a.getText().toString();
                String password=p.getText().toString();
                //如果有其中一笔资料去除空格(trim)==""(空值)成立的话,结果会反向(false)为不进入if,若都为false则为有资料=true
                if(!(("".equals(username.trim())) || ("".equals(account.trim())) || ("".equals(password.trim())))){
                    Toast.makeText(MainActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent();//新增交通工具
                    intent.setClass(MainActivity.this, Activity2.class);//目的地
/*************************************bundle片段*******************************************/
                    Bundle bundle = new Bundle();//新增行李箱(存放资料)
                    bundle.putString("username_key",username);//行李的东西(资料,取值用key)
                    bundle.putString("account_key",account);
                    bundle.putString("password_key",password);
                    intent.putExtras(bundle); /******************************************************************************************/
                    startActivity(intent);
                }
                else
                    Toast.makeText(MainActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
            }

        });
    }
}

这是MainActivity的程序码,可以看到bundle传入三个key值的资料,当然另外还有putInt之类的型态可以传入,最後利用putExtras这个方法把所有传入的东西包装起来,在startActivity(intent)就会传过去了,另外我有写了如果其中有资料未填入的话,则会toast出注册失败且不跳页。


程序-Activity2

public class Activity2 extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page2);
        Button B=findViewById(R.id.button2);//登入按钮
        EditText a=findViewById(R.id.editTextTextEmailAddress2);
        EditText p=findViewById(R.id.editTextTextPassword2);
        /***************************bundle取值*******************************/
        Bundle bundle = getIntent().getExtras();//取得前一个activity放进来的资料
        String account_register=bundle.getString("account_key");//用你刚刚设的key去取资料
        String password_register=bundle.getString("password_key");
        String username_register=bundle.getString("username_key");//使用者名称
        /*******************************************************************/
        B.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account_login=a.getText().toString();//此activity登入的资料
                String password_login=p.getText().toString();
                if(account_login.equals(account_register) && password_login.equals(password_register))//比对资料
                {
                    Toast.makeText(Activity2.this,username_register+"登入成功",Toast.LENGTH_SHORT).show();
                }
                else
                    Toast.makeText(Activity2.this,"登入失败",Toast.LENGTH_SHORT).show();
            }
        });
    }
}

可以透过getIntent().getExtras()来取得所有传入的资料,再透过getString(key)来取key对应输入的资料。
另外其他就是一些比对资料相同的动作。

成果:

https://ithelp.ithome.com.tw/upload/images/20210814/20139259vWT8ilNHwI.jpg

https://ithelp.ithome.com.tw/upload/images/20210814/2013925961czSiTmd6.jpg

https://ithelp.ithome.com.tw/upload/images/20210814/20139259hCyj5hq0bE.jpg


<<:  Day09 - [丰收款] 安全无虞後,开始建立订单:ATM虚拟帐号篇 (1)

>>:  AE-Lightning 雷电云特效1-Day23

Day 10 : PHP - 常用的阵列函数有哪些?

上篇介绍了PHP的阵列宣告、印出方式,这篇想和大家介绍PHP常用的阵列函数有哪些 1.in_arra...

Day20. 懂Bootstrap,并让Bootstrap带你上天堂

工程师都有google的习惯,但是 bootstrap 的使用方式不用特别去估狗,基本的用法只要看官...

参加 VMware 1V0-21.20 认证考试以获得成功的职业生涯

您是 VMware 认证技术助理 - 数据中心虚拟化考试的有抱负的候选人之一吗?然後你就中了头奖!多...

[Day27] Dev Ops

其实这次就已经有一个铁人赛组别完全是 DevOps 了,如果对於这个领域想要比较深入的了解,可以去看...

【零基础成为 AI 解梦大师秘笈】Day26 - 周易解梦之人工智慧(7)

人工智慧7 前言 系列文章简介 大家好,我们是 AI . FREE Team - 人工智慧自由团队,...