小猴子摘桃APP目录
目录
Android界面图:
运行结果图:
MainActivity.java代码:
MainActivity2.java代码:
activity_main.xml代码:
activity_main.xml2代码:
Android界面图:
运行结果图:
MainActivity.java代码:
package com.example.myapplication1;import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;public class MainActivity extends AppCompatActivity {private Button an; //去桃园按钮private TextView tv_cnt;private int totalCnt;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();}private void initView() {View btn1 = findViewById(R.id.btn_enter);tv_cnt = findViewById(R.id.tv_cnt);//为"去桃园"按钮增加监听事件,点击这个按钮,跳转到桃园界面btn1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Intent intent = new Intent(MainActivity.this, MainActivity2.class);startActivityForResult(intent, 1001);}});}// 用来接收上个界面传过来的信息的@Overrideprotected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {super.onActivityResult(requestCode, resultCode, data);if (requestCode == 1001 && resultCode == 1002) {int cnt = data.getIntExtra("cnt", 0);totalCnt = totalCnt + cnt;tv_cnt.setText("摘了" + totalCnt + "桃子");}}
}
MainActivity2.java代码:
package com.example.myapplication1;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;public class MainActivity2 extends AppCompatActivity implements View.OnClickListener {private ImageView imageView1, imageView2, imageView3, imageView4, imageView5, imageView6;private Button an;private int cnt = 0; //摘桃子的个数@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main2);initView();}public void initView() {imageView1 = findViewById(R.id.peach_1);imageView2 = findViewById(R.id.peach_2);imageView3 = findViewById(R.id.peach_3);imageView4 = findViewById(R.id.peach_4);imageView5 = findViewById(R.id.peach_5);imageView6 = findViewById(R.id.peach_6);an= findViewById(R.id.btn_exit);//监听器imageView1.setOnClickListener((View.OnClickListener) this);imageView2.setOnClickListener(this);imageView3.setOnClickListener((View.OnClickListener) this);imageView4.setOnClickListener(this);imageView5.setOnClickListener(this);imageView6.setOnClickListener(this);an.setOnClickListener(this);}public void onClick(View view) {//实现点击事件switch (view.getId()) {case R.id.peach_1:info(imageView1);break;case R.id.peach_2:info(imageView2);break;case R.id.peach_3:info(imageView3);break;case R.id.peach_4:info(imageView4);break;case R.id.peach_5:info(imageView5);break;case R.id.peach_6:info(imageView6);break;case R.id.btn_exit:returnData();break;}}private void returnData() {//将数据回传到上个界面Intent intent = new Intent();intent.putExtra("cnt", cnt);setResult(1002, intent); //1是返回码MainActivity2.this.finish();}//桃子的点击事件处理private void info(ImageView imageView) {cnt++;imageView.setVisibility(View.INVISIBLE);Toast.makeText(MainActivity2.this, "摘了" + cnt + "个桃子", Toast.LENGTH_LONG).show();}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {returnData();return true;}return false;}@Overridepublic void onPointerCaptureChanged(boolean hasCapture) {super.onPointerCaptureChanged(hasCapture);}
}
activity_main.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><TextViewandroid:layout_width="match_parent"android:layout_height="80dp"android:background="#FF009688"android:gravity="center"android:text="主页"android:textColor="@color/white"android:textSize="27sp" /><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bj"android:gravity="center_vertical"><ImageViewandroid:id="@+id/iv_monkey"android:layout_width="180dp"android:layout_height="180dp"android:src="@drawable/hz" /><Buttonandroid:id="@+id/btn_enter"android:layout_width="200dp"android:layout_height="100dp"android:layout_marginTop="20dp"android:layout_toRightOf="@+id/iv_monkey"android:background="@drawable/an"android:text="去桃园"android:textSize="40sp" /><ImageViewandroid:id="@+id/iv_peach"android:layout_width="130dp"android:layout_height="130dp"android:layout_marginLeft="20dp"android:layout_marginTop="200dp"android:src="@drawable/tz" /><TextViewandroid:id="@+id/tv_cnt"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:layout_marginTop="250dp"android:layout_toRightOf="@id/iv_peach"android:text="摘到了0个"android:textSize="40sp" /></RelativeLayout></LinearLayout>
activity_main.xml2代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity2"><TextViewandroid:layout_width="match_parent"android:layout_height="80dp"android:background="#FF009688"android:gravity="center"android:text="桃园"android:textColor="@color/white"android:textSize="27sp" /><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bj2"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_centerInParent="true"android:background="@drawable/shu"><ImageViewandroid:id="@+id/peach_1"android:layout_width="80dp"android:layout_height="80dp"android:layout_marginLeft="150dp"android:layout_marginTop="40dp"android:src="@drawable/tz" /><ImageViewandroid:id="@+id/peach_2"android:layout_width="80dp"android:layout_height="80dp"android:layout_below="@+id/peach_1"android:layout_marginLeft="100dp"android:src="@drawable/tz" /><ImageViewandroid:id="@+id/peach_3"android:layout_width="80dp"android:layout_height="80dp"android:layout_below="@+id/peach_1"android:layout_marginLeft="220dp"android:src="@drawable/tz" /><ImageViewandroid:id="@+id/peach_4"android:layout_width="80dp"android:layout_height="80dp"android:layout_below="@+id/peach_2"android:layout_marginLeft="75dp"android:src="@drawable/tz" /><ImageViewandroid:id="@+id/peach_5"android:layout_width="80dp"android:layout_height="80dp"android:layout_below="@id/peach_2"android:layout_marginLeft="175dp"android:src="@drawable/tz" /><ImageViewandroid:id="@+id/peach_6"android:layout_width="80dp"android:layout_height="80dp"android:layout_below="@id/peach_2"android:layout_marginLeft="270dp"android:src="@drawable/tz" /><Buttonandroid:id="@+id/btn_exit"android:layout_width="200dp"android:layout_height="100dp"android:layout_marginLeft="200dp"android:layout_marginTop="450dp"android:background="@drawable/an"android:text="退出桃园"android:textSize="30sp" /></RelativeLayout></RelativeLayout></LinearLayout>