Android中Activity的跳转与数据的传递案例(装备的选择)

今天我们介绍一个综合案例,包括activity跳转和回传数据功能
这里写图片描述
点击给宝宝购买装备:
这里写图片描述
会出来几件装备供主人选择,点击不同 的装备会回传到第一个Activity不同的值:
这里写图片描述
做一个这样有趣的小案例
1、做一个装备选择的布局,(附上宝宝的图片):
这里写图片描述

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.edu.bzu.equipments.MainActivity">

    <ImageView
        android:id="@+id/img1"
        android:src="@drawable/baby"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="45dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="主人,快给小宝宝购买装备吧!"
        />
        <!--通过表格布局来实现整齐的效果-->
  <TableLayout
      android:layout_marginTop="25dp"
      android:layout_gravity="center"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
      <TableRow

          android:layout_width="match_parent"
          android:layout_height="wrap_content">
          <TextView
              android:layout_weight="1"
              android:layout_width="0dp"
              android:layout_marginLeft="20dp"
              android:layout_height="wrap_content"
              android:text="生命值:">
          </TextView>
          <!--进度条-->
          <ProgressBar
              android:layout_marginLeft="5dp"
              android:layout_weight="3"
              style="?android:attr/progressBarStyleHorizontal"
              <!--默认的为圆形,需要修改一个它的style属性,变成水平的-->
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/progressBar1" />
          <TextView
              android:id="@+id/tv1"
              android:layout_weight="1"
              android:layout_width="0dp"
              android:layout_marginLeft="20dp"
              android:layout_height="wrap_content"
              android:text="0">

          </TextView>
      </TableRow>
      <TableRow
          android:layout_marginTop="10dp"
          android:layout_width="match_parent"
          android:layout_height="wrap_content">
          <TextView

              android:layout_weight="1"
              android:layout_width="0dp"
              android:layout_marginLeft="20dp"
              android:layout_height="wrap_content"
              android:text="攻击力:">
          </TextView>

          <ProgressBar
              android:layout_weight="3"
              android:layout_marginLeft="5dp"
              style="?android:attr/progressBarStyleHorizontal"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/progressBar2" />
          <TextView
              android:id="@+id/tv2"
              android:layout_weight="1"
              android:layout_width="0dp"
              android:layout_marginLeft="20dp"
              android:layout_height="wrap_content"
              android:text="0">

          </TextView>
      </TableRow>
      <TableRow
          android:layout_marginTop="10dp"
          android:layout_width="match_parent"
          android:layout_height="wrap_content">
          <TextView
              android:layout_weight="1"
              android:layout_width="0dp"
              android:layout_marginLeft="20dp"
              android:layout_height="wrap_content"
              android:text="敏捷度:">
          </TextView>

          <ProgressBar
              android:layout_weight="3"
              android:layout_marginLeft="5dp"
              style="?android:attr/progressBarStyleHorizontal"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/progressBar3" />
          <TextView
              android:id="@+id/tv3"
              android:layout_weight="1"
              android:layout_width="0dp"
              android:layout_marginLeft="20dp"
              android:layout_height="wrap_content"
              android:text="0">

          </TextView>
      </TableRow>
  </TableLayout>
    <LinearLayout
        android:layout_marginTop="20dp"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button
            android:id="@+id/btn1"
            android:layout_weight="1"
            android:text="给宝宝购买装备"
            <!--这里引用Android自带的图片-->           android:drawableRight="@android:drawable/ic_menu_add"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>
</LinearLayout>

2、与之对应的Activity中需要给Button设置一个点击事件,在这里把所有的控件初始化一下

    ProgressBar progressBar1;
    ProgressBar progressBar2;
    ProgressBar progressBar3;
    TextView textView2;
    TextView textView1;
    TextView textView3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
        progressBar2 = (ProgressBar) findViewById(R.id.progressBar2);
        progressBar3 = (ProgressBar) findViewById(R.id.progressBar3);
        textView1 = (TextView) findViewById(R.id.tv1);
        textView2 = (TextView) findViewById(R.id.tv2);
        textView3 = (TextView) findViewById(R.id.tv3);
        progressBar1.setMax(1000);//进度条最大值
        progressBar2.setMax(1000);
        progressBar3.setMax(1000);
        Button button1 = (Button) findViewById(R.id.btn1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ShoppingActivity.class);
                //需要重写onActivityResult()方法
                startActivityForResult(intent, 1);
            }
        });

3、创建装备界面activity_shopping,用布局嵌套,嵌套三个相对布局。

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_shopping"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
   >
    <View
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:background="@android:drawable/ic_menu_info_details"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        />
    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="60dp"
        android:text="商品名称"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">
        <TextView
            android:id="@+id/tv_life"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="13sp"
            android:text="生命值"/>
        <TextView
            android:id="@+id/tv_attack"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="13sp"
            android:text="攻击力"/>
        <TextView
            android:id="@+id/tv_speed"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="13sp"
            android:text="速度"/>
    </LinearLayout>
