当前位置:   article > 正文

Android Studio 实战演练—小猴子摘桃_android上机实验猴子摘桃

android上机实验猴子摘桃

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:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity"
  8. android:orientation="vertical">
  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="@android: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:id="@+id/iv_monkey"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:src="@drawable/monkey" />
  27. <Button
  28. android:id="@+id/btn_peach"
  29. android:layout_width="80dp"
  30. android:layout_height="40dp"
  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="@android:color/black" />
  37. <ImageView
  38. android:id="@+id/iv_peach"
  39. android:layout_width="45dp"
  40. android:layout_height="35dp"
  41. android:layout_centerHorizontal="true"
  42. android:layout_marginTop="80dp"
  43. android:src="@drawable/peach_pic" />
  44. <TextView
  45. android:id="@+id/tv_count"
  46. android:layout_width="wrap_content"
  47. android:layout_height="wrap_content"
  48. android:layout_marginTop="85dp"
  49. android:layout_toRightOf="@id/iv_peach"
  50. android:text="摘到0个"
  51. android:textColor="@android:color/black"
  52. android:textSize="16sp" />
  53. </RelativeLayout>
  54. </LinearLayout>

activity_mian.xml运行界面

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

PeachActivity.xml运行界面

MainActivity.java文件

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

 

 PeachActivity.java文件

  1. package cn.itcast.pickpeach;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.KeyEvent;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.Toast;
  9. public class PeachActivity extends AppCompatActivity implements View.OnClickListener {
  10. private int count = 0;
  11. private Button btn_one,btn_two,btn_three,btn_four,btn_five,btn_six,btn_exit;
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_peach);
  16. init();
  17. }
  18. private void init() {
  19. //定义点击事件,然后获取统计个数
  20. btn_one = findViewById(R.id.btn_one);
  21. btn_two = findViewById(R.id.btn_two);
  22. btn_three = findViewById(R.id.btn_three);
  23. btn_four = findViewById(R.id.btn_four);
  24. btn_five = findViewById(R.id.btn_five);
  25. btn_six = findViewById(R.id.btn_six);
  26. btn_exit = findViewById(R.id.btn_exit);
  27. //点击事件
  28. btn_one.setOnClickListener(PeachActivity.this);
  29. btn_two.setOnClickListener(PeachActivity.this);
  30. btn_three.setOnClickListener(PeachActivity.this);
  31. btn_four.setOnClickListener(PeachActivity.this);
  32. btn_six.setOnClickListener(PeachActivity.this);
  33. btn_five.setOnClickListener(PeachActivity.this);
  34. btn_exit.setOnClickListener(PeachActivity.this);
  35. }
  36. private void info(Button btn){
  37. count++;
  38. btn.setVisibility(View.INVISIBLE);
  39. Toast.makeText(PeachActivity.this,"摘到"+"个桃子",Toast.LENGTH_LONG).show();
  40. }
  41. @Override
  42. public void onClick(View view) {
  43. switch (view.getId()){
  44. case R.id.btn_one:
  45. info(btn_one);
  46. break;
  47. case R.id.btn_two:
  48. info(btn_two);
  49. break;
  50. case R.id.btn_three:
  51. info(btn_three);
  52. break;
  53. case R.id.btn_four:
  54. info(btn_four);
  55. break;
  56. case R.id.btn_five:
  57. info(btn_five);
  58. break;
  59. case R.id.btn_six:
  60. info(btn_six);
  61. break;
  62. case R.id.btn_exit:
  63. info(btn_exit);
  64. break;
  65. }
  66. }
  67. private void re() {
  68. Intent intent = new Intent();
  69. intent.putExtra("count",count);
  70. setResult(1,intent);
  71. //关闭进程
  72. PeachActivity.this.finish();
  73. }
  74. @Override
  75. public boolean onKeyDown(int keyCode, KeyEvent event) {
  76. if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0);
  77. re();
  78. return false;
  79. }
  80. }

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

闽ICP备14008679号