activity_main.xml
activity_mian.xml运行界面
PeachActivity.xml代码
PeachActivity.xml运行界面
MainActivity.java文件
package cn.itcast.pickpeach;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 btn_peach; private TextView tv_count; private int totalCount = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { btn_peach = findViewById(R.id.btn_peach); tv_count = findViewById(R.id.tv_count); btn_peach.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { 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 && requestCode ==1){ int count = data.getIntExtra("count",0); totalCount = totalCount+count; tv_count.setText("摘到"+totalCount+"个"); } }}
PeachActivity.java文件
package cn.itcast.pickpeach;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.Toast;public class PeachActivity extends AppCompatActivity implements View.OnClickListener { private int count = 0; private Button btn_one,btn_two,btn_three,btn_four,btn_five,btn_six,btn_exit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_peach); init(); } private void init() { //定义点击事件,然后获取统计个数 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); //点击事件 btn_one.setOnClickListener(PeachActivity.this); btn_two.setOnClickListener(PeachActivity.this); btn_three.setOnClickListener(PeachActivity.this); btn_four.setOnClickListener(PeachActivity.this); btn_six.setOnClickListener(PeachActivity.this); btn_five.setOnClickListener(PeachActivity.this); btn_exit.setOnClickListener(PeachActivity.this); } private void info(Button btn){ count++; btn.setVisibility(View.INVISIBLE); Toast.makeText(PeachActivity.this,"摘到"+"个桃子",Toast.LENGTH_LONG).show(); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.btn_one: info(btn_one); break; case R.id.btn_two: info(btn_two); break; case R.id.btn_three: info(btn_three); break; case R.id.btn_four: info(btn_four); break; case R.id.btn_five: info(btn_five); break; case R.id.btn_six: info(btn_six); break; case R.id.btn_exit: info(btn_exit); break; } } private void re() { Intent intent = new Intent(); intent.putExtra("count",count); setResult(1,intent); //关闭进程 PeachActivity.this.finish(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0); re(); return false; }}
来源地址:https://blog.csdn.net/NIng_jies/article/details/127621503