作者:占志杰
原文链接
demo源码地址
最近在学习Android开发,和学习其他任何语言一样,不管理论上学得多扎实,最后都一定要付诸实践,不然很容易忘掉,而且也仅仅停留在皮毛上,记录下自己练手的APP的详细实践过程,方便以后忘了回过头来复习。
这个APP的内容主要包括:APP图标的设置,名称设置,用户界面简单实现,宠物属性值的进度条展示,宠物装备的购买等等。
美中不足的是还没学到数据存储,每次进去都是初始化的数据,只能以后学到了,有时间更新一下。
APP图标,名称的设置;
intent 在不同Activity之间的跳转与传值
(startActivityForResult()与onActivityResult()与setResult()参数分析,activity带参数的返回);
简单的线性布局;
进度条ProgressBar的使用;
目录 |
---|
1.图标设置 |
2.设置APP名称 |
3.用户登录 |
4.信息显示 |
5.装备购买 |
6.颜色素材 |
扩展: startActivityForResult |
扩展: onActivityResult |
扩展: setResult |
返回目录
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.MyTest"
minSdkVersion 23
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
返回目录
返回目录
新建一个布局Layout 命名为: activity_user_login.xml
为登录按钮 设置监听事件,一旦点击就调用passData()
利用 intent 来进行 进行Activity之间的跳转与数据传值的
扩展:
startActivityForResult(Intent intent, int requestCode);
第一个参数:一个Intent对象,用于携带将跳转至下一个界面中使用的数据,使用putExtra(A,B)方法,此处存储的数据类型特别多,基本类型全部支持。
第二个参数:如果> = 0,当Activity结束时requestCode将归还在onActivityResult()中。以便确定返回的数据是从哪个Activity中返回,用来标识目标activity。
与下面的resultCode功能一致,感觉Android就是为了保证数据的严格一致性特地设置了两把锁,来保证数据的发送,目的地的严格一致性。
代码如下:
对应着,新建一个Activity,命名为: User_Login
监听登录按钮
获取用户名,密码的数据,并用 intent 跳转Activity和传值
返回目录
startActivityForResult(Intent intent, int requestCode);
第一个参数:一个Intent对象,用于携带将跳转至下一个界面中使用的数据,使用putExtra(A,B)方法,此处存储的数据类型特别多,基本类型全部支持。
第二个参数:如果 requestCode > = 0,当Activity结束时requestCode将归还在onActivityResult()中。以便确定返回的数据是从哪个Activity中返回,用来标识目标activity。
代码如下:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class User_Login extends AppCompatActivity {
private EditText et_password;
private Button btn_send;
private EditText et_name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_login);
et_name = (EditText)findViewById(R.id.et_name);
et_password = (EditText)findViewById(R.id.et_password);
btn_send = (Button)findViewById(R.id.btn_send);
btn_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
passData();
}
});
}
public void passData(){
Intent intent=new Intent(this,Show_Message.class);
intent.putExtra("name",et_name.getText().toString().trim());
intent.putExtra("password",et_password.getText().toString().trim());
startActivity(intent);
}
}
返回目录
对应着,新建一个Activity,命名为: Show_Message
初始化进度条和文本编辑组件,获取 intent 里面的用户登录信息展示出来,并设置进度条的最大值和置零
对商店购物按钮进行监听,设置 intent
重写onActivityResult()方法,获取从其他Activity跳转回来所带的数据
获取到数据后,对属性进度框进行更新,自定义方法updateProgress()
返回目录
onActivityResult(int requestCode, int resultCode, Intent data)
第一个参数:这个整数requestCode用于与startActivityForResult中的requestCode中值进行比较判断,是以便确认返回的数据是从哪个Activity返回的。
第二个参数:这整数resultCode是由子Activity通过其setResult()方法返回。适用于多个activity都返回数据时,来标识到底是哪一个activity返回的值。
第三个参数:一个Intent对象,带有返回的数据。可以通过data.getXxxExtra( );方法来获取指定数据类型的数据。
代码如下:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
public class Show_Message extends AppCompatActivity {
private TextView u_name;
private TextView u_password;
private ProgressBar mProgressBar1;
private ProgressBar mProgressBar2;
private ProgressBar mProgressBar3;
private TextView mLifeTV;
private TextView mAttackTV;
private TextView mSpeedTV;
private ProgressBar mProgressBar4;
private ProgressBar mProgressBar5;
private ProgressBar mProgressBar6;
private TextView mBrainTV;
private TextView mQualityTV;
private TextView mAbilityTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_message);
Intent intent = getIntent();
String name = intent.getStringExtra("name");
String password = intent.getStringExtra("password");
u_name = (TextView)findViewById(R.id.u_name);
u_password = (TextView)findViewById(R.id.u_password);
u_name.setText("用 户:"+name);
u_password.setText("密 码:"+password);
mLifeTV = (TextView)findViewById(R.id.tv_life_progress);
mAttackTV = (TextView)findViewById(R.id.tv_attack_progress);
mSpeedTV = (TextView)findViewById(R.id.tv_speed_progress);
mBrainTV = (TextView)findViewById(R.id.tv_brain_progress);
mQualityTV = (TextView)findViewById(R.id.tv_quality_progress);
mAbilityTV = (TextView)findViewById(R.id.tv_ability_progress);
initProgress();
}
private void initProgress(){
mProgressBar1 = (ProgressBar)findViewById(R.id.progressBar1);
mProgressBar2 = (ProgressBar)findViewById(R.id.progressBar2);
mProgressBar3 = (ProgressBar)findViewById(R.id.progressBar3);
mProgressBar1.setMax(1000);
mProgressBar2.setMax(1000);
mProgressBar3.setMax(1000);
mProgressBar4 = (ProgressBar)findViewById(R.id.progressBar4);
mProgressBar4.setMax(100);
mProgressBar5 = (ProgressBar)findViewById(R.id.progressBar5);
mProgressBar5.setMax(100);
mProgressBar6 = (ProgressBar)findViewById(R.id.progressBar6);
mProgressBar6.setMax(100);
}
public void click(View view){
Intent intent = new Intent(this,Shop_Device.class);
startActivityForResult(intent,1);
}
public void click2(View view){
Intent intent2 = new Intent(this,For_Study.class);
startActivityForResult(intent2,1);
}
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);
if(data!=null){
if(requestCode==1){
if(resultCode==1){
ItemInfo info = (ItemInfo)data.getSerializableExtra("equipment");
updateProgress(info);
}else{
Subject study = (Subject)data.getSerializableExtra("study");
updateProgress2(study);
}
}
}
}
private void updateProgress2(Subject study){
int progress4 = mProgressBar4.getProgress();
mProgressBar4.setProgress(progress4+study.getBrain());
mBrainTV.setText(mProgressBar4.getProgress()+"");
int progress5 = mProgressBar5.getProgress();
mProgressBar5.setProgress(progress5+study.getQuality());
mQualityTV.setText(mProgressBar5.getProgress()+"");
int progress6 = mProgressBar6.getProgress();
mProgressBar6.setProgress(progress6+study.getAbility());
mAbilityTV.setText(mProgressBar6.getProgress()+"");
}
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.getAcctack());
mProgressBar3.setProgress(progress3+info.getSpeed());
mLifeTV.setText(mProgressBar1.getProgress()+"");
mAttackTV.setText(mProgressBar2.getProgress()+"");
mSpeedTV.setText(mProgressBar3.getProgress()+"");
}
}
返回目录
为装备添加一个item类,存放装备的各种属性:ItemInfo
属性有名称,攻击属性,生命属性,速度属性,赋予get和set方法,并序列化
代码如下:
import java.io.Serializable;
public class ItemInfo implements Serializable {
private String name;
private int acctack;
private int life;
private int speed;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAcctack() {
return acctack;
}
public void setAcctack(int acctack) {
this.acctack = acctack;
}
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;
}
public ItemInfo(String name, int acctack, int life, int spped){
this.name = name;
this.acctack = acctack;
this.life = life;
this.speed = spped;
}
}
对应着,新建一个Activity,命名为: Shop_Device
初始化装备属性,并展示到界面上
为每个item的LinearLayout设置点击监听事件
获取被点击的装备数据,重写setResult(),将数据存入 intent ,并跳转回原展示界面
返回目录
setResult(int resultCode, Intent data)
在意图跳转的目的地界面调用这个方法把Activity想要返回的数据返回到主Activity,
第一个参数:当Activity结束时resultCode将归还在onActivityResult()中,一般为RESULT_CANCELED , RESULT_OK该值默认为-1。
第二个参数:一个Intent对象,返回给主Activity的数据。在intent对象携带了要返回的数据,使用putExtra( )方法。
代码如下:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class Shop_Device extends AppCompatActivity implements View.OnClickListener {
private ItemInfo itemInfo;
private ItemInfo itemInfo2;
private ItemInfo itemInfo3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shop_device);
// 初始化 装备属性
itemInfo = new ItemInfo("金剑",100,20,20);
itemInfo2 = new ItemInfo("宝马",50,10,180);
itemInfo3 = new ItemInfo("小侍女",20,50,30);
// 设置点击监听
findViewById(R.id.rl).setOnClickListener(this);
// 获取界面元素
TextView mLifeTV = (TextView)findViewById(R.id.tv_life);
TextView mNameTV = (TextView)findViewById(R.id.tv_name);
TextView mAttackTV = (TextView)findViewById(R.id.tv_attack);
TextView mSpeedTV = (TextView)findViewById(R.id.tv_speed);
// 界面设值
mLifeTV.setText("生命值+"+itemInfo.getLife());
mNameTV.setText(itemInfo.getName()+" ");
mSpeedTV.setText("敏捷度+"+itemInfo.getSpeed());
mAttackTV.setText("攻击力+"+itemInfo.getAcctack());
// 设置点击监听
findViewById(R.id.bmw).setOnClickListener(this);
TextView mLifeTV2 = (TextView)findViewById(R.id.tv_life2);
TextView mNameTV2 = (TextView)findViewById(R.id.tv_name2);
TextView mAttackTV2 = (TextView)findViewById(R.id.tv_attack2);
TextView mSpeedTV2 = (TextView)findViewById(R.id.tv_speed2);
mLifeTV2.setText("生命值+"+itemInfo2.getLife());
mNameTV2.setText(itemInfo2.getName()+" ");
mSpeedTV2.setText("敏捷度+"+itemInfo2.getSpeed());
mAttackTV2.setText("攻击力+"+itemInfo2.getAcctack());
// 设置点击监听
findViewById(R.id.girl).setOnClickListener(this);
TextView mLifeTV3 = (TextView)findViewById(R.id.tv_life3);
TextView mNameTV3 = (TextView)findViewById(R.id.tv_name3);
TextView mAttackTV3 = (TextView)findViewById(R.id.tv_attack3);
TextView mSpeedTV3 = (TextView)findViewById(R.id.tv_speed3);
mLifeTV3.setText("生命值+"+itemInfo3.getLife());
mNameTV3.setText(itemInfo3.getName()+" ");
mSpeedTV3.setText("敏捷度+"+itemInfo3.getSpeed());
mAttackTV3.setText("攻击力+"+itemInfo3.getAcctack());
}
@Override
public void onClick(View v) {
// 获取intent
Intent intent = new Intent();
switch (v.getId()){
case R.id.rl:
// 存放序列化后的装备对象
intent.putExtra("equipment",itemInfo);
// 返回结果码和intent
setResult(1,intent);
// 关闭该界面
finish();
// 退出选择
break;
case R.id.bmw:
// 存放序列化后的装备对象
intent.putExtra("equipment",itemInfo2);
// 返回结果码和intent
setResult(1,intent);
// 关闭该界面
finish();
// 退出选择
break;
case R.id.girl:
// 存放序列化后的装备对象
intent.putExtra("equipment",itemInfo3);
// 返回结果码和intent
setResult(1,intent);
// 关闭该界面
finish();
// 退出选择
break;
}
}
}
import java.io.Serializable;
public class Subject implements Serializable {
private String name;
private String content;
private int brain;
private int quality;
private int ability;
public Subject(String name, String content, int brain, int quality, int ability) {
this.name = name;
this.content = content;
this.brain = brain;
this.quality = quality;
this.ability = ability;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getBrain() {
return brain;
}
public void setBrain(int brain) {
this.brain = brain;
}
public int getQuality() {
return quality;
}
public void setQuality(int quality) {
this.quality = quality;
}
public int getAbility() {
return ability;
}
public void setAbility(int ability) {
this.ability = ability;
}
}
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class For_Study extends AppCompatActivity implements View.OnClickListener{
private Subject study;
private Subject study2;
private Subject study3;
private Subject study4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_for_study);
// 初始化 科目
study = new Subject("英语","每天学十个单词",2,3,5);
study2 = new Subject("数学","每天学一个小节",5,2,5);
study3 = new Subject("语文","每天背一首古诗",1,4,5);
study4 = new Subject("政治","每天背一个小节",3,3,5);
// 设置点击监听
TextView mNameTV = (TextView)findViewById(R.id.st_name);
TextView mContentTV = (TextView)findViewById(R.id.st_content);
findViewById(R.id.english).setOnClickListener(this);
TextView mBrainTV = (TextView)findViewById(R.id.tv_brain);
TextView mQualityTV = (TextView)findViewById(R.id.tv_quality);
TextView mAbilityTV = (TextView)findViewById(R.id.tv_ability);
mNameTV.setText(study.getName()+" ");
mContentTV.setText(study.getContent());
mBrainTV.setText("智 力+"+study.getBrain());
mQualityTV.setText("品 质+"+study.getQuality());
mAbilityTV.setText("能 力+"+study.getAbility());
// 设置点击监听
TextView mName2TV = (TextView)findViewById(R.id.st_name2);
TextView mContent2TV = (TextView)findViewById(R.id.st_content2);
findViewById(R.id.math).setOnClickListener(this);
TextView mBrain2TV = (TextView)findViewById(R.id.tv_brain2);
TextView mQuality2TV = (TextView)findViewById(R.id.tv_quality2);
TextView mAbility2TV = (TextView)findViewById(R.id.tv_ability2);
mName2TV.setText(study2.getName()+" ");
mContent2TV.setText(study2.getContent());
mBrain2TV.setText("智 力+"+study2.getBrain());
mQuality2TV.setText("品 质+"+study2.getQuality());
mAbility2TV.setText("能 力+"+study2.getAbility());
// 设置点击监听
TextView mName3TV = (TextView)findViewById(R.id.st_name3);
TextView mContent3TV = (TextView)findViewById(R.id.st_content3);
findViewById(R.id.china).setOnClickListener(this);
TextView mBrain3TV = (TextView)findViewById(R.id.tv_brain3);
TextView mQuality3TV = (TextView)findViewById(R.id.tv_quality3);
TextView mAbility3TV = (TextView)findViewById(R.id.tv_ability3);
mName3TV.setText(study3.getName()+" ");
mContent3TV.setText(study3.getContent());
mBrain3TV.setText("智 力+"+study3.getBrain());
mQuality3TV.setText("品 质+"+study3.getQuality());
mAbility3TV.setText("能 力+"+study3.getAbility());
// 设置点击监听
TextView mName4TV = (TextView)findViewById(R.id.st_name4);
TextView mContent4TV = (TextView)findViewById(R.id.st_content4);
findViewById(R.id.politics).setOnClickListener(this);
TextView mBrain4TV = (TextView)findViewById(R.id.tv_brain4);
TextView mQuality4TV = (TextView)findViewById(R.id.tv_quality4);
TextView mAbility4TV = (TextView)findViewById(R.id.tv_ability4);
mName4TV.setText(study4.getName()+" ");
mContent4TV.setText(study4.getContent());
mBrain4TV.setText("智 力+"+study4.getBrain());
mQuality4TV.setText("品 质+"+study4.getQuality());
mAbility4TV.setText("能 力+"+study4.getAbility());
}
@Override
public void onClick(View v) {
// 获取intent
Intent intent = new Intent();
switch (v.getId()){
case R.id.english:
// 存放序列化后的装备对象
intent.putExtra("study",study);
// 返回结果码和intent
setResult(2,intent);
// 关闭该界面
finish();
// 退出选择
break;
case R.id.math:
// 存放序列化后的装备对象
intent.putExtra("study",study2);
// 返回结果码和intent
setResult(2,intent);
// 关闭该界面
finish();
// 退出选择
break;
case R.id.china:
// 存放序列化后的装备对象
intent.putExtra("study",study3);
// 返回结果码和intent
setResult(2,intent);
// 关闭该界面
finish();
// 退出选择
break;
case R.id.politics:
// 存放序列化后的装备对象
intent.putExtra("study",study4);
// 返回结果码和intent
setResult(2,intent);
// 关闭该界面
finish();
// 退出选择
break;
}
}
}
返回目录
#008577
#00574B
#D81B60
#ffffff
#fffff0
#ffffe0
#ffff00
#fffafa
#fffaf0
#fffacd
#fff8dc
#fff5ee
#fff0f5
#ffefd5
#ffebcd
#ffe4e1
#ffe4c4
#ffe4b5
#ffdead
#ffdab9
#ffd700
#ffc0cb
#ffb6c1
#ffa500
#ffa07a
#ff8c00
#ff7f50
#ff69b4
#ff6347
#ff4500
#ff1493
#ff00ff
#ff00ff
#ff0000
#fdf5e6
#fafad2
#faf0e6
#faebd7
#fa8072
#f8f8ff
#f5fffa
#f5f5f5
#f5f5dc
#f5deb3
#f4a460
#f0ffff
#f0fff0
#f0f8ff
#f0e68c
#f08080
#eee8aa
#ee82ee
#e9967a
#e6e6fa
#e0ffff
#deb887
#dda0dd
#dcdcdc
#dc143c
#db7093
#daa520
#da70d6
#d8bfd8
#d3d3d3
#d3d3d3
#d2b48c
#d2691e
#cd853f
#cd5c5c
#c71585
#c0c0c0
#bdb76b
#bc8f8f
#ba55d3
#b8860b
#b22222
#b0e0e6
#b0c4de
#afeeee
#adff2f
#add8e6
#a9a9a9
#a9a9a9
#a52a2a
#a0522d
#9932cc
#98fb98
#9400d3
#9370db
#90ee90
#8fbc8f
#8b4513
#8b008b
#8b0000
#8a2be2
#87cefa
#87ceeb
#808080
#808080
#808000
#800080
#800000
#7fffd4
#7fff00
#7cfc00
#7b68ee
#778899
#778899
#708090
#708090
#6b8e23
#6a5acd
#696969
#696969
#66cdaa
#6495ed
#5f9ea0
#556b2f
#4b0082
#48d1cc
#483d8b
#4682b4
#4169e1
#40e0d0
#3cb371
#32cd32
#2f4f4f
#2f4f4f
#2e8b57
#228b22
#20b2aa
#1e90ff
#191970
#00ffff
#00ffff
#00ff7f
#00ff00
#00fa9a
#00ced1
#00bfff
#008b8b
#008080
#008000
#006400
#0000ff
#0000cd
#00008b
#000080
#000000