当前位置:   article > 正文

【Android Studio】实现简易猴子摘桃功能_android小猴子摘桃

android小猴子摘桃

\app\src\main\AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.example.peach">
  4. <application
  5. android:allowBackup="true"
  6. android:icon="@mipmap/ic_launcher"
  7. android:label="@string/app_name"
  8. android:roundIcon="@mipmap/ic_launcher_round"
  9. android:supportsRtl="true"
  10. android:theme="@style/Theme.Peach">
  11. <activity
  12. android:name=".MainActivity"
  13. android:exported="true">
  14. <intent-filter>
  15. <action android:name="android.intent.action.MAIN" />
  16. <category android:name="android.intent.category.LAUNCHER" />
  17. </intent-filter>
  18. </activity>
  19. <activity android:name=".PeachActivity"/>
  20. </application>
  21. </manifest>

\app\src\main\java\com\example\peach\MainActivity.java

  1. package com.example.peach;
  2. import androidx.activity.result.ActivityResult;
  3. import androidx.activity.result.ActivityResultCallback;
  4. import androidx.activity.result.ActivityResultLauncher;
  5. import androidx.activity.result.contract.ActivityResultContracts;
  6. import androidx.appcompat.app.AppCompatActivity;
  7. import android.content.Intent;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.TextView;
  13. public class MainActivity extends AppCompatActivity {
  14. private Button pickBtn;
  15. private TextView peachTotal;
  16. int count=0;
  17. private ActivityResultLauncher launcher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
  18. @Override
  19. public void onActivityResult(ActivityResult result) {
  20. if(result != null){
  21. if(result.getResultCode() == RESULT_OK){
  22. Intent data = result.getData();
  23. int peachNum = data.getIntExtra("peachNum", 0);
  24. Log.i("MainActivity", "onActivityResult: "+ peachNum);
  25. count = count + peachNum;
  26. peachTotal.setText("桃子" + count + "个");
  27. }
  28. }
  29. }
  30. });
  31. @Override
  32. protected void onCreate(Bundle savedInstanceState) {
  33. super.onCreate(savedInstanceState);
  34. setContentView(R.layout.activity_main);
  35. pickBtn = findViewById(R.id.pick_btn);
  36. peachTotal = findViewById(R.id.peach_total);
  37. pickBtn.setOnClickListener(new View.OnClickListener() {
  38. @Override
  39. public void onClick(View view) {
  40. Intent i = new Intent(MainActivity.this, PeachActivity.class);
  41. launcher.launch(i);
  42. }
  43. });
  44. }
  45. }

\app\src\main\java\com\example\peach\PeachActivity.java

  1. package com.example.peach;
  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.ImageView;
  8. public class PeachActivity extends AppCompatActivity implements View.OnClickListener {
  9. private ImageView peach1,peach2,peach3,peach4,peach5,peach6;
  10. private Button exitBtn;
  11. private int num = 0;
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_peach);
  16. peach1 = findViewById(R.id.peach1);
  17. peach2 = findViewById(R.id.peach2);
  18. peach3 = findViewById(R.id.peach3);
  19. peach4 = findViewById(R.id.peach4);
  20. peach5 = findViewById(R.id.peach5);
  21. peach6 = findViewById(R.id.peach6);
  22. exitBtn = findViewById(R.id.exit_btn);
  23. peach1.setOnClickListener(this);
  24. peach2.setOnClickListener(this);
  25. peach3.setOnClickListener(this);
  26. peach4.setOnClickListener(this);
  27. peach5.setOnClickListener(this);
  28. peach6.setOnClickListener(this);
  29. exitBtn.setOnClickListener(this);
  30. }
  31. @Override
  32. public void onClick(View view) {
  33. if(view.getId() == R.id.peach1){
  34. info(peach1);
  35. }else if(view.getId() == R.id.peach2){
  36. info(peach2);
  37. }else if(view.getId() == R.id.peach3){
  38. info(peach3);
  39. }else if(view.getId() == R.id.peach4){
  40. info(peach4);
  41. }else if(view.getId() == R.id.peach5){
  42. info(peach5);
  43. }else if(view.getId() == R.id.peach6){
  44. info(peach6);
  45. }else if(view.getId() == R.id.exit_btn){
  46. returnData();
  47. }
  48. }
  49. private void returnData() {
  50. Intent i = new Intent();
  51. i.putExtra("peachNum", num);
  52. setResult(RESULT_OK,i);
  53. finish();
  54. }
  55. private void info(ImageView imageView) {
  56. imageView.setVisibility(View.INVISIBLE);
  57. num++;
  58. }
  59. @Override
  60. public void onBackPressed() {
  61. Intent i = new Intent();
  62. i.putExtra("peachNum", num);
  63. setResult(RESULT_OK,i);
  64. finish();
  65. }
  66. }

