java怎么返回上一部,如何返回数据给上一个活动

返回上一个活动只需要按以下Back键就可以了,并且使用startActivityForResult(),这个方法也是用于启动活动的,而且会返回一个结果给上一个活动。

startActivityForResult()方法接收两个参数,第一个参数是Intent,第二个参数是请求码,用于在之后回调中判断数据的来源。

以下为例子:

首先你需要两个可以互传的Activity ,这里,我们以MainActivity和SecondActivity为例:

MainActivity的Layout如下:<?xml  version="1.0" encoding="utf-8"?>

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"

android:background="#ffffff"

tools:context=".MainActivity">

android:id="@+id/edt"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="top"

android:minLines="3"

android:maxLines="3"

android:textColor="#000000"

android:textSize="20dp"

android:hint="请输入.."

android:textColorHint="#707070"/>

android:id="@+id/btn"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="点击"

android:textSize="15dp"

android:textColor="#000000"/>

android:id="@+id/txt_first"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="变变变"

android:textColor="#000000"

android:textSize="20dp"

android:layout_marginTop="10dp"/>

SecondActivity的Layout如下:<?xml  version="1.0" encoding="utf-8"?>

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:background="#ffffff"

android:orientation="vertical"

tools:context=".SecondActivity">

android:id="@+id/txt"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="20dp"

android:textColor="#000000"/>

android:id="@+id/btn_1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="20dp"

android:textColor="#000000"

android:text="点吧"

android:layout_marginTop="10dp"/>

接着,我们要让SecondActivity在点击了按钮btn_1之后返回数据到MainActivity:import androidx.annotation.Nullable;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.SearchView;

import android.widget.TextView;

import java.io.BufferedWriter;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStreamWriter;

public class MainActivity extends AppCompatActivity {

private EditText edt;

private Button btn;

private TextView txt;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

edt = findViewById(R.id.edt);

btn = findViewById(R.id.btn);

txt = findViewById(R.id.txt_first);

btn.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

String data = edt.getText().toString() ;

Intent intent = new Intent(MainActivity.this,SecondActivity.class);

intent.putExtra("extra_data",data);

startActivityForResult(intent,1);

}

});

}

@Override

protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

super.onActivityResult(requestCode, resultCode, data);

switch(requestCode){

case 1:

if(resultCode == RESULT_OK){

String returnedData = data.getStringExtra("return_data");

txt.setText(returnedData);

}

break;

default:

}

}

}

然后是SecondActivity的代码: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.TextView;

public class SecondActivity extends AppCompatActivity {

private TextView txt2;

private Button btn_2;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_second);

Intent intent = getIntent();

String data= intent.getStringExtra("extra_data");

txt2 = findViewById(R.id.txt);

btn_2=findViewById(R.id.btn_1);

txt2.setText(data);

btn_2.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Intent intent = new Intent(SecondActivity.this,MainActivity.class);

intent.putExtra("return_data","This is the return of Second Activity.");

setResult(RESULT_OK,intent);

