使用Bundle在Activity中交换数据

大概过程

编写demo

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textColor="#ff0000"
        android:text="@string/intruduce"/>
    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:hint="请输入姓名"
        android:textColor="#000"
        android:layout_below="@+id/top"/>

    <EditText
        android:id="@+id/city"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:hint="请输入所在城市"
        android:textColor="#000"
        android:layout_below="@+id/name"/>

    <EditText
        android:id="@+id/sex"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:hint="请输入性别"
        android:textColor="#000"
        android:layout_below="@+id/city"/>
    <EditText
        android:id="@+id/phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:hint="请输入电话号码"
        android:textColor="#000"
        android:layout_below="@+id/sex"/>

    <EditText
        android:id="@+id/age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:hint="请输入年龄"
        android:textColor="#000"
        android:layout_below="@+id/phone"/>
    <Button
        android:id="@+id/btn_submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginRight="10dp"
        android:background="#ff5000"
        android:text="提交"/>
</RelativeLayout>

activity_result.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".ResultActivity">
    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:text="朱归强"
        android:textColor="#000" />
    <TextView
        android:id="@+id/sex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textColor="#000"
        android:text="男"
        android:layout_below="@+id/name"/>

    <LinearLayout
        android:id="@+id/agel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@+id/sex">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:textColor="#000"
            android:text="年龄:"/>
        <TextView
            android:id="@+id/age"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:text="23"
            android:textColor="#000"
            />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/cityl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@+id/agel">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:textColor="#000"
            android:text="所在城市:"/>
        <TextView
            android:id="@+id/city"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:text="长沙"
            android:textColor="#000"
            />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/phonel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@+id/cityl">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:textColor="#000"
            android:text="电话:"/>
        <TextView
            android:id="@+id/phone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:text="13155115510"
            android:textColor="#000"
            />
    </LinearLayout>

    <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginRight="10dp"
        android:textSize="26sp"
        android:textColor="#F31D0B"
        android:text="今日幸运值:60%"/>
</RelativeLayout>

MainActivity.java

package com.example.bundledemo;

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;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btnSubmit = (Button) findViewById(R.id.btn_submit);
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = ((EditText)findViewById(R.id.name)).getText().toString();
                String sex = ((EditText) findViewById(R.id.sex)).getText().toString();
                String age = ((EditText) findViewById(R.id.age)).getText().toString();
                String city = ((EditText) findViewById(R.id.city)).getText().toString();
                String phone = ((EditText) findViewById(R.id.phone)).getText().toString();
                if(!"".equals(name)&&!"".equals(sex)&&!"".equals(age)&&!"".equals(city)&&!"".
                        equals(phone)){
                    Intent intent = new Intent(MainActivity.this, ResultActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putCharSequence("name",name);
                    bundle.putCharSequence("sex",sex);
                    bundle.putCharSequence("age",age);
                    bundle.putCharSequence("city",city);
                    bundle.putCharSequence("phone",phone);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }else {
                    Toast.makeText(MainActivity.this, "请将信息填写完整", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

ResultActivity.java

package com.example.bundledemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;


public class ResultActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();

        String name = bundle.getString("name");
        String sex = bundle.getString("sex");
        String age = bundle.getString("age");
        String city = bundle.getString("city");
        String phone = bundle.getString("phone");

        TextView tv_name = (TextView) findViewById(R.id.name);
        TextView tv_sex = (TextView) findViewById(R.id.sex);
        TextView tv_age = (TextView) findViewById(R.id.city);
        TextView tv_city = (TextView) findViewById(R.id.age);
        TextView tv_phone = (TextView) findViewById(R.id.phone);

        tv_name.setText(name);
        tv_sex.setText(sex);
        tv_age.setText(age);
        tv_city.setText(city);
        tv_phone.setText(phone);
    }
}

效果:

posted @ 2020-04-22 19:21  千雨千寻  阅读(167)  评论(0编辑  收藏  举报