装备选择

来源:互联网 发布:织梦cms免费模板 编辑:程序博客网 时间:2024/04/29 19:25

1.先创建一个Android项目:


2.创建activity_main.xml:


3.创建activity_shop.xml:


4.创建MainActivity.java:


5.创建ShopppingActivity.java:


6.创建info.java:


7.创建AndroidManifest.xml:

7.在drawable下放置一个bb.jpg图片:


文件代码如下:

1.activity_main.xml中的代码:

<?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=".MainActivity">    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:layout_marginTop="65dp"        android:src="@drawable/bb" />    <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">        <TableRow            android:layout_width="match_parent"            android:layout_height="wrap_content">            <TextView                android:id="@+id/tv_life"                android:layout_width="0dp"                android:layout_weight="1"                android:layout_height="wrap_content"                android:layout_marginLeft="10dp"                android:text="生命值:" />            <ProgressBar                android:id="@+id/progressBar1"                style="?android:attr/progressBarStyleHorizontal"                android:layout_width="0dp"                android:layout_weight="3"                android:layout_height="wrap_content"                android:layout_marginLeft="20dp"                />            <TextView                android:layout_width="0dp"                android:layout_weight="1"                android:layout_height="wrap_content"                android:layout_marginLeft="20dp"                android:text="80" />        </TableRow>    </TableLayout>    <TableLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="25dp">        <TableRow            android:layout_width="match_parent"            android:layout_height="wrap_content">            <TextView                android:id="@+id/tv_attack"                android:layout_width="0dp"                android:layout_weight="1"                android:layout_height="wrap_content"                android:layout_marginLeft="10dp"                android:text="攻击力:" />            <ProgressBar                android:id="@+id/progressBar2"                style="?android:attr/progressBarStyleHorizontal"                android:layout_width="0dp"                android:layout_weight="3"                android:layout_height="wrap_content"                android:layout_marginLeft="20dp"                />            <TextView                android:layout_width="0dp"                android:layout_weight="1"                android:layout_height="wrap_content"                android:layout_marginLeft="20dp"                android:text="80" />        </TableRow>    </TableLayout>    <TableLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="25dp">        <TableRow            android:layout_width="match_parent"            android:layout_height="wrap_content">            <TextView                android:id="@+id/tv_speed"                android:layout_width="0dp"                android:layout_weight="1"                android:layout_height="wrap_content"                android:layout_marginLeft="10dp"                android:text="敏捷:" />            <ProgressBar                android:id="@+id/progressBar3"                style="?android:attr/progressBarStyleHorizontal"                android:layout_width="0dp"                android:layout_weight="3"                android:layout_height="wrap_content"                android:layout_marginLeft="20dp"                />            <TextView                android:layout_width="0dp"                android:layout_weight="1"                android:layout_height="wrap_content"                android:layout_marginLeft="20dp"                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:drawablePadding="3dp"            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:drawablePadding="3dp"            android:drawableRight="@android:drawable/ic_menu_add"            android:text="小宝宝购买装备"/>    </LinearLayout></LinearLayout>
2.activity_shop.xml中文件代码:
<?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="match_parent"    android:orientation="vertical">    <View        android:layout_width="40dp"        android:layout_height="40dp"        android:layout_alignParentLeft="true"        android:layout_marginTop="20dp"        android:background="@android:drawable/ic_menu_info_details" />    <TextView        android:id="@+id/tv_name"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="60dp"        android:layout_marginTop="30dp"        android:text="商品名称"        android:textSize="20sp" />    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_marginTop="10dp"        android:orientation="vertical">        <TextView            android:id="@+id/tv_life"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="生命值" />        <TextView            android:id="@+id/tv_attack"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="攻击力" />        <TextView            android:id="@+id/tv_speed"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="敏捷度" />    </LinearLayout></RelativeLayout>
3.MainActivity.java中的代码:

package cn.edu.bzu.sy1;import android.app.Activity;import android.content.DialogInterface;import android.content.Intent;import android.content.res.Resources;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.ProgressBar;import android.widget.RelativeLayout;import android.widget.TextView;import cn.edu.bzu.domain.Info;public class MainActivity extends AppCompatActivity{    private ProgressBar pb1;    private ProgressBar pb2;    private ProgressBar pb3;    private TextView tv_life;    private TextView tv_attack;    private TextView tv_speed;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //找到我们关心的控件        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_attack = (TextView) findViewById(R.id.tv_attack);        tv_speed = (TextView) findViewById(R.id.tv_speed);        //初始化一下进度条的最大值        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)获取当前progresBar的进度        int progress1 = pb1.getProgress();        int progress2 = pb2.getProgress();        int progress3 = pb3.getProgress();        //(2)更新一下ProgressBar的进度        pb1.setProgress(progress1+info.getLife());        pb2.setProgress(progress2+info.getAttack());        pb2.setProgress(progress3+info.getSpeed());        //(3)        tv_life.setText(pb1.getProgress()+"");        tv_attack.setText(pb2.getProgress()+"");        tv_speed.setText(pb3.getProgress()+"");    }}
4.ShoppingActivity.java中的代码:

package cn.edu.bzu.sy1;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.TextView;import cn.edu.bzu.domain.Info;public class ShoppingActivity extends Activity implements View.OnClickListener {    private Info info;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //加载布局        setContentView(R.layout.activity_shop);        findViewById(R.id.rl).setOnClickListener(this);        //初始化加载到页面上的数据        info = new Info("金剑", 20, 100, 20);        //找到控件,显示数据        TextView tv_name = (TextView) findViewById(R.id.tv_name);        TextView tv_life = (TextView) findViewById(R.id.tv_life);        TextView tv_attack = (TextView) findViewById(R.id.tv_attack);        TextView tv_speed = (TextView) findViewById(R.id.tv_speed);        //初始化一下数据,展示到控件上        tv_name.setText("info.getName()");        tv_attack.setText("攻击力:" + info.getAttack());        tv_life.setText("生命值:" + info.getLife());        tv_speed.setText("敏捷度:" + info.getSpeed());    }    public void onBackPressed(){        System.out.println("我点了回退按钮");        super.onBackPressed();    }    @Override    public void onClick(View v) {        //就具体判断一下点击的谁        switch (v.getId()) {            case R.id.rl://证明我们点击的就是这个布局                //(1)获取当前数据 把info数据返回                Intent intent = new Intent();                intent.putExtra("info", info);                //把结果返回给调用者(MainActivity) 通过onActivityResult()方法返回                setResult(10, intent);                //(2)关闭页面,通过onActivityResult()方法把数据返回                finish();                break;            default:                break;        }    }}
5.AndroidManifest.xml中的代码:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="cn.edu.bzu.sy1">    <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 android:name=".ShoppingActivity"></activity>    </application></manifest>
6.Info.java中的代码:

public class Info implements Serializable {    private String name;    private int  life;    private int attack;    private int speed;    public  Info(String name,int life,int attack,int speed){        this.name = name;        this.attack = attack;        this.life = life;        this.speed = speed;    }    public String getName(){        return name;    }    public void setName(String name ){        this.name = name;    }    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 int getSpeed(){        return speed;    }    public void setSpeed(int speed){        this.speed = speed;    }}






0 0
原创粉丝点击