当前位置:   article > 正文

Android Studio初学者实例:Activity之间数据传递--小猴子摘桃

activity之间数据传递--小猴子摘桃

此次学习的目的是学习Activity之间数据传递,包括了传递数据以及数据回传

主题是课堂上的案例小猴子摘桃:话不多说,看一下效果图:

后续代码讲解会在后续补充

 首先看一下第一个Activity的xml文件,也就是首页

我这里的文件名为activity_main48.xml,可以自行更改,用的文件并不多,activity两个

  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:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context=".MainActivity48">
  9. <TextView
  10. android:layout_width="match_parent"
  11. android:layout_height="50dp"
  12. android:background="#008577"
  13. android:gravity="center"
  14. android:text="首页"
  15. android:textColor="@color/white"
  16. android:textSize="20sp"/>
  17. <RelativeLayout
  18. android:layout_width="match_parent"
  19. android:layout_height="match_parent"
  20. android:background="@drawable/bg"
  21. android:gravity="center_vertical">
  22. <ImageView
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:id="@+id/iv_monkey"
  26. android:src="@drawable/monkey"/>
  27. <Button
  28. android:layout_width="80dp"
  29. android:layout_height="30dp"
  30. android:id="@+id/btn_peach"
  31. android:layout_marginLeft="30dp"
  32. android:layout_marginTop="20dp"
  33. android:layout_toRightOf="@+id/iv_monkey"
  34. android:background="@drawable/btn_peach"
  35. android:text="去桃园"
  36. android:textColor="@color/black"/>
  37. <ImageView
  38. android:layout_width="45dp"
  39. android:layout_height="35dp"
  40. android:layout_centerHorizontal="true"
  41. android:id="@+id/iv_peach"
  42. android:layout_marginTop="80dp"
  43. android:src="@drawable/peach_pic"/>
  44. <TextView
  45. android:layout_width="wrap_content"
  46. android:layout_height="wrap_content"
  47. android:id="@+id/tv_count"
  48. android:layout_marginTop="85dp"
  49. android:layout_toRightOf="@id/iv_peach"
  50. android:text="摘到0个"
  51. android:textColor="@color/black"
  52. android:textSize="16sp"/>
  53. </RelativeLayout>
  54. </LinearLayout>

 第二个界面代码:

