1. 目标
实现多个Activity集成应用,包含简单的页面转跳、参数传递、返回值传递,掌握Activity的创建、注册、启动、和相关回调方法的重写,Intent的实例化,参数设置和参数传递,Activity中参数回传的机制。

2. 要点实现
2.1 创建主要页面
通过对页面进行拆分,我们可以将页面分成线性布局,这样可以用极大的避免元件之间出现位置错误:
可以将其拆分为一个垂直布局和两个水平布局,就和html中的div盒子一样,将元件摆放在其中,其中对于线性布局,我们使用:
1 2 3
| <LinearLayout> //内部组件编写在这里 </LinearLayout>
|
将这个组件可以看作一个div盒子,对其设置相应的宽高,和布局方向:
1 2 3 4 5 6 7 8
| //宽度撑满父部容器 android:layout_width="match_parent" //高度跟随子容器需要的高度变化 android:layout_height="wrap_content" //设置布局为垂直布局 android:orientation="vertical" //设置布局为水平布局 android:orientation="horizontal"
|
在其中填充相应的文字,文本框,按钮等元件:
1 2 3 4 5 6
| //文字 <TextView/> //输入框 <EditText/> //按钮 <Button/>
|
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity">
<TextView android:layout_width="match_parent" android:layout_height="150dp" android:background="@color/Cyan1" android:gravity="center" android:text="这是首页" android:textColor="@color/black" android:textSize="50sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
<Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="70dp" android:layout_marginLeft="200px" android:layout_marginTop="30px" android:layout_marginRight="200px" android:textSize="20sp" android:text="无参启动"> </Button> //新建一个水平布局容器存放书名 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="书名:" android:textSize="40sp"> </TextView>
<EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginRight="50px"> </EditText> </LinearLayout> //新建一个水平布局容器存放价格 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="价格:" android:textSize="40sp">
</TextView>
<EditText android:id="@+id/editText2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginEnd="50px">
</EditText> </LinearLayout> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="70dp" android:layout_marginLeft="200px" android:layout_marginTop="30px" android:layout_marginRight="200px" android:textSize="20sp" android:text="传参启动"> </Button> <Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="70dp" android:layout_marginLeft="200px" android:layout_marginTop="30px" android:layout_marginRight="200px" android:textSize="20sp" android:text="接受返回值"> </Button> <TextView android:id="@+id/textView4" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="40sp" android:gravity="center" > </TextView> </LinearLayout>
|
效果图:

2.2 实现页面转跳
要想实现点击功能,首先创建转跳后的页面,这里按照图例中创建相应的Activity页面