finish();

}

});

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 复数类complex可以包含以下属性和方法: 属性: - 实部(real):表示复数的实部,类型为double。 - 虚部(imaginary):表示复数的虚部,类型为double。 方法: - 构造方法(Complex):用于创建复数对象,可以接受实部和虚部作为参数。 - 加法方法(add):用于将两个复数相加,返回一个新的复数对象。 - 减法方法(subtract):用于将两个复数相减,返回一个新的复数对象。 - 乘法方法(multiply):用于将两个复数相乘,返回一个新的复数对象。 - 除法方法(divide):用于将两个复数相除,返回一个新的复数对象。 - 模长方法(abs):用于计算复数的模长,返回一个double类型的值。 - toString方法:用于将复数对象转换为字符串表示。 下面是一个简单的复数类complex的实现: public class Complex { private double real; private double imaginary; public Complex(double real, double imaginary) { this.real = real; this.imaginary = imaginary; } public Complex add(Complex other) { double real = this.real + other.real; double imaginary = this.imaginary + other.imaginary; return new Complex(real, imaginary); } public Complex subtract(Complex other) { double real = this.real - other.real; double imaginary = this.imaginary - other.imaginary; return new Complex(real, imaginary); } public Complex multiply(Complex other) { double real = this.real * other.real - this.imaginary * other.imaginary; double imaginary = this.real * other.imaginary + this.imaginary * other.real; return new Complex(real, imaginary); } public Complex divide(Complex other) { double denominator = other.real * other.real + other.imaginary * other.imaginary; double real = (this.real * other.real + this.imaginary * other.imaginary) / denominator; double imaginary = (this.imaginary * other.real - this.real * other.imaginary) / denominator; return new Complex(real, imaginary); } public double abs() { return Math.sqrt(real * real + imaginary * imaginary); } @Override public String toString() { return real + (imaginary < ? "-" : "+") + Math.abs(imaginary) + "i"; } } 使用示例: Complex c1 = new Complex(1, 2); Complex c2 = new Complex(3, -4); Complex sum = c1.add(c2); Complex difference = c1.subtract(c2); Complex product = c1.multiply(c2); Complex quotient = c1.divide(c2); System.out.println("c1 = " + c1); System.out.println("c2 = " + c2); System.out.println("c1 + c2 = " + sum); System.out.println("c1 - c2 = " + difference); System.out.println("c1 * c2 = " + product); System.out.println("c1 / c2 = " + quotient); System.out.println("|c1| = " + c1.abs()); System.out.println("|c2| = " + c2.abs()); ### 回答2: 复数是由实数和虚数构成的数字,可以用a+bi的形式表示,其中a和b分别为实数和虚数部分。Java中可以设计一个复数类Complex来表示复数,该类应包含以下成员变量和方法。 成员变量: - 实数部分real,用double类型保存 - 虚数部分imaginary,用double类型保存 方法: - 构造方法Complex(double real, double imaginary):创建一个给定实数和虚数部分的复数对象 - 无参构造方法Complex():创建一个值为0的复数对象 - getter和setter方法:获取和设置各成员变量的值 - add方法:复数加法,将当前复数对象与给定复数相加并返回结果 - sub方法:复数减法,将当前复数对象与给定复数相减并返回结果 - mul方法:复数乘法,将当前复数对象与给定复数相乘并返回结果 - div方法:复数除法,将当前复数对象除以给定复数并返回结果 - abs方法:计算复数的模,即|a+bi| = √(a^2+b^2),返回double类型的结果 - toString方法:将复数对象转换成字符串形式,例如"3.0+4.0i" 其中,加法、减法、乘法、除法的运算规则如下: - 加法:(a+bi)+(c+di) = (a+c)+(b+d)i - 减法:(a+bi)-(c+di) = (a-c)+(b-d)i - 乘法:(a+bi)*(c+di) = (ac-bd)+(ad+bc)i - 除法:(a+bi)/(c+di) = (ac+bd)/(c^2+d^2) + (bc-ad)/(c^2+d^2)i 复数类Complex可以提供一些基本的应用场景,例如: - 在图像处理中,使用复数表示频域中的信号,进行傅里叶变换等操作 - 在电路分析中,使用复数表示电压、电流等复杂的信号,并进行相位和幅度等计算 - 在计算物理学中,使用复数表示波包、波函数等量,进行量子力学计算等 总之,Java的复数类Complex可以用于各种需要表示复数的领域,具有广泛的应用价值。 ### 回答3: Java是一种非常流行的编程语言,因其独特的面向对象设计理念而备受欢迎。面向对象思想强调数据和操作的结合,将现实世界中的概念和实体抽象成类(class),并通过类来描述对象(object)。在这种模式下,为了对复数进行计算,可以设计一个复数类。 复数是由实部和虚部组成的数,可以用双精度浮点数表示。我们可以首先定义复数类的成员变量,包括实部real和虚部imaginary: ``` public class Complex { private double real; private double imaginary; } ``` 接着,我们需要为复数类设计一些方法来实现计算。对于复数的加、减、乘、除等操作,都是基于实部和虚部的,因此我们需要考虑如何实现这些操作。例如,对于两个复数的加法,可以按照如下方式实现: ``` public Complex add(Complex number) { double realSum = real + number.real; double imaginarySum = imaginary + number.imaginary; return new Complex(realSum, imaginarySum); } ``` 这里我们定义了一个add方法,输入为另一个复数对象number,返回一个新的复数对象,其实部为两个实部之和,虚部为两个虚部之和。 类似地,我们还可以设计subtract、multiply、divide等方法来实现复数的减法、乘法、除法等操作。总体设计如下: ``` public class Complex { private double real; private double imaginary; public Complex(double real, double imaginary) { this.real = real; this.imaginary = imaginary; } public Complex add(Complex number) { double realSum = real + number.real; double imaginarySum = imaginary + number.imaginary; return new Complex(realSum, imaginarySum); } public Complex subtract(Complex number) { double realDiff = real - number.real; double imaginaryDiff = imaginary - number.imaginary; return new Complex(realDiff, imaginaryDiff); } public Complex multiply(Complex number) { double realProd = real * number.real - imaginary * number.imaginary; double imaginaryProd = real * number.imaginary + imaginary * number.real; return new Complex(realProd, imaginaryProd); } public Complex divide(Complex number) { double denominator = number.real * number.real + number.imaginary * number.imaginary; double realQuot = (real * number.real + imaginary * number.imaginary) / denominator; double imaginaryQuot = (imaginary * number.real - real * number.imaginary) / denominator; return new Complex(realQuot, imaginaryQuot); } } ``` 以上是一个简单的复数类的设计,可以用于实现常见的复数计算。总的来说,Java提供了丰富的面向对象的特性,可以让我们按照不同的需求设计出各种不同的类。复数类只是其中的一种,实践中还有许多其他有趣的类的设计。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值