获取Activity的返回值

获取另一个Activity界面的传值

模拟

来两个xml页面表示2个Activity
activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TextView
        android:id="@+id/tv_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="接收Activity返回值示例\n"
        />
    <Button android:id="@+id/btn_1"
        android:text="启动SecondActivity"
        android:textAllCaps="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"/>
    <TextView
        android:id="@+id/tv_return"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textColor="#000" />
</LinearLayout>


在这里插入图片描述

activity_second.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TextView
        android:id="@+id/tv_message2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="这里是SecondActivity\n"
        />
    <EditText
        android:id="@+id/txt_return"
        android:layout_width="match_parent"
        android:layout_height="60dip"
        android:hint="单击输入文字"
        />
    <TextView
        android:id="@+id/tv_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <Button android:id="@+id/btn_return"
        android:text="返回"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"/>
</LinearLayout>


在这里插入图片描述

方法

1. 从主界面跳转目标Activity

要用setActivityForResult(Intent,requestCode)方法调用,而不能用setActivity()方法调用。【参数一:Intent对象,参数二:是唯一标识目标Activity的标识码】

2. 从目标Activity返回

要调用setResult()方法设置返回值。【有俩参数,参数一:resultCode,参数二:标识Intent的结果数据】

代码

MainActivity.java


package com.example.week;

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private TextView tvReceive;
    private Intent myintent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvReceive=(TextView)findViewById(R.id.tv_return);
        Button btnstart=(Button)findViewById(R.id.btn_1);
        myintent =new Intent();
        myintent.setClass(MainActivity.this,SecondActivity.class);
        btnstart.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                startActivityForResult(myintent,1);
            }
        });
    }
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        if(data!=null){
            String receive = data.getStringExtra("backstring");
            if(requestCode==1){
                tvReceive.setText("\n接受到的返回数据是:"+receive);
            }
            else{
                tvReceive.setText("没有接收到任何返回数据");
            }
        }
    }
}

SecondActivity.java


package com.example.week;


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

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

public class SecondActivity extends AppCompatActivity {
    private String backstr;
    private EditText txtreturn;
    private TextView input;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        txtreturn=(EditText)findViewById(R.id.txt_return);
        input=(TextView)findViewById(R.id.tv_input);
        Button btnreturn=(Button)findViewById(R.id.btn_return);
        txtreturn.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
                backstr=txtreturn.getText().toString();
                return false;
            }
        });
        btnreturn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.putExtra("backstring",backstr);
                setResult(0,intent);
                finish();
            }
        });
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值