Android 小宝宝买装备案列创建

实现如下图所示的小宝宝买装备案例:


一、先需要实现一下页面,运用各种布局和控件 代码如下

第一个页面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="cn.edu.bzu.test1.MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="45dp"
        android:src="@drawable/baby" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="主人快给小宝宝购买装备吧!"
        android:textSize="18sp" />


    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:gravity="center"
        >
       <!-- 表格中第一行-->
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="生命值:" />

            <ProgressBar
                android:id="@+id/progressBar1"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_weight="3" />

            <TextView
                android:id="@+id/tv_life"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="5dp"
                android:text="80" />

        </TableRow>

        <!-- 表格中第二行-->
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            >

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

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

            <TextView
                android:id="@+id/tv_atk"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginTop="5dp"
                android:layout_marginLeft="20dp"
                android:text="80" />

        </TableRow>
        <!-- 表格中第三行-->
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            >

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="敏捷:" />

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

            <TextView
                android:id="@+id/tv_quick"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="5dp"
                android:text="80" />

        </TableRow>

    </TableLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="25dp"
        >
        <Button
            android:onClick="click1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:drawableRight="@android:drawable/ic_menu_add"
            android:text="主人购买装备"
            />
        <Button
            android:onClick="click2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:drawableRight="@android:drawable/ic_menu_add"
            android:text="小宝宝购买装备"
            />

    </LinearLayout>
</LinearLayout>
第二个页面:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <ImageButton
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_menu_info_details"
        />
    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="金剑"
        android:textSize="25dp"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="95dp"
        />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"

        >
        <TextView
            android:id="@+id/tv_life1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:layout_marginLeft="200dp"
        android:text="生命值+20"
        />
        <TextView
            android:id="@+id/tv_atk1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:layout_marginLeft="200dp"
            android:text="攻击力+100"
            />
        <TextView
            android:id="@+id/tv_quick1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:layout_marginLeft="200dp"
            android:text="敏捷度+20"
            />

    </LinearLayout>
</RelativeLayout>
                   
二、创建Info类 用来封装装备信息,右击空白处选择Generate然后选择Getter and Setter自动生成get set方法
同样生成实例方法 代码如下:
package cn.edu.bzu.test1;
import java.io.Serializable;
/**
 * Created by 宋港卉 on 2017/3/26.
 */
public class Info implements Serializable {
private String name; private int life; private int atk; private int quick; public Info(String name, int life, int atk, int quick) { this.name = name; this.life = life; this.atk = atk; this.quick = quick; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getLife() { return life; } public void setLife(int life) { this.life = life; } public int getAtk() { return atk; } public void setAtk(int atk) { this.atk = atk; } public int getQuick() { return quick; } public void setQuick(int quick) { this.quick = quick; }}
三、创建shoppingActivity用来展示装备信息,当单击shoppingActivity的装备时,会调回MainActivity,并将装备信息
回传给MainActivity,代码如下:
package cn.edu.bzu.test1;
import android.app.Activity;
import android.content.Intent;
import android.icu.text.IDNA;
import android.icu.text.RelativeDateTimeFormatter;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
 * Created by 宋港卉 on 2017/3/26.
 */
public class ShoppingActivity extends Activity implements View.OnClickListener {
    Info  info;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //加载布局
        setContentView(R.layout.activity_shop);
        findViewById(R.id.rl).setOnClickListener(this);
        //(1)初始化显示到界面上的数据
        info=new Info("金剑",20,100,20);
        //(2)找到控件,显示数据
        TextView tv_name=(TextView)findViewById(R.id.tv_name);
        TextView tv_life=(TextView)findViewById(R.id.tv_life1);
        TextView tv_atk=(TextView)findViewById(R.id.tv_atk1);
        TextView tv_quick=(TextView)findViewById(R.id.tv_quick1);
        //(3)初始化数据 展示到控件上
        tv_name.setText(info.getName());
        tv_life.setText("生命力:"+info.getLife());
        tv_atk.setText("攻击力:"+info.getAtk());
        tv_quick.setText("敏捷度:"+info.getQuick());
    }
    @Override
    public void onClick(View v){
        //具体判断一下点击的谁
        switch (v.getId()){
            case R.id.rl://证明我们点击的就是这个布局
                //(1)获取当前的数据 把info数据返回
                Intent intent =new Intent();
                intent.putExtra("info",info);
                //把结果返回给调用者(MainActivity)通过onActivityReult方法返回
                setResult(10,intent);
                //(2)关闭当前页面通过onActivityReult方法返回
                finish();
                break;
            default:
                break;
        }
    }
}
四、在MainActivity中编写界面交互代码,主要用于响应按钮的点击事件,并将返回的装备信息显示到指定的控件中
代码如下:
package cn.edu.bzu.test1;
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 AppCompatActivity {
    private ProgressBar pb1;
    private ProgressBar pb2;
    private  ProgressBar pb3;
    private TextView tv_life;
    private TextView tv_atk;
    private TextView tv_quick;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //(1).找到我们关心的控件
        pb1=(ProgressBar) findViewById(R.id.progressBar1);
         pb2=(ProgressBar) findViewById(R.id.progressBar2);
         pb3=(ProgressBar) findViewById(R.id.progressBar3);

         tv_life=(TextView) findViewById(R.id.tv_life);
         tv_atk=(TextView) findViewById(R.id.tv_atk);
         tv_quick=(TextView) findViewById(R.id.tv_quick);

        //(2)初始化一下进度条的最大值
        pb1.setMax(1000);
        pb2.setMax(1000);
        pb3.setMax(1000);


    }
    //点击按钮,跳转到另一个shopping页面进行购买装备
    public  void click1(View v){
        Intent intent=new Intent(this,ShoppingActivity.class);
        //开启一个页面 并且要开启这个页面的返回数据
        startActivityForResult(intent,1);

    }
    //当我们开启的Activity 关闭的时候调用 这个方法
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode==10){
            //(1)代表数据来源于shoppingActivity 取数据
           Info info=(Info) data.getExtras().get("info");
            //(2)更新一下ui
            updateProgressBar(info);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
    //更新当前控件的ui
    private void updateProgressBar(Info info){
        //(1)获取当前Progress的进度
       int progress1= pb1.getProgress();
        int progress2= pb2.getProgress();
        int progress3= pb3.getProgress();

        //(2)更新一下progressbar的进度
        pb1.setProgress(progress1+info.getLife());
        pb2.setProgress(progress2+info.getAtk());
        pb3.setProgress(progress3+info.getQuick());
        //(3)更新textview的值
        tv_life.setText(pb1.getProgress()+"");
        tv_atk.setText(pb2.getProgress()+"");
        tv_quick.setText(pb3.getProgress()+"");
    }
}
五、在清单文件中配置一下:红底为配置加的一句代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.edu.bzu.test1">


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!--配置activity-->
        <activity android:name=".ShoppingActivity"></activity>
    </application>


</manifest>

最终运行结果如下图:点击主人购买装备出现第二个界面,点击后返回第一个界面,并且第一个界面
                    中进度条发生变化。


(运行过程中,我点击主人购买装备后打不开第二个界面,第一个自动关闭出现错误,下面出现的
红色错误提示,应该从下网上看,经检查有的控件没有写上id,所以导致找不到出现错误,加上id
后程序成功运行)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值