赞
踩
此次学习的目的是学习Activity之间数据传递,包括了传递数据以及数据回传
主题是课堂上的案例小猴子摘桃:话不多说,看一下效果图:
后续代码讲解会在后续补充
首先看一下第一个Activity的xml文件,也就是首页
我这里的文件名为activity_main48.xml,可以自行更改,用的文件并不多,activity两个
- <?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"
- android:orientation="vertical"
- tools:context=".MainActivity48">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:background="#008577"
- android:gravity="center"
- android:text="首页"
- android:textColor="@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:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/iv_monkey"
- android:src="@drawable/monkey"/>
- <Button
- android:layout_width="80dp"
- android:layout_height="30dp"
- android:id="@+id/btn_peach"
- android:layout_marginLeft="30dp"
- android:layout_marginTop="20dp"
- android:layout_toRightOf="@+id/iv_monkey"
- android:background="@drawable/btn_peach"
- android:text="去桃园"
- android:textColor="@color/black"/>
- <ImageView
- android:layout_width="45dp"
- android:layout_height="35dp"
- android:layout_centerHorizontal="true"
- android:id="@+id/iv_peach"
- android:layout_marginTop="80dp"
- android:src="@drawable/peach_pic"/>
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/tv_count"
- android:layout_marginTop="85dp"
- android:layout_toRightOf="@id/iv_peach"
- android:text="摘到0个"
- android:textColor="@color/black"
- android:textSize="16sp"/>
- </RelativeLayout>
- </LinearLayout>
第二个界面代码:
activity_main47.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"
- android:theme="@style/Theme.AppCompat.NoActionBar"
- android:orientation="vertical"
- tools:context=".MainActivity47">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:background="#008577"
- android:text="桃园"
- android:textColor="@color/white"
- android:textSize="20sp"
- android:gravity="center"/>
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@drawable/tree_bg">
- <RelativeLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:layout_marginTop="70dp"
- android:background="@drawable/tree">
- <Button
- android:layout_width="45dp"
- android:layout_height="35dp"
- android:layout_marginLeft="85dp"
- android:id="@+id/btn_one"
- android:layout_marginTop="25dp"
- android:background="@drawable/peach_pic"/>
- <Button
- android:layout_width="45dp"
- android:layout_height="35dp"
- android:id="@+id/btn_two"
- android:layout_below="@+id/btn_one"
- android:layout_marginLeft="50dp"
- android:layout_marginTop="5dp"
- android:background="@drawable/peach_pic"/>
- <Button
- android:layout_width="45dp"
- android:layout_height="35dp"
- android:id="@+id/btn_three"
- 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:layout_width="45dp"
- android:layout_height="35dp"
- android:id="@+id/btn_four"
- android:layout_below="@id/btn_two"
- android:layout_marginLeft="25dp"
- android:layout_marginTop="5dp"
- android:background="@drawable/peach_pic"/>
- <Button
- android:layout_width="45dp"
- android:layout_height="35dp"
- android:id="@+id/btn_five"
- 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:layout_width="45dp"
- android:layout_height="35dp"
- android:id="@+id/btn_six"
- 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:layout_width="80dp"
- android:layout_height="30dp"
- android:layout_alignParentRight="true"
- android:layout_alignParentBottom="true"
- android:layout_margin="50dp"
- android:background="@drawable/btn_peach"
- android:text="退出桃园"
- android:textColor="@color/black"
- android:id="@+id/btn_exit"/>
- </RelativeLayout>
-
- </LinearLayout>
MainActivity48:对应第一个xml文件的逻辑代码(可以自行命名)
- 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 MainActivity48 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_main48);
- init();
- }
- public 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 v) {
- Intent intent=new Intent(MainActivity48.this,MainActivity47.class);
- startActivityForResult(intent,1);
- }
- });
- }
-
- @Override
- protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.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+"个");
- }
- }
- }
MainActivity47:
- 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 MainActivity47 extends AppCompatActivity implements View.OnClickListener {
- private Button btn_one,btn_two,btn_three,btn_four,btn_five,btn_six,btn_exit;
- private int count=0;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main47);
- 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(this);
- btn_two.setOnClickListener(this);
- btn_three.setOnClickListener(this);
- btn_four.setOnClickListener(this);
- btn_five.setOnClickListener(this);
- btn_six.setOnClickListener(this);
- btn_exit.setOnClickListener(this);
- }
-
- @Override
- public void onClick(View v) {
- switch (v.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:returnData();break;
- }
- }
- private void info(Button button){
- count++;
- button.setVisibility(View.INVISIBLE);
- Toast.makeText(MainActivity47.this,"摘到了"+count+"个桃子",Toast.LENGTH_SHORT).show();
- }
- private void returnData(){
- Intent intent=new Intent();
- intent.putExtra("count",count);
- setResult(1,intent);
- MainActivity47.this.finish();
- }
-
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if(keyCode==KeyEvent.KEYCODE_BACK&&event.getRepeatCount()==0){
- returnData();
- }
- return false;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。