\app\src\main\res\values\themes.xml

  1. <resources xmlns:tools="http://schemas.android.com/tools">
  2. <!-- Base application theme. -->
  3. <style name="Theme.Peach" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">
  4. <!-- Primary brand color. -->
  5. <item name="colorPrimary">@color/purple_500</item>
  6. <item name="colorPrimaryVariant">@color/purple_700</item>
  7. <item name="colorOnPrimary">@color/white</item>
  8. <!-- Secondary brand color. -->
  9. <item name="colorSecondary">@color/teal_200</item>
  10. <item name="colorSecondaryVariant">@color/teal_700</item>
  11. <item name="colorOnSecondary">@color/black</item>
  12. <!-- Status bar color. -->
  13. <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
  14. <!-- Customize your theme here. -->
  15. </style>
  16. </resources>

\app\src\main\res\layout\activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout 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:background="@drawable/bg">
  9. <TextView
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:background="@color/teal_700"
  13. android:gravity="center"
  14. android:padding="5dp"
  15. android:text="首页"
  16. android:textColor="#fff"
  17. android:textSize="25sp" />
  18. <ImageView
  19. android:id="@+id/monkey"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:src="@drawable/monkey"
  23. android:layout_centerVertical="true"/>
  24. <Button
  25. android:id="@+id/pick_btn"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:layout_alignTop="@+id/monkey"
  29. android:layout_toEndOf="@+id/monkey"
  30. android:background="@drawable/btn_peach"
  31. android:text="去桃园"
  32. android:textSize="22sp" />
  33. <ImageView
  34. android:id="@+id/peach"
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:layout_below="@+id/pick_btn"
  38. android:layout_alignLeft="@+id/pick_btn"
  39. android:layout_marginTop="50dp"
  40. android:src="@drawable/peach_pic" />
  41. <TextView
  42. android:id="@+id/peach_total"
  43. android:layout_width="wrap_content"
  44. android:layout_height="wrap_content"
  45. android:layout_alignBottom="@+id/peach"
  46. android:layout_toEndOf="@+id/peach"
  47. android:text="摘到0个"
  48. android:textSize="22sp" />
  49. </RelativeLayout>

\app\src\main\res\layout\activity_peach.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout 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:background="@drawable/tree_bg">
  9. <TextView
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:background="@color/teal_700"
  13. android:gravity="center"
  14. android:padding="5dp"
  15. android:text="桃园"
  16. android:textColor="#fff"
  17. android:textSize="25sp" />
  18. <ImageView
  19. android:id="@+id/tree"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_centerInParent="true"
  23. android:src="@drawable/tree" />
  24. <ImageView
  25. android:id="@+id/peach1"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:layout_alignTop="@+id/tree"
  29. android:layout_centerHorizontal="true"
  30. android:layout_marginTop="30dp"
  31. android:src="@drawable/peach_pic" />
  32. <ImageView
  33. android:id="@+id/peach2"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:layout_below="@+id/peach1"
  37. android:layout_alignLeft="@+id/tree"
  38. android:layout_marginLeft="50dp"
  39. android:src="@drawable/peach_pic" />
  40. <ImageView
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. android:src="@drawable/peach_pic"
  44. android:id="@+id/peach3"
  45. android:layout_below="@+id/peach1"
  46. android:layout_alignRight="@+id/tree"
  47. android:layout_marginRight="50dp"/>
  48. <ImageView
  49. android:id="@+id/peach4"
  50. android:layout_width="wrap_content"
  51. android:layout_height="wrap_content"
  52. android:layout_below="@+id/peach2"
  53. android:layout_centerHorizontal="true"
  54. android:src="@drawable/peach_pic" />
  55. <ImageView
  56. android:layout_width="wrap_content"
  57. android:layout_height="wrap_content"
  58. android:src="@drawable/peach_pic"
  59. android:id="@+id/peach5"
  60. android:layout_below="@+id/peach2"
  61. android:layout_toStartOf="@+id/peach4"
  62. android:layout_marginRight="25dp"/>
  63. <ImageView
  64. android:id="@+id/peach6"
  65. android:layout_width="wrap_content"
  66. android:layout_height="wrap_content"
  67. android:layout_below="@+id/peach2"
  68. android:layout_toEndOf="@+id/peach4"
  69. android:src="@drawable/peach_pic"
  70. android:layout_marginLeft="20dp"/>
  71. <Button
  72. android:id="@+id/exit_btn"
  73. android:layout_width="wrap_content"
  74. android:layout_height="wrap_content"
  75. android:layout_alignParentRight="true"
  76. android:layout_alignParentBottom="true"
  77. android:layout_marginBottom="50dp"
  78. android:background="@drawable/btn_peach"
  79. android:text="退出桃园"
  80. android:textSize="22sp" />
  81. </RelativeLayout>

 

 

 

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

闽ICP备14008679号