0


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

文章目录

在这里插入图片描述

PeachActivity.java

publicclassPeachActivityextendsAppCompatActivityimplementsView.OnClickListener{privateImageView imageView1, imageView2, imageView3, imageView4, imageView5, imageView6;privateButton btn;privateint cnt =0;//摘桃子的个数@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_peach);initView();}publicvoidinitView(){
        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);}@OverridepublicvoidonClick(View view){//实现点击事件switch(view.getId()){caseR.id.peach_1:info(imageView1);break;caseR.id.peach_2:info(imageView2);break;caseR.id.peach_3:info(imageView3);break;caseR.id.peach_4:info(imageView4);break;caseR.id.peach_5:info(imageView5);break;caseR.id.peach_6:info(imageView6);break;caseR.id.btn_exit:returnData();break;}}privatevoidreturnData(){//将数据回传到上个界面Intent intent =newIntent();
        intent.putExtra("cnt", cnt);setResult(1002, intent);//1是返回码PeachActivity.this.finish();}//桃子的点击事件处理privatevoidinfo(ImageView imageView){
        cnt++;
        imageView.setVisibility(View.INVISIBLE);Toast.makeText(PeachActivity.this,"摘了"+ cnt +"个桃子",Toast.LENGTH_LONG).show();}@OverridepublicbooleanonKeyDown(int keyCode,KeyEvent event){if(keyCode ==KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0){returnData();returntrue;}returnfalse;}}

MainActivity.java

publicclassMainActivityextendsAppCompatActivity{privateButton btn1;//去桃园按钮privateTextView tv_cnt;privateint totalCnt;@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();}privatevoidinitView(){
        btn1 =findViewById(R.id.btn_enter);
        tv_cnt =findViewById(R.id.tv_cnt);//为"去桃园"按钮增加监听事件,点击这个按钮,跳转到桃园界面
        btn1.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(View view){Intent intent =newIntent(MainActivity.this,PeachActivity.class);startActivityForResult(intent,1001);}});}//  用来接收上个界面传过来的信息的@OverrideprotectedvoidonActivityResult(int requestCode,int resultCode,@NullableIntent 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

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns: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"android:orientation="vertical"tools:context=".MainActivity"><TextViewandroid:layout_width="match_parent"android:layout_height="80dp"android:background="#FF009688"android:gravity="center"android:text="主页"android:textColor="@color/white"android:textSize="27sp"/><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bg1"android:gravity="center_vertical"><ImageViewandroid:id="@+id/iv_monkey"android:layout_width="180dp"android:layout_height="180dp"android:src="@drawable/houzi"/><Buttonandroid:id="@+id/btn_enter"android:layout_width="200dp"android:layout_height="100dp"android:layout_marginTop="20dp"android:layout_toRightOf="@+id/iv_monkey"android:background="@drawable/btn2"android:text="去桃园"android:textSize="40sp"/><ImageViewandroid:id="@+id/iv_peach"android:layout_width="130dp"android:layout_height="130dp"android:layout_marginLeft="20dp"android:layout_marginTop="200dp"android:src="@drawable/taozi"/><TextViewandroid:id="@+id/tv_cnt"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:layout_marginTop="250dp"android:layout_toRightOf="@id/iv_peach"android:text="摘到了0个"android:textSize="40sp"/></RelativeLayout></LinearLayout>

activity_peach.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns: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"android:orientation="vertical"tools:context=".PeachActivity"><TextViewandroid:layout_width="match_parent"android:layout_height="80dp"android:background="#FF009688"android:gravity="center"android:text="桃园"android:textColor="@color/white"android:textSize="27sp"/><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bg2"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_centerInParent="true"android:background="@drawable/shu"><ImageViewandroid:id="@+id/peach_1"android:layout_width="80dp"android:layout_height="80dp"android:layout_marginLeft="150dp"android:layout_marginTop="40dp"android:src="@drawable/taozi"/><ImageViewandroid:id="@+id/peach_2"android:layout_width="80dp"android:layout_height="80dp"android:layout_below="@+id/peach_1"android:layout_marginLeft="100dp"android:src="@drawable/taozi"/><ImageViewandroid:id="@+id/peach_3"android:layout_width="80dp"android:layout_height="80dp"android:layout_below="@+id/peach_1"android:layout_marginLeft="220dp"android:src="@drawable/taozi"/><ImageViewandroid:id="@+id/peach_4"android:layout_width="80dp"android:layout_height="80dp"android:layout_below="@+id/peach_2"android:layout_marginLeft="75dp"android:src="@drawable/taozi"/><ImageViewandroid:id="@id/peach_5"android:layout_width="80dp"android:layout_height="80dp"android:layout_below="@id/peach_2"android:layout_marginLeft="175dp"android:src="@drawable/taozi"/><ImageViewandroid:id="@+id/peach_6"android:layout_width="80dp"android:layout_height="80dp"android:layout_below="@id/peach_2"android:layout_marginLeft="270dp"android:src="@drawable/taozi"/><Buttonandroid:id="@+id/btn_exit"android:layout_width="200dp"android:layout_height="100dp"android:layout_marginLeft="200dp"android:layout_marginTop="450dp"android:background="@drawable/btn2"android:text="退出桃园"android:textSize="30sp"/></RelativeLayout></RelativeLayout></LinearLayout>

本文转载自: https://blog.csdn.net/yi_tong_/article/details/127693032
版权归原作者 小星星xx 所有, 如有侵权,请联系我们删除。

“安卓---实验7---小猴子摘桃”的评论:

还没有评论