装备选择案例
2017-03-28 19:18
330 查看
1.创建程序
创建一个名为“装备选择”的工程,设计用户交互界面:
布局代码中使用到了控件ProgressBar(进度条),他是用来显示人物的生命值、攻击力和敏捷度。他有两种表现形式,一种是水平的,另一种是环形的。几种常用的方法属性:
style属性:控制ProgressBar的表现形式,水平进度条需设置style的属性值为“?android:attr/ProgressBarStyleHorizontal”,环形进度条需设置的属性值为“?android:attr/ProgressBarStyleLargel”。
setMax()方法:设置进度条的最大值。
setProgress()方法:设置当前进度。
getProgress():获取当前进度。
2.创建装备界面
创建装备界面activity_shop.xml,该界面是用来展示装备的,购买装备界面相应的布局文件如下:
3.创建ItemInfo类
代码:
4.创建ShopActivity
ShopActivity是用来展示装备信息的,当单击ShopActivity的装备时,回调会MainActivity并将装备信息回传给MainActivity。代码:
5.编写界面交互代码(MainActivity)
主要用于响应按钮的点击事件,并将返回装备信息显示到指定的ListView控件中,代码:
6.配置清单文件
代码:
创建一个名为“装备选择”的工程,设计用户交互界面:
<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" > <ImageView android:layout_width="164dp" android:layout_height="155dp" android:layout_gravity="center_horizontal" android:layout_marginTop="25dp" android:src="@mipmap/xiaoren" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="主人,快给小宝宝买装备吧" android:layout_gravity="center_horizontal"/> <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:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:text="生命值:" /> <ProgressBar style="?android:attr/progressBarStyleHorizontal" android:layout_width="0dp" android:layout_weight="3" android:layout_marginLeft="20dp" android:layout_height="wrap_content" android:id="@+id/progressBar3" /> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:text="80" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content" android[b]:layout_marginTop="15dp" > <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:text="攻擊力:" /> <ProgressBar style="?android:attr/progressBarStyleHorizontal" android:layout_width="0dp" android:layout_weight="3" android:layout_marginLeft="20dp" android:layout_height="wrap_content" android:id="@+id/progressBar4" /> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:text="200" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" > <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:text="敏捷:" /> <ProgressBar style="?android:attr/progressBarStyleHorizontal" android:layout_width="0dp" android:layout_weight="3" android:layout_marginLeft="20dp" android:layout_height="wrap_content" android:id="@+id/progressBar5" /> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:text="40" /> </TableRow> </TableLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp"> <Button android:id="@+id/btn_master" android:onClick="click1" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:text="主人購買裝備" android:layout_marginTop="15dp" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <Button android:id="@+id/btn_baby" android:onClick="click2" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="小寶寶購買裝備" android:layout_marginTop="15dp" /> </LinearLayout> </LinearLayout>
布局代码中使用到了控件ProgressBar(进度条),他是用来显示人物的生命值、攻击力和敏捷度。他有两种表现形式,一种是水平的,另一种是环形的。几种常用的方法属性:
style属性:控制ProgressBar的表现形式,水平进度条需设置style的属性值为“?android:attr/ProgressBarStyleHorizontal”,环形进度条需设置的属性值为“?android:attr/ProgressBarStyleLargel”。
setMax()方法:设置进度条的最大值。
setProgress()方法:设置当前进度。
getProgress():获取当前进度。
2.创建装备界面
创建装备界面activity_shop.xml,该界面是用来展示装备的,购买装备界面相应的布局文件如下:
<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"> <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_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="13sp" android:text="生命值:" /> <TextView android:id="@+id/tv_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="13sp" android:text="攻击力:" android:layout_marginTop="5dp"/> <TextView android:id="@+id/tv_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="13sp" android:text="敏 捷:" android:layout_marginTop="5dp"/> </LinearLayout> </RelativeLayout>
3.创建ItemInfo类
代码:
public class ItemInfo implements Serializable{ private String name; private int life; private int atk; private int quick; public ItemInfo(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; } }
4.创建ShopActivity
ShopActivity是用来展示装备信息的,当单击ShopActivity的装备时,回调会MainActivity并将装备信息回传给MainActivity。代码:
public class Shopping extends AppCompatActivity implements View.OnClickListener{ private ItemInfo itemInfo; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shopping); itemInfo = new ItemInfo("金剑", 100, 20, 20); findViewById(R.id.rl).setOnClickListener(this); TextView mLifeTV = (TextView) findViewById(R.id.tv_1); TextView mNameTV = (TextView) findViewById(R.id.tv_name); TextView mSpeedTV = (TextView) findViewById(R.id.tv_3); TextView mAttackTV = (TextView) findViewById(R.id.tv_2); mLifeTV.setText("生命值+" + itemInfo.getLife()); mNameTV.setText(itemInfo.getName() + ""); mSpeedTV.setText("敏捷度+" + itemInfo.getQuick()); mAttackTV.setText("攻击力+" + itemInfo.getAtk()); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.rl: Intent intent = new Intent(); intent.putExtra("equipment", itemInfo); setResult(1, intent); finish(); break; } } }
5.编写界面交互代码(MainActivity)
主要用于响应按钮的点击事件,并将返回装备信息显示到指定的ListView控件中,代码:
public class MainActivity extends AppCompatActivity { private ProgressBar mProgressBar1; private ProgressBar mProgressBar2; private ProgressBar mProgressBar3; private TextView mLifeTV; private TextView mAttackTV; private TextView mSpeedTV; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mLifeTV = (TextView) findViewById(R.id.tv_life); mAttackTV = (TextView) findViewById(R.id.tv_atk); mSpeedTV = (TextView) findViewById(R.id.tv_quick); initProgress(); } private void initProgress() { mProgressBar1 = (ProgressBar) findViewById(R.id.pro1); mProgressBar2 = (ProgressBar) findViewById(R.id.pro2); mProgressBar3 = (ProgressBar) findViewById(R.id.pro3); mProgressBar1.setMax(1000); mProgressBar2.setMax(1000); mProgressBar3.setMax(1000); } public void click_1(View view) { Intent intent = new Intent(this, Shopping.class); startActivityForResult(intent, 1); } public void click_2(View view) { Intent intent = new Intent(this, Shopping.class); startActivityForResult(intent, 1); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (data != null) { if (resultCode == 1) { if (requestCode == 1) { ItemInfo info= (ItemInfo) data.getSerializableExtra("equipment"); updateProgress(info); } } } } private void updateProgress(ItemInfo info) { int progress1 = mProgressBar1.getProgress(); int progress2 = mProgressBar2.getProgress(); int progress3 = mProgressBar3.getProgress(); mProgressBar1.setProgress(progress1+info.getLife()); mProgressBar2.setProgress(progress2+info.getAtk()); mProgressBar3.setProgress(progress3+info.getQuick()); mLifeTV.setText(mProgressBar1.getProgress()+""); mAttackTV.setText(mProgressBar2.getProgress() + ""); mSpeedTV.setText(mProgressBar3.getProgress() + ""); } @Override public boolean onCreateOptionsMenu(Menu menu) { return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); return super.onOptionsItemSelected(item); } }
6.配置清单文件
代码:
<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=".Shopping"></activity> </application>
相关文章推荐
- 装备选择的案例
- 案例--装备选择
- Android---装备选择(案例)
- 实验:Android案例——小宝宝装备选择
- 装备选择案例
- 案例——装备选择
- Android装备选择案例
- 装备选择案例
- 开发小游戏之(案例——装备选择)
- Android中Activity的跳转与数据的传递案例(装备的选择)
- 装备选择案例
- 装备选择上(案例)
- Andriod--Activity练习之装备选择案例
- 案例_装备选择
- 装备选择案例
- Android两个android两个activity之间相互传递数据之装备选择(书上案例)
- 课本案例--装备选择
- Activity的数据跳转回传案例——装备选择案例
- Activity跳转(装备选择案例)
- Android装备选择案例