Android开发案例——装备选择

      该案例主要为了练习android开发中的数据操作,如何在多个布局中进行数据的回传。首先需要创建一个布局文件,首先分析一下这个布局文件可以如何编写,该使用哪种布局。

Android开发案例——装备选择_第1张图片

我们经过分析他可以由线性布局和表格布局组成,或者也可以用线性布局一种布局完成,这两个布局我全部都编写了,代码如下:

第一种:线性布局和表格布局

   xmlns:app="http://schemas.android.com/apk/res-auto"

   xmlns:tools="http://schemas.android.com/tools"

   android:id="@+id/activity_main"

   android:layout_width="match_parent"

   android:layout_height="match_parent"

   tools:context="cn.edu.bzu.test_demo5.MainActivity"

   android:orientation="vertical"

   android:layout_gravity="center">

 

   

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:id="@+id/imageView"

       android:layout_gravity="center_horizontal"

       android:layout_marginTop="30dp"

       android:layout_marginBottom="15dp"

       android:src="@drawable/timg1"

        />

   

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_gravity="center_horizontal"

       android:gravity="center"

       android:layout_marginBottom="25dp"

        android:text="主人,快给小宝宝买装备吧"/>

   

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:gravity="center"

       android:layout_marginBottom="20dp">

       

           android:layout_width="fill_parent"

           android:layout_height="wrap_content">

           

               android:layout_width="0dp"

               android:layout_height="wrap_content"

               android:layout_weight="1"

               android:textColor="@color/black"

               android:textSize="14sp"

               android:text="生命值:"

                />

           

               android:id="@+id/progressbar1"

               style="?android:attr/progressBarStyleHorizontal"

               android:layout_width="0dp"

               android:layout_height="wrap_content"

               android:layout_gravity="center"

               android:layout_weight="2"/>

           

                android:layout_width="0dp"

               android:layout_height="wrap_content"

               android:id="@+id/text1"

               android:layout_weight="1"

               android:text="0"

               android:gravity="center"

                android:textColor="#000000"/>

 

 

       

       

           android:layout_width="fill_parent"

           android:layout_height="wrap_content">

           

               android:layout_width="0dp"

               android:layout_height="wrap_content"

               android:layout_weight="1"

               android:textColor="@color/black"

               android:textSize="14sp"

               android:text="攻击力:"

                />

           

               android:id="@+id/progressbar2"

               style="?android:attr/progressBarStyleHorizontal"

               android:layout_width="0dp"

               android:layout_height="wrap_content"

               android:layout_gravity="center"

               android:layout_weight="2"/>

           

               android:layout_width="0dp"

               android:layout_height="wrap_content"

               android:id="@+id/text2"

               android:layout_weight="1"

                android:text="0"

               android:gravity="center"

               android:textColor="#000000"/>

 

 

       

       

           android:layout_width="fill_parent"

           android:layout_height="wrap_content">

            

               android:layout_width="0dp"

               android:layout_height="wrap_content"

               android:layout_weight="1"

               android:textColor="@color/black"

               android:textSize="14sp"

               android:text="敏捷:"

                />

           

               android:id="@+id/progressbar3"

               style="?android:attr/progressBarStyleHorizontal"

               android:layout_width="0dp"

                android:layout_height="wrap_content"

               android:layout_gravity="center"

               android:layout_weight="2"/>

           

               android:layout_width="0dp"

               android:layout_height="wrap_content"

                android:id="@+id/text3"

               android:layout_weight="1"

               android:text="0"

               android:gravity="center"

               android:textColor="#000000"/>

 

 

       

   

   

        android:layout_width="match_parent"

       android:layout_height="wrap_content"

       android:layout_marginLeft="50dp"

       android:layout_marginRight="50dp"

       android:layout_marginTop="20dp">

       

           android:layout_width="match_parent"

           android:layout_height="wrap_content"

           android:id="@+id/btn1"

           android:drawablePadding="3dp"

           android:drawableRight="@drawable/timg1"

           android:onClick="click"

           android:text="小宝宝购买装备"

           android:textSize="14sp"/>

   


