使用sharedpreference是对信息的存储,也可以进行传值,今天通过查找资料,学习了Activity的跳转和传值方法。
跳转
1、显示跳转
4种方法
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
|
public class AActivity extends AppCompatActivity { private Button ma; @Override protected void onCreate( @Nullable Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_a); ma=findViewById(R.id.btn_a); ma.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { //显式方法1 Intent intent= new Intent(AActivity. this ,BActivity. class ); startActivity(intent); /*显式跳转方法2 Intent intent=new Intent(); intent.setClass(AActivity.this,BActivity.class); startActivity(intent); */ /*显式跳转方法3 Intent intent=new Intent(); intent.setClassName(AActivity.this,"com.example.textview.jump.BActivity"); startActivity(intent); */ /*显式跳转方法4 Intent intent=new Intent(); intent.setComponent(new ComponentName(AActivity.this,"com.example.textview.jump.BActivity")); startActivity(intent); */ } }); } } |
数据传递
AActivity点击跳转后发送数据
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
|
package com.example.textview.jump; import android.content.ComponentName; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import com.example.textview.R; public class AActivity extends AppCompatActivity { private Button ma; @Override protected void onCreate( @Nullable Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_a); ma=findViewById(R.id.btn_a); ma.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { //显式方法1 Intent intent= new Intent(AActivity. this ,BActivity. class ); //数值传递 Bundle bundle= new Bundle(); bundle.putString( "name" , "刘亦菲" ); //bundle里放传输的数据 bundle.putInt( "number" , 28 ); intent.putExtras(bundle); //bundle通过intent传过去 startActivity(intent); } }); } } |
BActivity接收数据
B的布局界面放一个TextView用来显式传输来的文件
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:layout_width= "match_parent" android:padding= "10dp" android:layout_height= "match_parent" > <TextView android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:textSize= "25dp" android:textColor= "#F08080" android:id= "@+id/text_1" /> </LinearLayout> |
BActivity.java
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
|
package com.example.textview.jump; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import com.example.textview.R; public class BActivity extends AppCompatActivity { private TextView mTV; @Override protected void onCreate( @Nullable Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_b); mTV=findViewById(R.id.text_1); Bundle bundle=getIntent().getExtras(); String name=bundle.getString( "name" ); int number=bundle.getInt( "number" ); mTV.setText(name+ "," +number); } } |
startActivityForResul
界面1跳转到界面2,界面2将得到的数据又转到界面1
AActivity.java
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
|
package com.example.textview.jump; import android.content.ComponentName; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import com.example.textview.R; public class AActivity extends AppCompatActivity { private Button ma; @Override protected void onCreate( @Nullable Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_a); ma=findViewById(R.id.btn_a); ma.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { //显式方法1 Intent intent= new Intent(AActivity. this ,BActivity. class ); //数值传递 Bundle bundle= new Bundle(); bundle.putString( "name" , "刘亦菲" ); //bundle里放传输的数据 bundle.putInt( "number" , 28 ); intent.putExtras(bundle); //bundle通过intent传过去 // startActivity(intent); startActivityForResult(intent, 0 ); } }); } @Override protected void onActivityResult( int requestCode, int resultCode, @Nullable Intent data) { super .onActivityResult(requestCode, resultCode, data); Toast.makeText( this , data.getExtras().getString( "title" ), Toast.LENGTH_SHORT).show(); } } |
BActivity.java
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
|
package com.example.textview.jump; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import com.example.textview.R; public class BActivity extends AppCompatActivity { private TextView mTV; private Button mab; @Override protected void onCreate( @Nullable Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_b); mTV=findViewById(R.id.text_1); mab=findViewById(R.id.ab_1); Bundle bundle=getIntent().getExtras(); String name=bundle.getString( "name" ); int number=bundle.getInt( "number" ); mTV.setText(name+ "," +number); mab.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Intent intent= new Intent(); Bundle bundle1= new Bundle(); bundle1.putString( "title" , "宝宝回啦了" ); intent.putExtras(bundle1); setResult(Activity.RESULT_OK,intent); finish(); } }); } } |
【推广】
免费学中医,健康全家人
原文地址:https://www.cnblogs.com/1305536110-dym/p/14907963.html
- 推荐文章
- 插件上传1
- Thinkphp拖拽上传文件-使用webuploader插件(自己改动了一些地方)——分片上传
- PHP大文件分割上传(分片上传)
- 数据库数据字典生成代码
- Linux下SVN安装配置以及使用
- 哈希(Hash)与加密(Encrypt)的基本原理、区别及工程应用
- 如何在开启了log-bin的MySQL Server中创建FUNCTION
- This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its de 错误解决办法
- rsync 端口更换(默认873)
- MySQL双主(主主)架构方案
- Dockerfile创建MySQL容器
- 如何使用yum来下载RPM包而不进行安装
- centos 6 rsync+inotify 实时同步
- MYSQL主从复制配置遇到的问题
- MySQL——修改root密码的4种方法(以windows为例)
- mysql5.5主从同步复制配置
- Windows 7 Apache下计算机无法访问局域网网站的问题
- php 面试
- Apache的配置
- jquery easy ui 学习 (9)Pagination in TreeGrid 分页
- jquery easy ui 学习 (8)basic treegrid
- jquery easy ui 学习 (7) TreeGrid Actions
- jquery easy ui 学习 (6) basic validatebox
- jquery easy ui 学习 (5) windowlayout
- jquery easy ui 学习 (4) window 打开之后 限制操纵后面元素属性
- 提示“应用程序无法启动,因为应用程序的并行配置不正确”不能加载 System.Data.SQLite.dll
- visual studio 添加虚线的快捷键
- VS2010打开项目时,出现“已经在解决方案中打开了具有该名称的项目”问题的解决方案
- visual studio 编译时 出现 Files 的值 乱码
- 微信 连接被意外关闭