安卓数据回传的例子+详细代码

效果图
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

Main2Activity.java`

package xdglyt.software.jhwz.myapplication9bataback;

import androidx.appcompat.app.AppCompatActivity;

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

public class Main2Activity extends AppCompatActivity {
    private TextView tv1;
    private TextView tv2;
    private Button btn;
    private MyInfo myInfo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        tv1 = (TextView) findViewById(R.id.tv_show_name);
        tv2 = (TextView) findViewById(R.id.tv_show_psd);
        btn = (Button) findViewById(R.id.edit);

    }

    public void onclick(View v) {
        Intent intent = new Intent(this,MainActivity.class);
        startActivityForResult(intent,1);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        if(data != null){
            if(requestCode == 1){
                if(resultCode == 1){
                    myInfo = (MyInfo) data.getSerializableExtra("myInfo");
                    updateMyInfo(myInfo);
                }
            }
        }
    }

    private void updateMyInfo(MyInfo myInfo){
        tv1.setText(myInfo.getName()+"");
        tv2.setText(myInfo.getPsd()+"");
    }
}

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".Main2Activity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="15dp">
        <TextView
            style="@style/text"
            android:text="姓名:"/>
        <TextView
            android:id="@+id/tv_show_name"
            android:background="#fff"
            style="@style/input"
            />

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="15dp">
        <TextView
            style="@style/text"
            android:text="密码:"/>
        <TextView
            android:id="@+id/tv_show_psd"
            android:background="#fff"
            style="@style/input"
            />

    </LinearLayout>
    <Button
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:text="edit"
        android:onClick="onclick"
        android:textSize="25sp"/>


</LinearLayout>

MainActivity.java

package xdglyt.software.jhwz.myapplication9bataback;

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;

public class MainActivity extends AppCompatActivity {
    private Button btn;
    private EditText et1;
    private EditText et2;
    private MyInfo myInfo;
    private String name;
    private String psd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = (Button) findViewById(R.id.submit);
        et1 = (EditText) findViewById(R.id.et_name);
        et2 = (EditText) findViewById(R.id.et_psd);

    }
    public void onclick(View v) {
        Intent intent = new Intent();
        name = et1.getText().toString();
        psd = et2.getText().toString();
        myInfo = new MyInfo(name,psd);
        intent.putExtra("myInfo",myInfo);
        setResult(1,intent);
        finish();
    }
}

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            style="@style/text"
            android:text="用户名:"/>
        <EditText
            android:id="@+id/et_name"
            style="@style/input"
            android:hint="please input the name"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            style="@style/text"
            android:text="密   码:"/>
        <EditText
            android:id="@+id/et_psd"
            style="@style/input"
            android:hint="please input the name"/>
    </LinearLayout>
    <Button
        android:id="@+id/submit"
        android:onClick="onclick"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:text="submit"
        android:textSize="25sp"/>

</LinearLayout>

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="text">
        <item name="android:textSize">25sp</item>
        <item name="android:layout_weight">2</item>
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:gravity">right</item>
        <item name="android:paddingRight">5dp</item>
    </style>
    <style name="input">
        <item name="android:textSize">25sp</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:padding">5dp</item>
        <item name="android:textColor">#000</item>
        <item name="android:paddingLeft">20dp</item>
        <item name="android:background">#fff</item>
        <item name="android:layout_marginRight">15dp</item>
        <item name="android:layout_weight">5</item>
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_marginLeft">8dp</item>

    </style>
</resources>

MyInfo.java

package xdglyt.software.jhwz.myapplication9bataback;

import java.io.Serializable;

public class MyInfo implements Serializable {
    private String name;
    private String psd;

    public MyInfo(String name,String psd){
        this.name = name;
        this.psd = psd;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPsd() {
        return psd;
    }

    public void setPsd(String psd) {
        this.psd = psd;
    }
}

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="xdglyt.software.jhwz.myapplication9bataback">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Main2Activity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity">

        </activity>
    </application>

</manifest>
  • 0
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

365JHWZGo

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

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

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

打赏作者

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

抵扣说明:

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

余额充值