Android Studio 实战演练—小猴子摘桃
短信预约 -IT技能 免费直播动态提醒
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
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341