安卓---实验7---小猴子摘桃

05-01 1870阅读

文章目录

  • PeachActivity.java
  • MainActivity.java
  • activity_main.xml
  • activity_peach.xml

    安卓---实验7---小猴子摘桃

    PeachActivity.java

    public class PeachActivity extends AppCompatActivity implements View.OnClickListener {
        private ImageView imageView1, imageView2, imageView3, imageView4, imageView5, imageView6;
        private Button btn;
        private int cnt = 0;  //摘桃子的个数
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_peach);
            initView();
        }
        public void initView() {
            imageView1 = findViewById(R.id.peach_1);
            imageView2 = findViewById(R.id.peach_2);
            imageView3 = findViewById(R.id.peach_3);
            imageView4 = findViewById(R.id.peach_4);
            imageView5 = findViewById(R.id.peach_5);
            imageView6 = findViewById(R.id.peach_6);
            btn = findViewById(R.id.btn_exit);
            //监听器
            imageView1.setOnClickListener(this);
            imageView2.setOnClickListener(this);
            imageView3.setOnClickListener(this);
            imageView4.setOnClickListener(this);
            imageView5.setOnClickListener(this);
            imageView6.setOnClickListener(this);
            btn.setOnClickListener(this);
        }
        @Override
        public void onClick(View view) {
            //实现点击事件
            switch (view.getId()) {
                case R.id.peach_1:
                    info(imageView1);
                    break;
                case R.id.peach_2:
                    info(imageView2);
                    break;
                case R.id.peach_3:
                    info(imageView3);
                    break;
                case R.id.peach_4:
                    info(imageView4);
                    break;
                case R.id.peach_5:
                    info(imageView5);
                    break;
                case R.id.peach_6:
                    info(imageView6);
                    break;
                case R.id.btn_exit:
                    returnData();
                    break;
            }
        }
        private void returnData() {
            //将数据回传到上个界面
            Intent intent = new Intent();
            intent.putExtra("cnt", cnt);
            setResult(1002, intent);   //1是返回码
            PeachActivity.this.finish();
        }
        //桃子的点击事件处理
        private void info(ImageView imageView) {
            cnt++;
            imageView.setVisibility(View.INVISIBLE);
            Toast.makeText(PeachActivity.this, "摘了" + cnt + "个桃子", Toast.LENGTH_LONG).show();
        }
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                returnData();
                return true;
            }
            return false;
        }
    }
    

    MainActivity.java

    public class MainActivity extends AppCompatActivity {
        private Button btn1; //去桃园按钮
        private TextView tv_cnt;
        private int totalCnt;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
        }
        private void initView() {
            btn1 = findViewById(R.id.btn_enter);
            tv_cnt = findViewById(R.id.tv_cnt);
            //为"去桃园"按钮增加监听事件,点击这个按钮,跳转到桃园界面
            btn1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(MainActivity.this, PeachActivity.class);
                    startActivityForResult(intent, 1001);
                }
            });
        }
        //  用来接收上个界面传过来的信息的
        @Override
        protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == 1001 && resultCode == 1002) {
                int cnt = data.getIntExtra("cnt", 0);
                totalCnt = totalCnt + cnt;
                tv_cnt.setText("摘了" + totalCnt + "桃子");
            }
        }
    }
    

    activity_main.xml

    
        
        
            
            
            
            
        
    
    

    activity_peach.xml

    
        
        
            
                
                
                
                
                
                
                
            
        
    
    
VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]