第二种:线性布局

   android:layout_height="match_parent"

   xmlns:android="http://schemas.android.com/apk/res/android"

   android:orientation="vertical">

 

   

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:src="@drawable/timg1"

       android:layout_gravity="center_horizontal"

       android:layout_marginTop="50dp"/>

   

        android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_gravity="center_horizontal"

       android:text="快来购买装备吧!"

       android:textSize="18sp"

       android:layout_marginTop="10dp"

        android:textStyle="bold"/>

 

 

   

       android:layout_width="match_parent"

       android:layout_height="match_parent"

       android:layout_marginTop="100dp"

       android:layout_weight="1">

   

       android:layout_width="0dp"

       android:layout_height="match_parent"

       android:layout_weight="1"

       android:orientation="vertical">

       

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:text="生命值:"

           android:layout_weight="1"/>

       

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:text="攻击力:"

           android:layout_weight="1"/>

       

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:text="敏捷:"

           android:layout_weight="1"/>

 

   

 

       

            android:layout_width="0dp"

           android:layout_height="match_parent"

           android:layout_weight="1"

           android:orientation="vertical">

           

               style="?android:attr/progressBarStyleHorizontal"

                android:layout_width="match_parent"

               android:layout_height="wrap_content"

               android:layout_gravity="center"

               android:id="@+id/progressbar1"/>

           

               style="?android:attr/progressBarStyleHorizontal"

               android:layout_width="match_parent"

               android:layout_height="wrap_content"

               android:layout_gravity="center"

               android:layout_weight="1"

               android:id="@+id/progressbar2"/>

           

               style="?android:attr/progressBarStyleHorizontal"

               android:layout_width="match_parent"

               android:layout_height="wrap_content"

                android:layout_gravity="center"

               android:layout_weight="1"

               android:id="@+id/progressbar3"/>

 

       

 

       

       android:layout_width="0dp"

       android:layout_height="match_parent"

        android:layout_weight="1"

       android:orientation="vertical">

           

               android:layout_width="wrap_content"

               android:layout_height="wrap_content"

               android:text="0"

                android:layout_weight="1"

               android:layout_gravity="center"

               android:id="@+id/mlife"/>

           

               android:layout_width="wrap_content"

               android:layout_height="wrap_content"

               android:text="0"

               android:layout_weight="1"

               android:layout_gravity="center"

               android:id="@+id/mattack"/>

           

               android:layout_width="wrap_content"

                android:layout_height="wrap_content"

               android:text="0"

               android:layout_weight="1"

               android:layout_gravity="center"

               android:id="@+id/mspeed" />

 

   

   

   

       android:layout_width="match_parent"

       android:layout_height="match_parent"

       android:layout_weight="1"

       android:orientation="vertical">

       

           android:layout_width="wrap_content"

            android:layout_height="wrap_content"

           android:layout_gravity="center_horizontal"

           android:layout_marginTop="30dp"

           android:text="购买装备"

           android:drawableLeft="@drawable/timg1"

           android:onClick="click"/>

 

   

接下来编写购买界面,首先分析其布局

Android开发案例——装备选择_第2张图片

该布局主要由相对布局和线性布局构成,代码如下:

   android:orientation="vertical"

   android:layout_width="match_parent"

   android:layout_height="wrap_content"

   android:id="@+id/r1">

   android:layout_width="30dp"

   android:layout_height="30dp"

   android:background="@drawable/timg1"

   android:layout_centerVertical="true"

   android:layout_alignParentLeft="true"

    />

   

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:id="@+id/text1"

       android:layout_centerVertical="true"

       android:layout_marginLeft="60dp"

       android:text="商品名称"/>

   

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

        android:layout_centerInParent="true"

       android:orientation="vertical">

       

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:textSize="13sp"

           android:id="@+id/text2"

            android:text="生命值"/>

       

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:textSize="13sp"

           android:id="@+id/text3"

           android:text="攻击力"/>

       

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:textSize="13sp"

           android:id="@+id/text4"

           android:text="速度"/>

   

接下来编写交互代码,首先在MainActivity编写事件,初始化进度条,绑定事件,代码如下:

package cn.edu.bzu.test_demo5;


import android.app.Activity;

import android.content.Intent;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.ProgressBar;

import android.widget.TextView;

 

public class MainActivity extends Activity {

    private ProgressBarprogressBar1;    //声明进度条

    private ProgressBarprogressBar2;

    private ProgressBarprogressBar3;

    private TextViewmlife;     //声明textview

    private TextViewmattack;

    private TextViewmspeed;

    @Override

    protected void onCreate(BundlesavedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_main);

        mlife=(TextView)findViewById(R.id.text1);   //绑定控件

       mattack=(TextView) findViewById(R.id.text2);

        mspeed=(TextView)findViewById(R.id.text3);

