这次做的是一个购买装备并显示属性加成的案例,一个是购买界面,一个是装备属性界面。
属性加成由progressbar显示,还有就是数值变化。
1、装备购买界面如下:
代码如下
<?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:id="@+id/activity_two" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.administrator.weightjisuanqi.mainActivity"> <ImageView android:layout_width="100dp" android:layout_height="60dp" android:layout_gravity="center_horizontal" android:layout_marginTop="150dp" android:src="@drawable/shangdian"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="主人给我一把无尽吧" android:gravity="center_horizontal" android:textSize="20dp" /> <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" > <TableRow android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="生命值" /> <ProgressBar style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_weight="3" android:layout_marginLeft="20dp" android:layout_height="wrap_content" android:id="@+id/progressBar1" /> <TextView android:layout_width="0dp" android:id="@+id/tv_life" android:layout_height="wrap_content" android:layout_weight="1" android:text="495" android:layout_marginLeft="20dp" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="5dp"> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="攻击力" /> <ProgressBar style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_weight="3" android:layout_marginLeft="20dp" android:layout_height="wrap_content" android:id="@+id/progressBar2" /> <TextView android:layout_width="0dp" android:id="@+id/tv_atk" android:layout_height="wrap_content" android:layout_weight="1" android:text="70" android:layout_marginLeft="20dp" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="5dp"> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="暴击" /> <ProgressBar style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_weight="3" android:layout_marginLeft="20dp" android:layout_height="wrap_content" android:id="@+id/progressBar3" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="0" android:layout_marginLeft="20dp" android:id="@+id/tv_bang" /> </TableRow> </TableLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:onClick="onClick1" android:layout_width="0dp" android:layout_marginTop="10dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="出门装"/> <Button android:onClick="onclick2" android:layout_width="0dp" android:layout_marginTop="10dp" android:layout_height="match_parent" android:layout_weight="1" android:text="暴击"/> </LinearLayout> </LinearLayout>
2、装备属性界面
代码如下:
3、<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/rl"> <ImageView android:layout_width="100dp" android:layout_height="100dp" android:background="@drawable/duolanjian" android:layout_gravity="center_horizontal"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="多兰剑" android:textSize="50dp" android:layout_marginTop="20dp" android:gravity="center"/> <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TableRow> <TextView android:id="@+id/tv_name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="生命值" android:textSize="20dp" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="80" android:textSize="20dp" android:layout_marginTop="20dp" /> </TableRow> <TableRow> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="攻击力" android:textSize="20dp" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="7" android:textSize="20dp" android:layout_marginTop="20dp" /> </TableRow> <TableRow> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="暴击" android:textSize="20dp" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="3" android:textSize="20dp" android:layout_marginTop="20dp" /> </TableRow> </TableLayout> </LinearLayout>
运行程序后先点击按钮一,需要跳转到装备属性界面,并且还要返回装备属性的值,用来更新progressbar的值
需要用到startActivityForResult()方法
代码如下:
public class mainActivity extends AppCompatActivity { private ProgressBar pbs1; private ProgressBar pbs2; private ProgressBar pbs3; private TextView tv_life; private TextView tv_atk; private TextView tv_bang; private TextView tv_name; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_two); //1、 找到需要的控件; pbs1=(ProgressBar)findViewById(R.id.progressBar1); pbs2=(ProgressBar)findViewById(R.id.progressBar2); pbs3=(ProgressBar)findViewById(R.id.progressBar3); tv_life=(TextView)findViewById(R.id.tv_life); tv_name=(TextView) findViewById(R.id.tv_name); tv_atk=(TextView)findViewById(R.id.tv_atk); tv_bang=(TextView)findViewById(R.id.tv_bang); //2、初始化属性的最大值 pbs1.setMax(10000); pbs2.setMax(300); pbs3.setMax(100); } public void onClick1(View v){ Intent intent=new Intent(this,shoppingActivity.class); //开启一个页面并返回值 startActivityForResult(intent,1); } //当我们关闭Activity时调用这个方法 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode==10){ //代表数据来源于shoppingactivity Info info=(Info)data.getExtras().get("info"); //更新一下ui updateProgressBar(info); } } //更新当前控件的ui public void updateProgressBar(Info info){ //获取当前progressBar的进度 int progress1=pbs1.getProgress(); int progress2=pbs2.getProgress(); int progress3=pbs3.getProgress(); //更新一下progressBar的进度 pbs1.setProgress(progress1+info.getLife()); pbs2.setProgress(progress2+info.getAtk()); pbs3.setProgress(progress3+info.getBang()); //更新一下textview的值 tv_life.setText(495+pbs1.getProgress()+""); tv_atk.setText(70+pbs2.getProgress()+""); tv_bang.setText(pbs3.getProgress()+""); } }4、装备属性放到一个java文件中 我的叫Info
构造了get,set方法
代码如下:
public class Info implements Serializable{
private String name; private int life; private int atk; private int bang; public Info(String name, int life, int atk, int bang) { this.name = name; this.life = life; this.atk = atk; this.bang = bang; } public int getLife() { return life; } public void setLife(int life) { this.life = life; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAtk() { return atk; } public void setAtk(int atk) { this.atk = atk; } public int getBang() { return bang; } public void setBang(int bang) { this.bang = bang; } }
5、最后是连接mainActivity和shopping界面的java文件
代码如下:
public class shoppingActivity extends AppCompatActivity implements View.OnClickListener { private Info info; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.shoping); findViewById(R.id.rl).setOnClickListener(this); //1、初始化显示到页面的数据 // info=new Info("多兰剑",80,7,3); //2、找到控件,显示数据 TextView tv_life=(TextView)findViewById(R.id.tv_life); TextView tv_atk=(TextView)findViewById(R.id.tv_atk); TextView tv_bang=(TextView)findViewById(R.id.tv_bang); TextView tv_name=(TextView)findViewById(R.id.tv_name); //3、初始化数据 显示到控件上 // tv_name.setText(info.getName()); // tv_atk.setText("攻击力"+info.getAtk()); // tv_life.setText("生命值"+info.getLife()); // tv_bang.setText("暴击"+info.getBang()); } @Override public void onClick(View v) { //具体判断一下点击的谁 switch (v.getId()){ case R.id.rl: //证明我们点击的是这个布局 //获取当前数据 把info数据返回 Intent intent=new Intent(); intent.putExtra("info",info); //把结果返回给mainActivity 通过onActivityResult方法返回 setResult(10,intent); //关闭当前页面 finish(); break; default: break; } } }
6、运行结果如图:
1、购买装备前
2、购买装备后