多activity界面跳转并传递数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多activity界面跳转并传递数据相关的知识,希望对你有一定的参考价值。

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:id="@+id/activity_main"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     android:orientation="vertical"
 8     tools:context="com.example.zhoushasha.myapplication.MainActivity">
 9 
10     <EditText
11         android:id="@+id/et1"
12         android:layout_width="wrap_content"
13         android:layout_height="wrap_content"
14         android:hint="请输入姓名"
15         android:layout_gravity="center"
16         android:textSize="25dp"
17         />
18     <View
19         android:layout_width="match_parent"
20         android:layout_height="1dp"/>
21     <LinearLayout
22         android:layout_width="match_parent"
23         android:layout_height="wrap_content">
24         <Button
25             android:id="@+id/bt1"
26             android:layout_weight="1"
27             android:onClick="onClick"
28             android:layout_width="wrap_content"
29             android:layout_height="wrap_content"
30             android:text="进入评估"
31             android:textSize="25dp"/>
32         <Button
33             android:id="@+id/bt2"
34             android:layout_weight="1"
35             android:onClick="onClick"
36             android:layout_width="wrap_content"
37             android:layout_height="wrap_content"
38             android:text="退出"
39             android:textSize="25dp"/>
40     </LinearLayout>
41     <TextView
42         android:id="@+id/tv1"
43         android:layout_gravity="center_horizontal"
44         android:layout_width="wrap_content"
45         android:layout_height="wrap_content"
46         android:text="评论结果"
47         android:textSize="25dp"/>
48 </LinearLayout>
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:id="@+id/activity_main2"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     android:orientation="vertical"
 8     tools:context="com.example.zhoushasha.myapplication.Main2Activity">
 9 <TextView
10     android:id="@+id/tv1"
11     android:layout_width="match_parent"
12     android:layout_height="wrap_content"
13     android:text="有没有信心?"
14     android:textSize="54dp" />
15     <LinearLayout
16         android:layout_width="match_parent"
17         android:layout_height="wrap_content"
18         android:orientation="vertical" />
19 
20         <Button
21             android:id="@+id/bt1"
22             android:layout_width="match_parent"
23             android:layout_height="wrap_content"
24             android:text=""
25             android:textSize="25dp"/>
26 
27     <Button
28         android:id="@+id/bt2"
29         android:layout_width="match_parent"
30         android:layout_height="wrap_content"
31         android:onClick="onClick"
32         android:text="没有"
33         android:textSize="25dp"/>
34 </LinearLayout>

第一个activity的跳转功能实现代码

 1 package com.example.zhoushasha.myapplication;
 2 
 3 import android.content.Context;
 4 import android.content.Intent;
 5 import android.support.v7.app.AppCompatActivity;
 6 import android.os.Bundle;
 7 import android.text.TextUtils;
 8 import android.view.View;
 9 import android.widget.Button;
10 import android.widget.EditText;
11 import android.widget.TextView;
12 import android.widget.Toast;
13 
14 import static android.R.attr.data;
15 import static android.R.attr.name;
16 import static android.R.attr.revisionCode;
17 
18 public class MainActivity extends AppCompatActivity {
19     private Button bt1;
20     private Button bt2;
21     private Context mContent;
22     private EditText et1;
23     private TextView tv1;
24     public static final int REQUEST_CODE = 1000;
25     public static final int RESULT_CODE = 1001;
26 
27     @Override
28     protected void onCreate(Bundle savedInstanceState) {
29         super.onCreate(savedInstanceState);
30         setContentView(R.layout.activity_main);
31         et1 = (EditText) findViewById(R.id.et1);
32         bt1 = (Button) findViewById(R.id.bt1);
33         bt2 = (Button) findViewById(R.id.bt2);
34         tv1 = (TextView) findViewById(R.id.tv1);
35 
36     }
37            public void onClick(View view) {
38                switch (view.getId()) {
39                    case R.id.bt1:
40                        setBt1();
41                        break;
42                }
43            }
44 
45            private void setBt1() {
46                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
47 
48                if (TextUtils.isEmpty(et1.getText().toString())) {
49                    Toast.makeText(MainActivity.this, "输入名称不能为空", Toast.LENGTH_SHORT).show();
50                    return;
51                }
52                Intent mIntent = new Intent(MainActivity.this, Main2Activity.class);
53                mIntent.putExtra("intent", et1.getText().toString().trim());
54                startActivityForResult(mIntent, 1000);
55            }
56 
57            @Override
58            protected void onActivityResult(int requestCode, int resultCold, Intent result) {
59                MainActivity.super.onActivityResult(requestCode, resultCold, result);
60                if (requestCode == REQUEST_CODE && resultCold == RESULT_CODE) {
61                    String result_value = result.getStringExtra("result");
62                    tv1.setText("评论内容返回为:" + result_value);
63                }
64            }
65       

第二个activity的实现传回数据的代码

 1 package com.example.zhoushasha.myapplication;
 2 
 3 import android.content.Intent;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.widget.Button;
 8 import android.widget.TextView;
 9 
10 public class Main2Activity extends AppCompatActivity {
11     private TextView tv1;
12     private Button bt1;
13     private Button bt2;
14 
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.activity_main2);
19         tv1= (TextView) findViewById(R.id.tv1);
20         String userName = tv1.getText().toString();
21         bt1 = (Button) findViewById(R.id.bt1);
22         bt1.setOnClickListener(new View.OnClickListener() {
23             @Override
24             public void onClick(View view) {
25                 String result = "";
26                 Intent intent = new Intent();
27                 intent.putExtra("result",result);
28                 setResult(MainActivity.RESULT_CODE,intent);
29                 finish();
30             }
31 
32         });
33         bt2= (Button) findViewById(R.id.bt2);
34         bt2.setOnClickListener(new View.OnClickListener() {
35             @Override
36             public void onClick(View view) {
37                 String result = "没有";
38                 Intent intent = new Intent();
39                 intent.putExtra("result", result);
40                 setResult(MainActivity.RESULT_CODE,intent);
41                 finish();
42             }
43         });
44     }
45 }

 

以上是关于多activity界面跳转并传递数据的主要内容,如果未能解决你的问题,请参考以下文章

Android中实现activity的页面跳转并传值

Android 完美解决bundle实现页面跳转并保留之前数据+传值

原生与Flutter页面跳转并传递数据

原生与Flutter页面跳转并传递数据

原生与Flutter页面跳转并传递数据

安卓小案例收集三