目录
具体功能
首页显示摘桃数量,并有一个去桃园摘桃子的按钮。
点击去桃园按钮跳转至桃园页面,桃园页面可以摘桃子,并带有退出桃园按钮。
在桃园摘桃子会实时显示在页面下方,当点击退出桃园按钮时会回到首页并将摘桃数显示在首页,当再次点击去桃园按钮时会清除桃子数量重新计算。
相关配置
我的gradle-wrapper.properties配置如下:
我的项目目录结构如下:
具体实现代码
MainActivity.java
package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private Button btn_peach;
private TextView tv_count;
private int totalCount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
setupClickListeners();
updatePeachCount();
}
private void initViews() {
btn_peach = findViewById(R.id.btn_peach);
tv_count = findViewById(R.id.tv_count);
}
private void setupClickListeners() {
btn_peach.setOnClickListener(v -> {
// 启动PeachActivity时重置计数
totalCount = 0;
updatePeachCount();
Intent intent = new Intent(MainActivity.this, PeachActivity.class);
startActivityForResult(intent, 1);
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data != null) {
// 获取本次摘桃子的数量(PeachActivity已经返回的是总数)
totalCount = data.getIntExtra("count", 0);
updatePeachCount();
}
}
private void updatePeachCount() {
tv_count.setText("摘到" + totalCount + "个");
}
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("totalCount", totalCount);
}
@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
totalCount = savedInstanceState.getInt("totalCount");
updatePeachCount();
}
}
PeachActivity.java
package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class PeachActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_one, btn_two, btn_three, btn_four, btn_five, btn_six, btn_exit;
private int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_peach);
initViews();
setupClickListeners();
// 每次进入桃园时重置计数
count = 0;
resetAllPeaches();
}
private void initViews() {
btn_one = findViewById(R.id.btn_one);
btn_two = findViewById(R.id.btn_two);
btn_three = findViewById(R.id.btn_three);
btn_four = findViewById(R.id.btn_four);
btn_five = findViewById(R.id.btn_five);
btn_six = findViewById(R.id.btn_six);
btn_exit = findViewById(R.id.btn_exit);
}
private void setupClickListeners() {
btn_one.setOnClickListener(this);
btn_two.setOnClickListener(this);
btn_three.setOnClickListener(this);
btn_four.setOnClickListener(this);
btn_five.setOnClickListener(this);
btn_six.setOnClickListener(this);
btn_exit.setOnClickListener(this);
}
private void resetAllPeaches() {
btn_one.setVisibility(View.VISIBLE);
btn_two.setVisibility(View.VISIBLE);
btn_three.setVisibility(View.VISIBLE);
btn_four.setVisibility(View.VISIBLE);
btn_five.setVisibility(View.VISIBLE);
btn_six.setVisibility(View.VISIBLE);
}
@Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.btn_exit) {
returnData();
} else {
count++;
v.setVisibility(View.INVISIBLE);
Toast.makeText(this, "摘到" + count + "个桃子", Toast.LENGTH_SHORT).show();
}
}
private void returnData() {
Intent resultIntent = new Intent();
resultIntent.putExtra("count", count); // 返回本次摘桃子的总数
setResult(RESULT_OK, resultIntent);
finish();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
returnData();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#008577"
android:gravity="center"
android:text="首页"
android:textColor="@android:color/white"
android:textSize="20sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:gravity="center_vertical">
<ImageView
android:id="@+id/iv_monkey"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/monkey" />
<Button
android:id="@+id/btn_peach"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/iv_monkey"
android:background="@drawable/btn_peach"
android:text="去桃园"
android:textColor="@android:color/black" />
<ImageView
android:id="@+id/iv_peach"
android:layout_width="45dp"
android:layout_height="35dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:src="@drawable/peach_pic" />
<TextView
android:id="@+id/tv_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="85dp"
android:layout_toRightOf="@id/iv_peach"
android:text="摘到0个"
android:textColor="@android:color/black"
android:textSize="16sp" />
</RelativeLayout>
</LinearLayout>
activity_peach.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#008577"
android:gravity="center"
android:text="桃园"
android:textColor="@android:color/white"
android:textSize="20sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/tree_bg">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="70dp"
android:background="@drawable/tree">
<Button
android:id="@+id/btn_one"
android:layout_width="45dp"
android:layout_height="35dp"
android:layout_marginLeft="85dp"
android:layout_marginTop="25dp"
android:background="@drawable/peach_pic" />
<Button
android:id="@+id/btn_two"
android:layout_width="45dp"
android:layout_height="35dp"
android:layout_below="@id/btn_one"
android:layout_marginLeft="50dp"
android:layout_marginTop="5dp"
android:background="@drawable/peach_pic" />
<Button
android:id="@+id/btn_three"
android:layout_width="45dp"
android:layout_height="35dp"
android:layout_below="@id/btn_one"
android:layout_marginLeft="25dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/btn_two"
android:background="@drawable/peach_pic" />
<Button
android:id="@+id/btn_four"
android:layout_width="45dp"
android:layout_height="35dp"
android:layout_below="@id/btn_two"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:background="@drawable/peach_pic" />
<Button
android:id="@+id/btn_five"
android:layout_width="45dp"
android:layout_height="35dp"
android:layout_below="@id/btn_two"
android:layout_marginLeft="25dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/btn_four"
android:background="@drawable/peach_pic" />
<Button
android:id="@+id/btn_six"
android:layout_width="45dp"
android:layout_height="35dp"
android:layout_below="@id/btn_two"
android:layout_marginLeft="25dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/btn_five"
android:background="@drawable/peach_pic" />
</RelativeLayout>
<Button
android:id="@+id/btn_exit"
android:layout_width="130dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_margin="50dp"
android:background="@drawable/btn_peach"
android:text="退出桃园"
android:textColor="@android:color/black" />
</RelativeLayout>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".PeachActivity"></activity>
</application>
</manifest>
图片资源放到res/drawable目录下
取图片资源:
链接: https://pan.baidu.com/s/18QCm5dmWiK0sYGne8l-FuQ 提取码: v5mh
如有问题请在文章评论留言