Android-数据的回传

我们当从一个页面跳转到第二个页面进行相关操作后,当关闭第二个页面时,需要从该页面返回一些数据到第一个页面需要用到:

 

 

例如:

第一个Activity的布局文件main_activity.xml,添加运行Activity的页面布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="next"
        android:text="下一页" />

</LinearLayout>

 第二个Activity的布局文件second_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SecondActivity">
    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入名字"
        />
    <EditText
        android:id="@+id/et_age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入年龄"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="返回数据"
        android:onClick="returndata"
        />

</LinearLayout>

创建MainActivity:

package com.example.myapplication55;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {
    private int myrequestCode=111;
    private TextView tv_show;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_show = (TextView)findViewById(R.id.tv_show);//获取id
    }

    //下一页按钮的方法
    public void next(View view) {

        Intent intent=new Intent(this,SecondActivity.class);
        startActivityForResult(intent,myrequestCode);// 1.startActivityForResult()方法,用于开启一个Activity
    }
    //3.onActivityResult()方法,接收回传的数据
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==myrequestCode && resultCode==222){
            String name=data.getStringExtra("my_name");
            int age=data.getIntExtra("my_age",18);
            tv_show.setText(name+"    "+age);
        }
    }
}

创建SecondActivity:

package com.example.myapplication55;

import androidx.appcompat.app.AppCompatActivity;

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

public class SecondActivity extends AppCompatActivity {

    private EditText et_name;
    private EditText et_age;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        et_name = (EditText) findViewById(R.id.et_name);
        et_age = (EditText) findViewById(R.id.et_age);
    }

    //返回数据按钮的方法
    public void returndata(View view) {
        String name=et_name.getText().toString();
        int age=Integer.parseInt(et_age.getText().toString());
        Intent intent=new Intent();
        intent.putExtra("my_name",name);
        intent.putExtra("my_age",age);
        setResult(222,intent);//2. setResult()方法,用于携带数据进行回传
        finish();
    }
}

结果:

首次运行

点击下一页:

 

 

点击返回数据:第二个页面关闭,第一个页面接收数据

 

 

  • 7
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喵俺第一专栏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值