Android activity传输与接收数据项目自我介绍

在这里插入图片描述
在这里插入图片描述
主要代码
前端

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名:"
            android:textSize="30dp" />

        <EditText
            android:id="@+id/xingming"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="专业:"
            android:textSize="30dp" />

        <EditText
            android:id="@+id/zhuanye"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="自我介绍:"
            android:textSize="30dp" />

        <Button
            android:id="@+id/bianji"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="编辑"
            android:textSize="30dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="300dp">

        <TextView
            android:id="@+id/zhanshi"
            android:textSize="30dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="自我介绍:"
            android:textSize="30dp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="400dp">
        <EditText
            android:id="@+id/bianji"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="25dp"/>
    </LinearLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/tijiao"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="提交"
            android:textSize="30dp"
            android:layout_centerHorizontal="true"/>
    </RelativeLayout>
</LinearLayout>

后端

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {
    
    

    private TextView xingming, zhuanye, zhanshi;
    private Button bianji;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        xingming = findViewById(R.id.xingming);
        zhuanye = findViewById(R.id.zhuanye);
        zhanshi = findViewById(R.id.zhanshi);
        bianji = findViewById(R.id.bianji);

        bianji.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View v) {
    
    
                Intent intent = new Intent();
                intent.setClass(com.example.ziwojieshao.MainActivity.this, JieShao.class);
                Bundle bundle = new Bundle();
                bundle.putString("now", zhanshi.getText().toString());
                bundle.putString("name",xingming.getText().toString());
                intent.putExtras(bundle);
                startActivityForResult(intent, 2);
            }
        });
    }
    //重写 onActivityResult()方法,接收传回的数据
    @Override
    protected void onActivityResult ( int requestCode,
                                      int resultCode, Intent data){
    
    
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2 && resultCode == RESULT_OK) {
    
    
            Bundle bundle = data.getExtras();
            String state = bundle.getString("state");
            zhanshi.setText(state);
        }
    }
}

import android.content.Intent;
import android.os.Bundle;
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 JieShao extends AppCompatActivity {
    
    

    private TextView name;
    private EditText bianji;
    private Button tijiao;
    private Intent intent = new Intent();
    private Bundle bundle = new Bundle();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jieshao);

        bianji = findViewById(R.id.bianji);
        tijiao = findViewById(R.id.tijiao);
        name=findViewById(R.id.name);
        //获取 Intent 对象
        Intent intent = getIntent();
        //获取传来的 Bundle 对象
        Bundle bundle = intent.getExtras();
        //取出当前
        String nowStatus = bundle.getString("now");
        bianji.setText(nowStatus);
        String nameStatus=bundle.getString("name");
        name.setText(nameStatus+"的自我介绍");

        tijiao.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View v) {
    
    
                bundle.putString("state", bianji.getText().toString());
                intent.putExtras(bundle);
                setResult(RESULT_OK, intent);
                finish();
            }
        });

    }
}

猜你喜欢

转载自blog.csdn.net/qq_45688164/article/details/121610629