\app\src\main\AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.peach">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Peach">
<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"/>
</application>
</manifest>
\app\src\main\java\com\example\peach\MainActivity.java
package com.example.peach;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button pickBtn;
private TextView peachTotal;
int count=0;
private ActivityResultLauncher launcher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if(result != null){
if(result.getResultCode() == RESULT_OK){
Intent data = result.getData();
int peachNum = data.getIntExtra("peachNum", 0);
Log.i("MainActivity", "onActivityResult: "+ peachNum);
count = count + peachNum;
peachTotal.setText("桃子" + count + "个");
}
}
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pickBtn = findViewById(R.id.pick_btn);
peachTotal = findViewById(R.id.peach_total);
pickBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, PeachActivity.class);
launcher.launch(i);
}
});
}
}
\app\src\main\java\com\example\peach\PeachActivity.java
package com.example.peach;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class PeachActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView peach1,peach2,peach3,peach4,peach5,peach6;
private Button exitBtn;
private int num = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_peach);
peach1 = findViewById(R.id.peach1);
peach2 = findViewById(R.id.peach2);
peach3 = findViewById(R.id.peach3);
peach4 = findViewById(R.id.peach4);
peach5 = findViewById(R.id.peach5);
peach6 = findViewById(R.id.peach6);
exitBtn = findViewById(R.id.exit_btn);
peach1.setOnClickListener(this);
peach2.setOnClickListener(this);
peach3.setOnClickListener(this);
peach4.setOnClickListener(this);
peach5.setOnClickListener(this);
peach6.setOnClickListener(this);
exitBtn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if(view.getId() == R.id.peach1){
info(peach1);
}else if(view.getId() == R.id.peach2){
info(peach2);
}else if(view.getId() == R.id.peach3){
info(peach3);
}else if(view.getId() == R.id.peach4){
info(peach4);
}else if(view.getId() == R.id.peach5){
info(peach5);
}else if(view.getId() == R.id.peach6){
info(peach6);
}else if(view.getId() == R.id.exit_btn){
returnData();
}
}
private void returnData() {
Intent i = new Intent();
i.putExtra("peachNum", num);
setResult(RESULT_OK,i);
finish();
}
private void info(ImageView imageView) {
imageView.setVisibility(View.INVISIBLE);
num++;
}
@Override
public void onBackPressed() {
Intent i = new Intent();
i.putExtra("peachNum", num);
setResult(RESULT_OK,i);
finish();
}
}
\app\src\main\res\values\themes.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Peach" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
\app\src\main\res\layout\activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity"
android:background="@drawable/bg">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/teal_700"
android:gravity="center"
android:padding="5dp"
android:text="首页"
android:textColor="#fff"
android:textSize="25sp" />
<ImageView
android:id="@+id/monkey"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/monkey"
android:layout_centerVertical="true"/>
<Button
android:id="@+id/pick_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/monkey"
android:layout_toEndOf="@+id/monkey"
android:background="@drawable/btn_peach"
android:text="去桃园"
android:textSize="22sp" />
<ImageView
android:id="@+id/peach"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/pick_btn"
android:layout_alignLeft="@+id/pick_btn"
android:layout_marginTop="50dp"
android:src="@drawable/peach_pic" />
<TextView
android:id="@+id/peach_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/peach"
android:layout_toEndOf="@+id/peach"
android:text="摘到0个"
android:textSize="22sp" />
</RelativeLayout>
\app\src\main\res\layout\activity_peach.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".PeachActivity"
android:background="@drawable/tree_bg">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/teal_700"
android:gravity="center"
android:padding="5dp"
android:text="桃园"
android:textColor="#fff"
android:textSize="25sp" />
<ImageView
android:id="@+id/tree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/tree" />
<ImageView
android:id="@+id/peach1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/tree"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:src="@drawable/peach_pic" />
<ImageView
android:id="@+id/peach2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/peach1"
android:layout_alignLeft="@+id/tree"
android:layout_marginLeft="50dp"
android:src="@drawable/peach_pic" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/peach_pic"
android:id="@+id/peach3"
android:layout_below="@+id/peach1"
android:layout_alignRight="@+id/tree"
android:layout_marginRight="50dp"/>
<ImageView
android:id="@+id/peach4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/peach2"
android:layout_centerHorizontal="true"
android:src="@drawable/peach_pic" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/peach_pic"
android:id="@+id/peach5"
android:layout_below="@+id/peach2"
android:layout_toStartOf="@+id/peach4"
android:layout_marginRight="25dp"/>
<ImageView
android:id="@+id/peach6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/peach2"
android:layout_toEndOf="@+id/peach4"
android:src="@drawable/peach_pic"
android:layout_marginLeft="20dp"/>
<Button
android:id="@+id/exit_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
android:background="@drawable/btn_peach"
android:text="退出桃园"
android:textSize="22sp" />
</RelativeLayout>
文章来源:https://www.toymoban.com/news/detail-742546.html
文章来源地址https://www.toymoban.com/news/detail-742546.html
到了这里,关于【Android Studio】实现简易猴子摘桃功能的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!