创建出一个空的NewActivity,同时打开java文件夹下的MainActivity文件重现Button的按钮的点击方法来实现转跳功能:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| public class MainActivity extends AppCompatActivity { Button button1; Button button2,button3; EditText editText1,editText2; TextView textView4; final int REQUEST_CODE=1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_main); button1=(Button)this.findViewById(R.id.button1); button2=(Button)this.findViewById(R.id.button2); button3=(Button)this.findViewById(R.id.button3); editText1 = (EditText)this.findViewById(R.id.editText1); editText2 = (EditText)this.findViewById(R.id.editText2); textView4 = (TextView)this.findViewById(R.id.textView4); ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); return insets; }); button1.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View arg0) { Intent intent = new Intent(MainActivity.this, NewActivity.class); startActivity(intent); } }); } }
|
这样就是实现了在在页面之间的转跳。
2.3 页面布局
根据上面给出的线性布局分析方法可以很简单的得出,后面三项的布局文件都可以快速的搭建出来:
activity_new.xml :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity">
<TextView android:layout_width="match_parent" android:layout_height="150dp" android:background="@color/Cyan1" android:gravity="center" android:text="无参页面" android:textColor="@color/black" android:textSize="50sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="70dp" android:layout_marginLeft="200px" android:layout_marginTop="200dp" android:layout_marginRight="200px" android:text="返回" android:textSize="20sp" > </Button>
</LinearLayout>
|
activity_params.xml :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity">
<TextView android:layout_width="match_parent" android:layout_height="150dp" android:background="@color/Cyan1" android:gravity="center" android:text="有参页面" android:textColor="@color/black" android:textSize="50sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="200px" > <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="书名:" android:textSize="40sp"> </TextView>
<EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginRight="50px"> </EditText> </LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="价格:" android:textSize="40sp">
</TextView>
<EditText android:id="@+id/editText2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginEnd="50px">
</EditText> </LinearLayout>
<Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="70dp" android:layout_marginLeft="200px" android:layout_marginTop="200dp" android:layout_marginRight="200px" android:text="返回" android:textSize="20sp" > </Button>
</LinearLayout>
|
activity_return_params.xml :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity">
<TextView
android:layout_width="match_parent" android:layout_height="150dp" android:background="@color/Cyan1" android:gravity="center" android:text="有返回参页面" android:textColor="@color/black" android:textSize="50sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="200px" android:orientation="vertical">
<TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="需要返回的参数:" android:textSize="40sp"></TextView>
<EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginRight="50px"></EditText> </LinearLayout>
<Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="50dp" android:layout_marginTop="50sp" android:layout_marginRight="50dp" android:text="返回参数" android:textSize="40sp"></Button> </LinearLayout>
|
2.4 参数传递
想要将主页面中的代码传递给子页面,我们就是需要对按钮点击事件进行重写,即在点击按钮的同时对页面输入框中的所有文本内容进行读取,然后传递给子页面,这里我们重写第二个按钮的点击方法:
1 2 3 4 5 6 7 8 9 10 11 12
| button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String bookName = editText1.getText().toString(); String bookPrice = editText2.getText().toString(); Intent intent = new Intent(MainActivity.this, ParamsActivity.class); intent.putExtra("bookName", bookName); intent.putExtra("bookPrice", bookPrice); startActivity(intent); } });
|
通过对代码进行分析我们得知,在点击按钮的同时,将我们所需要传递的值类似于map<key,value>的键值对的形式添加到意图(intent)中,由它一并传递给新的页面,同样的,当我们需要在新的页面接收他时,也是通过key去寻找对应的value值的,但是我们需要在页面创建的时候就需要获取他的值了,所以我们需要在ParamsActivity的onCreate方法中添加:
1 2 3 4 5 6
| Intent intent = this.getIntent(); Bundle bundle = intent.getExtras(); String name = bundle.getString("bookName"); String price = bundle.getString("bookPrice"); editText1.setText(name); editText2.setText(price);
|
这里Bundle在Android开发中非常常见,它的作用主要时用于传递数据。Bundle传递的数据包括:string、int、boolean、byte、float、long、double等基本类型或它们对应的数组,Bundle所保存的数据是以key-value(键值对)的形式保存在ArrayMap中
2.5 接收返回值
在实际的开发中,例如我们可能需要知道用户操作等等是否成功,这样我们就需要在页面中接收刚才页面传回的返回值了,和参数传递非常的类似,不过我们需要告知意图我们是传回成功了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(); String msg = editText1.getText().toString(); if (!msg.isEmpty()) { msg = ("返回值为:" + msg); } else { msg = ("返回值未输入"); } intent.putExtra("msg", msg); ReturnParamsActivity.this.setResult(ReturnParamsActivity.this.RESULT_OK, intent); finish(); } });
|
这样只是单单的完成了对数据传回,如何对数据进行接收呢,页面又怎么知道是不是有返回值呢:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == REQUEST_CODE){ if(resultCode == RESULT_OK){ Bundle bundle = data.getExtras(); String msg = bundle.getString("msg"); textView4.setText(msg); } else if(resultCode == this.RESULT_CANCELED){ textView4.setText("无效返回"); } } }
|
这里就需要我们重写AppCompatActivity中的onActivityResult方法,将其中的请求代码作比较,确认是我们需要的页面传回的参数,当存在多个返回页面值的时候,我们就可以在REQUEST_CODE中定义不同的值来对他进行区分。
3. 完整项目
3.1 java层
3.1.1 MainActivity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| package com.example.activitydemo;
import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.TextView; import android.widget.Button; import android.widget.EditText; import androidx.activity.EdgeToEdge; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity { Button button1; Button button2,button3; EditText editText1,editText2; TextView textView4; final int REQUEST_CODE=1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_main); button1=(Button)this.findViewById(R.id.button1); button2=(Button)this.findViewById(R.id.button2); button3=(Button)this.findViewById(R.id.button3); editText1 = (EditText)this.findViewById(R.id.editText1); editText2 = (EditText)this.findViewById(R.id.editText2); textView4 = (TextView)this.findViewById(R.id.textView4); ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); return insets; }); button1.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View arg0) { Intent intent = new Intent(MainActivity.this, NewActivity.class); startActivity(intent); } }); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String bookName = editText1.getText().toString(); String bookPrice = editText2.getText().toString(); Intent intent = new Intent(MainActivity.this, ParamsActivity.class); intent.putExtra("bookName", bookName); intent.putExtra("bookPrice", bookPrice); startActivity(intent); } }); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this, ReturnParamsActivity.class); startActivityForResult(intent,REQUEST_CODE); } }); }
@Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == REQUEST_CODE){ if(resultCode == RESULT_OK){ Bundle bundle = data.getExtras(); String msg = bundle.getString("msg"); textView4.setText(msg); }else if(resultCode==this.RESULT_CANCELED){ textView4.setText("无效返回"); } } } }
|
3.1.2 NewActivity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| package com.example.activitydemo;
import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button;
import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat;
public class NewActivity extends AppCompatActivity { Button button1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_new); button1 = (Button)this.findViewById(R.id.button1); ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); return insets; }); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(NewActivity.this,MainActivity.class); startActivity(intent); } }); } }
|
3.1.3 ParamsActivity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| package com.example.activitydemo;
import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText;
import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat;
public class ParamsActivity extends AppCompatActivity { Button button1; EditText editText1, editText2;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_params); button1 = (Button) this.findViewById(R.id.button1); editText1 = (EditText) this.findViewById(R.id.editText1); editText2 = (EditText) this.findViewById(R.id.editText2); Intent intent = this.getIntent(); Bundle bundle = intent.getExtras(); String name = bundle.getString("bookName"); String price = bundle.getString("bookPrice"); editText1.setText(name); editText2.setText(price); ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); return insets; }); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent1 = new Intent(ParamsActivity.this,MainActivity.class); startActivity(intent1); } }); } }
|
3.1.4 ReturnParamsActivity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| package com.example.activitydemo;
import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText;
import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat;
public class ReturnParamsActivity extends AppCompatActivity { final int RESULT_CODE = 1; Button button1; EditText editText1;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_return_params); button1 = (Button) this.findViewById(R.id.button1); editText1 = (EditText) this.findViewById(R.id.editText1); ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); return insets; }); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(); String msg = editText1.getText().toString(); if (!msg.isEmpty()) { msg = ("返回值为:" + msg); } else { msg = ("返回值未输入"); } intent.putExtra("msg", msg); ReturnParamsActivity.this.setResult(ReturnParamsActivity.this.RESULT_OK, intent); finish(); } }); } }
|
3.2 layout层
3.2.1 activity_main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity">
<TextView android:layout_width="match_parent" android:layout_height="150dp" android:background="@color/Cyan1" android:gravity="center" android:text="这是首页" android:textColor="@color/black" android:textSize="50sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
<Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="70dp" android:layout_marginLeft="200px" android:layout_marginTop="30px" android:layout_marginRight="200px" android:textSize="20sp" android:text="无参启动"> </Button>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="书名:" android:textSize="40sp"> </TextView>
<EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginRight="50px"> </EditText> </LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="价格:" android:textSize="40sp">
</TextView>
<EditText android:id="@+id/editText2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginEnd="50px">
</EditText> </LinearLayout> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="70dp" android:layout_marginLeft="200px" android:layout_marginTop="30px" android:layout_marginRight="200px" android:textSize="20sp" android:text="传参启动"> </Button> <Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="70dp" android:layout_marginLeft="200px" android:layout_marginTop="30px" android:layout_marginRight="200px" android:textSize="20sp" android:text="接受返回值"> </Button> <TextView android:id="@+id/textView4" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="40sp" android:gravity="center" > </TextView> </LinearLayout>
|
3.2.2 activity_new
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity">
<TextView android:layout_width="match_parent" android:layout_height="150dp" android:background="@color/Cyan1" android:gravity="center" android:text="无参页面" android:textColor="@color/black" android:textSize="50sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="70dp" android:layout_marginLeft="200px" android:layout_marginTop="200dp" android:layout_marginRight="200px" android:text="返回" android:textSize="20sp" > </Button>
</LinearLayout>
|
3.2.3 activity_params
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity">
<TextView android:layout_width="match_parent" android:layout_height="150dp" android:background="@color/Cyan1" android:gravity="center" android:text="有参页面" android:textColor="@color/black" android:textSize="50sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="200px" > <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="书名:" android:textSize="40sp"> </TextView>
<EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginRight="50px"> </EditText> </LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="价格:" android:textSize="40sp">
</TextView>
<EditText android:id="@+id/editText2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginEnd="50px">
</EditText> </LinearLayout>
<Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="70dp" android:layout_marginLeft="200px" android:layout_marginTop="200dp" android:layout_marginRight="200px" android:text="返回" android:textSize="20sp" > </Button>
</LinearLayout>
|
3.2.4 activity_return_params
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity">
<TextView
android:layout_width="match_parent" android:layout_height="150dp" android:background="@color/Cyan1" android:gravity="center" android:text="有返回参页面" android:textColor="@color/black" android:textSize="50sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="200px" android:orientation="vertical">
<TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="需要返回的参数:" android:textSize="40sp"></TextView>
<EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginRight="50px"></EditText> </LinearLayout>
<Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="50dp" android:layout_marginTop="50sp" android:layout_marginRight="50dp" android:text="返回参数" android:textSize="40sp"></Button> </LinearLayout>
|
4. 归档
4.1 页面转跳
1 2 3 4 5 6 7 8 9 10
| button1.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View arg0) { Intent intent = new Intent(MainActivity.this, NewActivity.class); startActivity(intent); } });
|
4.2 参数传递
1 2 3 4 5 6 7 8 9 10 11 12
| button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String bookName = editText1.getText().toString(); String bookPrice = editText2.getText().toString(); Intent intent = new Intent(MainActivity.this, ParamsActivity.class); intent.putExtra("bookName", bookName); intent.putExtra("bookPrice", bookPrice); startActivity(intent); } });
|
4.3 接收返回值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(); String msg = editText1.getText().toString(); if (!msg.isEmpty()) { msg = ("返回值为:" + msg); } else { msg = ("返回值未输入"); } intent.putExtra("msg", msg); ReturnParamsActivity.this.setResult(ReturnParamsActivity.this.RESULT_OK, intent); finish(); } });
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == REQUEST_CODE){ if(resultCode == RESULT_OK){ Bundle bundle = data.getExtras(); String msg = bundle.getString("msg"); textView4.setText(msg); } else if(resultCode == this.RESULT_CANCELED){ textView4.setText("无效返回"); } } }
|