</RelativeLayout>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/activity_shopping1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        >
        <View
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@android:drawable/ic_menu_info_details"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            />
        <TextView
            android:id="@+id/tv_name1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="60dp"
            android:text="商品名称"/>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:orientation="vertical">
            <TextView
                android:id="@+id/tv_life1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="13sp"
                android:text="生命值"/>
            <TextView
                android:id="@+id/tv_attack1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="13sp"
                android:text="攻击力"/>
            <TextView
                android:id="@+id/tv_speed1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="13sp"
                android:text="速度"/>
        </LinearLayout>
    </RelativeLayout>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/activity_shopping2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        >
        <View
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@android:drawable/ic_menu_info_details"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            />
        <TextView
            android:id="@+id/tv_name3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="60dp"
            android:text="商品名称"/>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:orientation="vertical">
            <TextView
                android:id="@+id/tv_life3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="13sp"
                android:text="生命值"/>
            <TextView
                android:id="@+id/tv_attack3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="13sp"
                android:text="攻击力"/>
            <TextView
                android:id="@+id/tv_speed3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="13sp"
                android:text="速度"/>
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>

4、创建Info实体类,封装装备信息。为了方便数据的传递,在这里让Info类实现Serializable接口。

public class Info implements Serializable {
    private String name;
    private int life;
    private int attack;
    private int quick;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getQuick() {
        return quick;
    }

    public void setQuick(int quick) {
        this.quick = quick;
    }

    public int getAttack() {
        return attack;
    }

    public void setAttack(int attack) {
        this.attack = attack;
    }

    public int getLife() {
        return life;
    }

    public void setLife(int life) {
        this.life = life;
    }

    public Info(String name, int life, int attack, int quick) {
        this.name = name;
        this.life = life;
        this.attack = attack;
        this.quick = quick;
    }
}

5、创建ShopActivity

public class ShoppingActivity extends AppCompatActivity {
    Info info;
    Info info1;
    Info info3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping);
        TextView textView1 =(TextView) findViewById(R.id.tv_name);
        TextView textView2 =(TextView) findViewById(R.id.tv_life);
        TextView textView3 =(TextView) findViewById(R.id.tv_attack);
        TextView textView4 =(TextView) findViewById(R.id.tv_speed);
        info = new Info("金剑",20,100,20);
        textView1.setText(info.getName());
        textView2.setText("生命值"+ info.getLife());
        textView3.setText("攻击力"+ info.getAttack());
        textView4.setText("速度"+ info.getQuick());
        RelativeLayout relativeLayout =(RelativeLayout) findViewById(R.id.activity_shopping);
        TextView textView12 =(TextView) findViewById(R.id.tv_name1);
        TextView textView22 =(TextView) findViewById(R.id.tv_life1);
        TextView textView32 =(TextView) findViewById(R.id.tv_attack1);
        TextView textView42 =(TextView) findViewById(R.id.tv_speed1);
        info1 = new Info("大剑",40,200,20);
        textView12.setText(info1.getName());
        textView22.setText("生命值"+ info1.getLife());
        textView32.setText("攻击力"+ info1.getAttack());
        textView42.setText("速度"+ info1.getQuick());
        RelativeLayout relativeLayout1 =(RelativeLayout) findViewById(R.id.activity_shopping1);
        TextView textView13 =(TextView) findViewById(R.id.tv_name3);
        TextView textView23 =(TextView) findViewById(R.id.tv_life3);
        TextView textView33 =(TextView) findViewById(R.id.tv_attack3);
        TextView textView43 =(TextView) findViewById(R.id.tv_speed3);
        info3 = new Info("多兰剑",10,80,10);
        textView13.setText(info3.getName());
        textView23.setText("生命值"+ info3.getLife());
        textView33.setText("攻击力"+ info3.getAttack());
        textView43.setText("速度"+ info3.getQuick());
        RelativeLayout relativeLayout3 =(RelativeLayout) findViewById(R.id.activity_shopping2);
        relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                        Intent intent = new Intent();
                        intent.putExtra("info", info);
                        //把结果返回给调用者(mainA --onActivityResult())
                        setResult(10,intent);
                        finish();

            }
        });
        relativeLayout1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("info1", info1);
                //把结果返回给调用者(mainA --onActivityResult())
                setResult(9,intent);
                finish();
            }
        });
        relativeLayout3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("info3", info3);
                //把结果返回给调用者(mainA --onActivityResult())
                setResult(8,intent);
                finish();
            }
        });

    }
}

6、回传数据,编写MainActivity

protected void onActivityResult(int requestCode, int resultCode,Intent data){
            if(resultCode==10){
                //代表数据来源于数据shop
               Info info =(Info) data.getExtras().get("info");
                updataProgressBar(info);
            }
            if(resultCode==9){     
                Info info =(Info) data.getExtras().get("info1");
                updataProgressBar(info);
            }
            if(resultCode==8){    
                Info info =(Info) data.getExtras().get("info3");
                updataProgressBar(info);//后面要写这个方法
            }
            super.onActivityResult(requestCode,resultCode,data);
        }

7、updataProgressBar()方法,更新进度条和后面的数值。

        public void updataProgressBar(Info info){
            int pb1 = progressBar1.getProgress();
            int pb2 = progressBar2.getProgress();
            int pb3 =progressBar3.getProgress();

            progressBar1.setProgress(pb1+info.getLife());
            progressBar2.setProgress(pb2+info.getAttack());
            progressBar3.setProgress(pb3+info.getQuick());


            textView1.setText(progressBar1.getProgress()+"");
            textView2.setText(progressBar2.getProgress()+"");
            textView3.setText(progressBar3.getProgress()+"");



        }

现在这个小案例就完成了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值