activity_main47.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:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:theme="@style/Theme.AppCompat.NoActionBar"
  8. android:orientation="vertical"
  9. tools:context=".MainActivity47">
  10. <TextView
  11. android:layout_width="match_parent"
  12. android:layout_height="50dp"
  13. android:background="#008577"
  14. android:text="桃园"
  15. android:textColor="@color/white"
  16. android:textSize="20sp"
  17. android:gravity="center"/>
  18. <RelativeLayout
  19. android:layout_width="match_parent"
  20. android:layout_height="match_parent"
  21. android:background="@drawable/tree_bg">
  22. <RelativeLayout
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:layout_centerInParent="true"
  26. android:layout_marginTop="70dp"
  27. android:background="@drawable/tree">
  28. <Button
  29. android:layout_width="45dp"
  30. android:layout_height="35dp"
  31. android:layout_marginLeft="85dp"
  32. android:id="@+id/btn_one"
  33. android:layout_marginTop="25dp"
  34. android:background="@drawable/peach_pic"/>
  35. <Button
  36. android:layout_width="45dp"
  37. android:layout_height="35dp"
  38. android:id="@+id/btn_two"
  39. android:layout_below="@+id/btn_one"
  40. android:layout_marginLeft="50dp"
  41. android:layout_marginTop="5dp"
  42. android:background="@drawable/peach_pic"/>
  43. <Button
  44. android:layout_width="45dp"
  45. android:layout_height="35dp"
  46. android:id="@+id/btn_three"
  47. android:layout_below="@id/btn_one"
  48. android:layout_marginLeft="25dp"
  49. android:layout_marginTop="5dp"
  50. android:layout_toRightOf="@id/btn_two"
  51. android:background="@drawable/peach_pic"/>
  52. <Button
  53. android:layout_width="45dp"
  54. android:layout_height="35dp"
  55. android:id="@+id/btn_four"
  56. android:layout_below="@id/btn_two"
  57. android:layout_marginLeft="25dp"
  58. android:layout_marginTop="5dp"
  59. android:background="@drawable/peach_pic"/>
  60. <Button
  61. android:layout_width="45dp"
  62. android:layout_height="35dp"
  63. android:id="@+id/btn_five"
  64. android:layout_below="@id/btn_two"
  65. android:layout_marginLeft="25dp"
  66. android:layout_marginTop="5dp"
  67. android:layout_toRightOf="@id/btn_four"
  68. android:background="@drawable/peach_pic"/>
  69. <Button
  70. android:layout_width="45dp"
  71. android:layout_height="35dp"
  72. android:id="@+id/btn_six"
  73. android:layout_below="@id/btn_two"
  74. android:layout_marginLeft="25dp"
  75. android:layout_marginTop="5dp"
  76. android:layout_toRightOf="@id/btn_five"
  77. android:background="@drawable/peach_pic"/>
  78. </RelativeLayout>
  79. <Button
  80. android:layout_width="80dp"
  81. android:layout_height="30dp"
  82. android:layout_alignParentRight="true"
  83. android:layout_alignParentBottom="true"
  84. android:layout_margin="50dp"
  85. android:background="@drawable/btn_peach"
  86. android:text="退出桃园"
  87. android:textColor="@color/black"
  88. android:id="@+id/btn_exit"/>
  89. </RelativeLayout>
  90. </LinearLayout>

 MainActivity48:对应第一个xml文件的逻辑代码(可以自行命名)

  1. import androidx.annotation.Nullable;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.TextView;
  8. public class MainActivity48 extends AppCompatActivity {
  9. private Button btn_peach;
  10. private TextView tv_count;
  11. private int totalCount=0;
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main48);
  16. init();
  17. }
  18. public void init(){
  19. btn_peach=findViewById(R.id.btn_peach);
  20. tv_count=findViewById(R.id.tv_count);
  21. btn_peach.setOnClickListener(new View.OnClickListener() {
  22. @Override
  23. public void onClick(View v) {
  24. Intent intent=new Intent(MainActivity48.this,MainActivity47.class);
  25. startActivityForResult(intent,1);
  26. }
  27. });
  28. }
  29. @Override
  30. protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
  31. super.onActivityResult(requestCode, resultCode, data);
  32. if(requestCode==1&&requestCode==1){
  33. int count=data.getIntExtra("count",0);
  34. totalCount=totalCount+count;
  35. tv_count.setText("摘到"+totalCount+"个");
  36. }
  37. }
  38. }

 MainActivity47:

  1. import androidx.appcompat.app.AppCompatActivity;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.view.KeyEvent;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.Toast;
  8. public class MainActivity47 extends AppCompatActivity implements View.OnClickListener {
  9. private Button btn_one,btn_two,btn_three,btn_four,btn_five,btn_six,btn_exit;
  10. private int count=0;
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main47);
  15. init();
  16. }
  17. private void init(){
  18. btn_one=findViewById(R.id.btn_one);
  19. btn_two=findViewById(R.id.btn_two);
  20. btn_three=findViewById(R.id.btn_three);
  21. btn_four=findViewById(R.id.btn_four);
  22. btn_five=findViewById(R.id.btn_five);
  23. btn_six=findViewById(R.id.btn_six);
  24. btn_exit=findViewById(R.id.btn_exit);
  25. btn_one.setOnClickListener(this);
  26. btn_two.setOnClickListener(this);
  27. btn_three.setOnClickListener(this);
  28. btn_four.setOnClickListener(this);
  29. btn_five.setOnClickListener(this);
  30. btn_six.setOnClickListener(this);
  31. btn_exit.setOnClickListener(this);
  32. }
  33. @Override
  34. public void onClick(View v) {
  35. switch (v.getId()){
  36. case R.id.btn_one:info(btn_one);break;
  37. case R.id.btn_two:info(btn_two);break;
  38. case R.id.btn_three:info(btn_three);break;
  39. case R.id.btn_four:info(btn_four);break;
  40. case R.id.btn_five:info(btn_five);break;
  41. case R.id.btn_six:info(btn_six);break;
  42. case R.id.btn_exit:returnData();break;
  43. }
  44. }
  45. private void info(Button button){
  46. count++;
  47. button.setVisibility(View.INVISIBLE);
  48. Toast.makeText(MainActivity47.this,"摘到了"+count+"个桃子",Toast.LENGTH_SHORT).show();
  49. }
  50. private void returnData(){
  51. Intent intent=new Intent();
  52. intent.putExtra("count",count);
  53. setResult(1,intent);
  54. MainActivity47.this.finish();
  55. }
  56. @Override
  57. public boolean onKeyDown(int keyCode, KeyEvent event) {
  58. if(keyCode==KeyEvent.KEYCODE_BACK&&event.getRepeatCount()==0){
  59. returnData();
  60. }
  61. return false;
  62. }
  63. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/502267
推荐阅读
相关标签
  

闽ICP备14008679号