        initProgress();    //进度条初始化函数,进行进度条的初始化

    }

    private void  initProgress(){     //进度条初始化函数的编写

       progressBar1=(ProgressBar) findViewById(R.id.progressbar1);   //进度条绑定

       progressBar2=(ProgressBar) findViewById(R.id.progressbar2);

        progressBar3=(ProgressBar)findViewById(R.id.progressbar3);

       progressBar1.setMax(1000);   //设置进度条最大数值

       progressBar2.setMax(1000);

       progressBar3.setMax(1000);

    }

    public voidclick(View view){      //click点击事件

        Intent intent=newIntent(this,shopActivity.class);

       startActivityForResult(intent,1);

    }

    @Override

    protected voidonActivityResult( int requestCode,int resultCode,Intent data){  //接受回传来的数据

       super.onActivityResult(requestCode,resultCode,data);

        if (data !=null){    

            if(resultCode==1){

                if(requestCode==1){

                   Iteminfo iteminfo=(Iteminfo)data.getSerializableExtra("equipment");

                   update(iteminfo);    //更新数据

                }

        }

 

    }

}

private  voidupdate(Iteminfo iteminfo){      //更新函数的编写

    intprogress1=progressBar1.getProgress();    //得到进度条

    intprogress2=progressBar2.getProgress();

    intprogress3=progressBar3.getProgress();

   progressBar1.setProgress(progress1+iteminfo.getLife());    //进行更新数据更新

    progressBar2.setProgress(progress2+iteminfo.getAcctack());

   progressBar3.setProgress(progress3+iteminfo.getSpeed());

   mlife.setText(progressBar1.getProgress()+"");    //textview数值更新

   mattack.setText(progressBar2.getProgress()+"");

   mspeed.setText(progressBar3.getProgress()+"");

}

}

然后编写shopActivity函数,进行交互,代码如下:

package cn.edu.bzu.test_demo5;

 

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.TextView;

import android.view.View.OnClickListener;

/**

 * Created by ASUS on2017/3/19.

 */

 

public class shopActivity extends Activity implementsOnClickListener {

    private Iteminfoiteminfo;

    @Override

    protected voidonCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.shop);

        iteminfo = newIteminfo("金剑", 100, 20, 20);

       findViewById(R.id.r1).setOnClickListener(this);

        TextView mlife =(TextView) findViewById(R.id.text2);

        TextView mname = (TextView)findViewById(R.id.text1);

        TextView mspeed =(TextView) findViewById(R.id.text4);

        TextView mattack= (TextView) findViewById(R.id.text3);

       mlife.setText("生命值+" + iteminfo.getLife());

        mname.setText(iteminfo.getName()+ "");

       mspeed.setText("敏捷度+" + iteminfo.getSpeed());

       mattack.setText("攻击力+" + iteminfo.getAcctack());

 

    }

 

    @Override

    public voidonClick(View v) {  //click函数编写

        switch(v.getId()) {

            case R.id.r1:

                Intentintent = new Intent();

               intent.putExtra("equipment", iteminfo);

               setResult(1, intent);

                finish();

                break;

        }

    }

}

进行 Iteminfo 的交互编写,代码如下:

package cn.edu.bzu.test_demo5;

 

import java.io.Serializable;

 

/**

 * Created by ASUS on2017/3/18.

 */

 

public class Iteminfo implements Serializable {

    private String name;

    private int acctack;

    private int life;

    private  int speed;

    public  Iteminfo(String name,int  acctack,int life,int speed){    //get,set方法编写

        this.name=name;

       this.acctack=acctack;

        this.life=life;

        this.speed=speed;

 

    }

    public StringgetName(){

        return name;

    }

    public voidsetName(){

        this.name=name;

    }

    public intgetAcctack(){

        return acctack;

    }

    public voidsetAcctack(int acctack){

       this.acctack=acctack;

    }

    public int getLife(){

        return life;

    }

    public voidsetLife(int life){

        this.life=life;

    }

    public intgetSpeed(){

        return speed;

    }

    public voidsetSpeed(int speed){

        this.speed=speed;

    }

    public StringtoString(){

        return"[name="+name+",acctack="+acctack+",life="+life+",speed="+speed+"]";

    }

}

至此已完成装备选择的全部代码编写。

在模拟器运行如下图:

Android开发案例——装备选择_第3张图片

Android开发案例——装备选择_第4张图片

Android开发案例——装备选择_第5张图片

你可能感兴趣的:(Andriod)