经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android Intent实现页面跳转
来源:cnblogs  作者:carry莫奈  时间:2019/2/11 9:08:14  对本文有异议

什么是Intent

    Intent可以理解为信使(意图)

    由Intent来协作完成Android各个组件之间的通讯, 也可以说是实现页面与页面之间的跳转

Intent实现页面之间的跳转

  1. startActivity(intent)        //第一种方式启动
  2. startActivityForResult(intent, requestCode); //第二种启动方式

    onActivityResult(int requestCode, int resultCode, Intent data)

    setResult(resultCode, data);

第一种启动方式实现直接跳转,无返回值

第二种启动方式A页面->B页面, B页面也能回传到A页面数据

onActivityResult(int requestCode, int resultCode, Intent data)

这个是 用来A页面接收B页面回传的数据用的

setResult(resultCode, data) 这个是用来B页面回传给A页面数据

首先创建一个项目

新建两个Activity

 

然后再layout 里面新建两个页面布局 layout右键ànewàAndroid XML File

随后,进行绑定,例如:

然后,配置清单文件AndroidManifest.xml

然后,在activity_first 插入两个button 一个textview 分别用来第一个和第二个跳转方式, 以及数据回传,代码如下:

 

  1. 1 <?xml version="1.0" encoding="utf-8"?>
  2. 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. 3 android:layout_width="match_parent"
  4. 4 android:layout_height="match_parent"
  5. 5 android:orientation="vertical" >
  6. 6
  7. 7 <Button
  8. 8 android:id="@+id/bt_first"
  9. 9 android:layout_width="match_parent"
  10. 10 android:layout_height="wrap_content"
  11. 11 android:text="第一种启动方式" />
  12. 12
  13. 13 <Button
  14. 14 android:id="@+id/bt_Second"
  15. 15 android:layout_width="match_parent"
  16. 16 android:layout_height="wrap_content"
  17. 17 android:text="第二种启动方式" />
  18. 18
  19. 19 <TextView
  20. 20 android:id="@+id/textview"
  21. 21 android:layout_width="match_parent"
  22. 22 android:layout_height="wrap_content"
  23. 23 android:text="把第二个页面回传的数据显示" />
  24. 24
  25. 25 </LinearLayout>

刚才提到了,实现跳转需要用到Intent意图,代码很简单 清晰明了 我直接贴代码了

首先 FirstActivity

  1. 1 package com.example.intentdemo;
  2. 2
  3. 3 import android.app.Activity;
  4. 4 import android.content.Intent;
  5. 5 import android.os.Bundle;
  6. 6 import android.view.View;
  7. 7 import android.view.View.OnClickListener;
  8. 8 import android.widget.Button;
  9. 9 import android.widget.TextView;
  10. 10
  11. 11 public class FirstActivity extends Activity {
  12. 12 Button bt_first, bt_second;
  13. 13 TextView textView;
  14. 14
  15. 15 @Override
  16. 16 protected void onCreate(Bundle savedInstanceState) {
  17. 17 super.onCreate(savedInstanceState);
  18. 18 setContentView(R.layout.activity_first);
  19. 19
  20. 20 initView();
  21. 21 }
  22. 22
  23. 23 private void initView() {
  24. 24 // 关联控件
  25. 25 bt_first = (Button) findViewById(R.id.bt_first);
  26. 26 bt_second = (Button) findViewById(R.id.bt_Second);
  27. 27 textView = (TextView) findViewById(R.id.textview);
  28. 28 bt_first.setOnClickListener(new OnClickListener() {
  29. 29
  30. 30 @Override
  31. 31 public void onClick(View arg0) {
  32. 32 // 第一种跳转,无数据返回跳转
  33. 33 Intent intent = new Intent(FirstActivity.this,
  34. 34 SecondActivity.class);
  35. 35 intent.putExtra("content", "第一种跳转方式");
  36. 36 startActivity(intent);
  37. 37 }
  38. 38 });
  39. 39 bt_second.setOnClickListener(new OnClickListener() {
  40. 40
  41. 41 @Override
  42. 42 public void onClick(View arg0) {
  43. 43 // 第二种方式,有数据返回跳转
  44. 44 Intent intent = new Intent(FirstActivity.this,
  45. 45 SecondActivity.class);
  46. 46 intent.putExtra("content", "第二种跳转方式");
  47. 47
  48. 48 /*
  49. 49 * 第一个参数是intent对象 第二个参数的请求的一个标识
  50. 50 */
  51. 51 startActivityForResult(intent, 1);
  52. 52 }
  53. 53 });
  54. 54 }
  55. 55
  56. 56 /*
  57. 57 * 通过startActivityForresult跳转,接收返回数据的方法 requestCode:请求的标识
  58. 58 * resultCode:第二个页面返回的标识 data:第二个页面回传的数据
  59. 59 */
  60. 60
  61. 61 @Override
  62. 62 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  63. 63 super.onActivityResult(requestCode, resultCode, data);
  64. 64 if (requestCode == 1) {
  65. 65 if (resultCode == 1) {
  66. 66 String text = data.getStringExtra("content");
  67. 67 textView.setText(text);
  68. 68 }
  69. 69 }
  70. 70 }
  71. 71 }

SecondActivity代码如下:

  1. 1 package com.example.intentdemo;
  2. 2
  3. 3 import android.app.Activity;
  4. 4 import android.content.Intent;
  5. 5 import android.os.Bundle;
  6. 6 import android.view.View;
  7. 7 import android.view.View.OnClickListener;
  8. 8 import android.widget.Button;
  9. 9 import android.widget.TextView;
  10. 10
  11. 11 public class SecondActivity extends Activity {
  12. 12 TextView textView;
  13. 13 Button button;
  14. 14 @Override
  15. 15 protected void onCreate(Bundle savedInstanceState) {
  16. 16 super.onCreate(savedInstanceState);
  17. 17 setContentView(R.layout.activity_second);
  18. 18 //获取第一个页面的意图
  19. 19 Intent intent = getIntent();
  20. 20
  21. 21 button = (Button) findViewById(R.id.button);
  22. 22 textView = (TextView) findViewById(R.id.textview);
  23. 23 //接收第一个页面传送的数据
  24. 24 textView.setText(intent.getStringExtra("content"));
  25. 25 button.setOnClickListener(new OnClickListener() {
  26. 26
  27. 27 @Override
  28. 28 public void onClick(View arg0) {
  29. 29 setResult(1, new Intent().putExtra("content", "从第二个页面返回"));
  30. 30 //结束当前页面
  31. 31 finish();
  32. 32 }
  33. 33 });
  34. 34 }
  35. 35 }

 

 

 

这样就简单的实现了,

如果有不懂的可以在下面发,我看到会帮你解决,Android其他问题也可以

下面贴上Demo:https://pan.baidu.com/s/16HEQ1pVnpB995i3-lAG7qw

原文链接:http://www.cnblogs.com/MrChen-/p/10356866.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号