activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns: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"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#008577"
android:gravity="center"
android:text="首页"
android:textColor="@android:color/white"
android:textSize="20sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:gravity="center_vertical">
<ImageView
android:id="@+id/iv_monkey"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/monkey" />
<Button
android:id="@+id/btn_peach"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/iv_monkey"
android:background="@drawable/btn_peach"
android:text="去桃园"
android:textColor="@android:color/black" />
<ImageView
android:id="@+id/iv_peach"
android:layout_width="45dp"
android:layout_height="35dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:src="@drawable/peach_pic" />
<TextView
android:id="@+id/tv_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="85dp"
android:layout_toRightOf="@id/iv_peach"
android:text="摘到0个"
android:textColor="@android:color/black"
android:textSize="16sp" />
</RelativeLayout>
</LinearLayout>
activity_mian.xml运行界面
PeachActivity.xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns: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"
tools:context=".PeachActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#008577"
android:gravity="center"
android:text="桃园"
android:textColor="@android:color/white"
android:textSize="20sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/tree_bg">
<RelativeLayout
android:layout_width="227dp"
android:layout_height="254dp"
android:layout_centerInParent="true"
android:layout_marginTop="70dp"
android:background="@drawable/tree">
<Button
android:id="@+id/btn_one"
android:layout_width="45dp"
android:layout_height="35dp"
android:layout_marginLeft="85dp"
android:layout_marginTop="25dp"
android:background="@drawable/peach_pic" />
<Button
android:id="@+id/btn_two"
android:layout_width="45dp"
android:layout_height="35dp"
android:layout_below="@id/btn_one"
android:layout_marginLeft="50dp"
android:layout_marginTop="5dp"
android:background="@drawable/peach_pic" />
<Button
android:id="@+id/btn_three"
android:layout_width="45dp"
android:layout_height="35dp"
android:layout_below="@id/btn_one"
android:layout_marginLeft="25dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/btn_two"
android:background="@drawable/peach_pic" />
<Button
android:id="@+id/btn_four"
android:layout_width="45dp"
android:layout_height="35dp"
android:layout_below="@id/btn_two"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:background="@drawable/peach_pic" />
<Button
android:id="@+id/btn_five"
android:layout_width="45dp"
android:layout_height="35dp"
android:layout_below="@id/btn_two"
android:layout_marginLeft="25dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/btn_four"
android:background="@drawable/peach_pic" />
<Button
android:id="@+id/btn_six"
android:layout_width="45dp"
android:layout_height="35dp"
android:layout_below="@id/btn_two"
android:layout_marginLeft="25dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/btn_five"
android:background="@drawable/peach_pic" />
</RelativeLayout>
<Button
android:id="@+id/btn_exit"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="50dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="50dp"
android:layout_marginBottom="50dp"
android:background="@drawable/btn_peach"
android:text="退出桃园"
android:textColor="@android:color/black" />
</RelativeLayout>
</LinearLayout>
PeachActivity.xml运行界面
文章来源:https://www.toymoban.com/news/detail-520393.html
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+"个");
}
}
}
文章来源地址https://www.toymoban.com/news/detail-520393.html
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;
}
}
到了这里,关于Android Studio 实战演练—小猴子摘桃的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!