\app\src\main\AndroidManifest.xml
\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() { @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
\app\src\main\res\layout\activity_main.xml
\app\src\main\res\layout\activity_peach.xml
来源地址:https://blog.csdn.net/weixin_53158593/article/details/127661001