在这里插入图片描述

PeachActivity.java

public class PeachActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView imageView1, imageView2, imageView3, imageView4, imageView5, imageView6;
    private Button btn;
    private int cnt = 0;  //摘桃子的个数

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_peach);
        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);
        btn = findViewById(R.id.btn_exit);

        //监听
        imageView1.setOnClickListener(this);
        imageView2.setOnClickListener(this);
        imageView3.setOnClickListener(this);
        imageView4.setOnClickListener(this);
        imageView5.setOnClickListener(this);
        imageView6.setOnClickListener(this);
        btn.setOnClickListener(this);
    }

    @Override
    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是返回
        PeachActivity.this.finish();
    }

    //桃子的点击事件处理
    private void info(ImageView imageView) {
        cnt++;
        imageView.setVisibility(View.INVISIBLE);
        Toast.makeText(PeachActivity.this, "摘了" + cnt + "个桃子", Toast.LENGTH_LONG).show();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            returnData();
            return true;
        }
        return false;
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private Button btn1; //去桃园按钮
    private TextView tv_cnt;
    private int totalCnt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        btn1 = findViewById(R.id.btn_enter);
        tv_cnt = findViewById(R.id.tv_cnt);
        //为"去桃园"按钮增加监听事件,点击这个按钮,跳转到桃园界面
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, PeachActivity.class);
                startActivityForResult(intent, 1001);
            }
        });
    }

    //  用来接收上个界面传过来信息
    @Override
    protected 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 + "桃子");
        }
    }
}

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">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="#FF009688"
        android:gravity="center"
        android:text="主页"
        android:textColor="@color/white"
        android:textSize="27sp" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg1"
        android:gravity="center_vertical">

        <ImageView
            android:id="@+id/iv_monkey"
            android:layout_width="180dp"
            android:layout_height="180dp"
            android:src="@drawable/houzi" />

        <Button
            android: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/btn2"
            android:text="去桃园"
            android:textSize="40sp" />

        <ImageView
            android:id="@+id/iv_peach"
            android:layout_width="130dp"
            android:layout_height="130dp"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="200dp"
            android:src="@drawable/taozi" />

        <TextView
            android: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_peach.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=".PeachActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="#FF009688"
        android:gravity="center"
        android:text="桃园"
        android:textColor="@color/white"
        android:textSize="27sp" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg2">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:background="@drawable/shu">

            <ImageView
                android:id="@+id/peach_1"
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:layout_marginLeft="150dp"
                android:layout_marginTop="40dp"
                android:src="@drawable/taozi" />

            <ImageView
                android: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/taozi" />

            <ImageView
                android: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/taozi" />

            <ImageView
                android: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/taozi" />

            <ImageView
                android: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/taozi" />

            <ImageView
                android: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/taozi" />

            <Button
                android:id="@+id/btn_exit"
                android:layout_width="200dp"
                android:layout_height="100dp"
                android:layout_marginLeft="200dp"
                android:layout_marginTop="450dp"
                android:background="@drawable/btn2"
                android:text="退出桃园"
                android:textSize="30sp" />
        </RelativeLayout>

    </RelativeLayout>

</LinearLayout>

原文地址:https://blog.csdn.net/yi_tong_/article/details/127693032

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_10341.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注