Activity概述

image.png

Activity的响应事件

image.png

Activity之间的数据传递

image.png

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:orientation="vertical"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context=".MainActivity">
  9. <TextView
  10. android:id="@+id/tv1"
  11. android:textSize="20sp"
  12. android:text="第一个activity"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content" />
  15. <Button
  16. android:id="@+id/btn1"
  17. android:text="启动第二个activity"
  18. android:onClick="btnClick1"
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content" />
  21. <Button
  22. android:id="@+id/btn2"
  23. android:text="启动第二个activity-传数据1"
  24. android:onClick="btnClick2"
  25. android:layout_width="match_parent"
  26. android:layout_height="wrap_content" />
  27. <Button
  28. android:id="@+id/btn3"
  29. android:text="启动第二个activity-传数据2"
  30. android:onClick="btnClick3"
  31. android:layout_width="match_parent"
  32. android:layout_height="wrap_content" />
  33. <Button
  34. android:id="@+id/btn4"
  35. android:text="启动第二个activity-传回数据1"
  36. android:onClick="btnClick4"
  37. android:layout_width="match_parent"
  38. android:layout_height="wrap_content" />
  39. </LinearLayout>

activity_main2.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:orientation="vertical"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context=".Main2Activity">
  9. <TextView
  10. android:id="@+id/tv1"
  11. android:textSize="20sp"
  12. android:text="第二个activity"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content" />
  15. <TextView
  16. android:id="@+id/tv2"
  17. android:textSize="20sp"
  18. android:text="传递的数据:"
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content" />
  21. <Button
  22. android:id="@+id/btn1"
  23. android:textSize="20sp"
  24. android:text="返回"
  25. android:onClick="btnRet"
  26. android:layout_width="match_parent"
  27. android:layout_height="wrap_content" />
  28. </LinearLayout>

MainActivity.java

  1. package com.bluelesson.activitydemo;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.util.Log;
  6. import android.view.KeyEvent;
  7. import android.view.MotionEvent;
  8. import android.view.View;
  9. import android.widget.Toast;
  10. public class MainActivity extends AppCompatActivity {
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15. }
  16. @Override
  17. public boolean onKeyUp(int keyCode, KeyEvent event) {
  18. Log.d("15pb","onKeyUp");
  19. return super.onKeyUp(keyCode, event);
  20. }
  21. @Override
  22. public boolean onKeyDown(int keyCode, KeyEvent event) {
  23. Log.d("15pb","onKeyDown");
  24. return super.onKeyDown(keyCode, event);
  25. }
  26. @Override
  27. public boolean onTouchEvent(MotionEvent event) {
  28. Log.d("15pb","onTouchEvent");
  29. return super.onTouchEvent(event);
  30. }
  31. public void btnClick1(View view) {
  32. // 1. 创建Intent对象
  33. Intent intent = new Intent();
  34. // 2. 设置信息
  35. intent.setClass(this,
  36. Main2Activity.class
  37. );
  38. // 3. 启动activity
  39. startActivity(intent);
  40. }
  41. public void btnClick2(View view) {
  42. // 1. 创建Intent对象
  43. Intent intent = new Intent();
  44. // 2. 设置信息
  45. // 2.1 设置类类型
  46. intent.setClass(this,
  47. Main2Activity.class
  48. );
  49. // 2.2 设置传递的数据
  50. intent.putExtra("name","hello");
  51. // 3. 启动activity
  52. startActivity(intent);
  53. }
  54. public void btnClick3(View view) {
  55. // 1. 创建Intent对象
  56. Intent intent = new Intent();
  57. // 2. 设置信息
  58. // 2.1 设置类类型
  59. intent.setClass(this,
  60. Main2Activity.class
  61. );
  62. // 2.2 设置传递的数据
  63. Bundle bundle = new Bundle();
  64. bundle.putString("name","hello15pb");
  65. intent.putExtras(bundle);
  66. // 3. 启动activity
  67. startActivity(intent);
  68. }
  69. public void btnClick4(View view) {
  70. // 1. 创建Intent对象
  71. Intent intent = new Intent();
  72. // 2. 设置信息
  73. intent.setClass(this,
  74. Main2Activity.class
  75. );
  76. // 3. 启动activity
  77. // 传入请求码
  78. startActivityForResult(intent,0x111);
  79. }
  80. @Override
  81. protected void onActivityResult(int requestCode,
  82. int resultCode,
  83. Intent data) {
  84. super.onActivityResult(requestCode, resultCode, data);
  85. if(resultCode == 0x111){
  86. String name = data.getStringExtra("name");
  87. Toast.makeText(this,
  88. "传回的数据:"+name,
  89. Toast.LENGTH_SHORT).show();
  90. }
  91. }
  92. }

Main2Activity.java

  1. package com.bluelesson.activitydemo;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.view.View;
  6. import android.widget.TextView;
  7. public class Main2Activity extends AppCompatActivity {
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main2);
  12. // 接收数据
  13. Intent intent = getIntent();
  14. String name = intent.getStringExtra("name");
  15. if(name == null){
  16. Bundle bundle = intent.getExtras();
  17. if(bundle != null){
  18. name = bundle.getString("name");
  19. }
  20. }
  21. // 显示数据
  22. TextView textView = findViewById(R.id.tv2);
  23. textView.setText("传递的数据:"+name);
  24. }
  25. public void btnRet(View view) {
  26. // 返回时,可以设置数据
  27. // setResult(0x111);
  28. Intent intent = new Intent();
  29. intent.putExtra("name","hi hello");
  30. setResult(0x111,intent);
  31. finish();//关闭activity
  32. }
  33. }

Activity的intent隐式启动

隐式启动

image.png

image.png

image.png

隐式启动设置data

image.png