您的位置:首页 > 移动开发 > Android开发

一只大二狗的Android历程--Activity间数据交换的实例

2017-02-26 10:24 453 查看
2017年 2月26日 10:25 AM

昨晚上敲了一晚上的代码,又参照了教材上的那个Activity数据交换的实例,自己改了一个,做了一个可以登录的角色管理,功能很简单,首先呈现的是注册界面Main.java,然后输入名字,密码和性别以后点击“Register”进入SecAct.java,点击按钮“Go TO MY CHACATER”进入CharAct.java,然后点击GO TO SHOP,购买装备,自动返回,发现属性已经改变了

效果图:













这个应用相比课本上的实例多了一些功能

--- 可以实现注册登录

--- 可以传递注册人的信息

--- 当选择的性别不同时,分配的角色信息是不同的(最后一图)

现在将源代码贴在这里(activity_main.xml和activity_secact.xml没有变化,可以去我上一篇博文里去找):

activity_char.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
/>
<ImageView
android:id="@+id/iv"
android:layout_width="120dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:background="@drawable/soldier76"/>
<TextView
android:id="@+id/CN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:textSize="23sp"
android:text="Soldier76"
/>
<TextView
android:id="@+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:layout_marginBottom="3dp"
android:text="Wonderful"
android:textColor="#7CFC00"/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Health"
android:textSize="20sp"
android:layout_weight="1"
/>
<ProgressBar
android:id="@+id/pro_health"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_weight="2"
android:indeterminateTint="#7CFC00"
android:indeterminateTintMode="src_atop"/>

<TextView
android:id="@+id/status_hea"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
/>

</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Power"
android:textSize="20sp"
android:layout_weight="1"
/>
<ProgressBar
android:id="@+id/pro_power"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_weight="2"
android:indeterminateTint="#6495ED"
android:indeterminateTintMode="src_atop"/>
<TextView
android:id="@+id/status_pow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
/>

</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sensitivity"
android:textSize="20sp"
android:layout_weight="1"
/>
<ProgressBar
android:id="@+id/pro_sen"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_weight="2"
android:indeterminateTint="#EEEE00"
android:indeterminateTintMode="src_atop"/>
<TextView
android:id="@+id/status_sen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
/>

</TableRow>

</TableLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Go to Shop"/>
</LinearLayout>

activity_shop.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/rr"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="@+id/item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item"
android:layout_marginLeft="60dp"
android:layout_marginTop="20dp"
android:textSize="20sp"
android:layout_centerVertical="true"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/item"
android:layout_marginLeft="10dp"
android:layout_centerInParent="true"
android:orientation="vertical">
<TextView
android:id="@+id/health"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Health"
android:textSize="23sp"/>
<TextView
android:id="@+id/power"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Power"
android:textSize="23sp"/>
<TextView
android:id="@+id/sen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sen"
android:textSize="23sp"/>

</LinearLayout>

</RelativeLayout>

新建一个package,里面新建一个ItemInfo的类,用来存储装备信息
ItermInfo.java

package cn.itcast.domain;

import java.io.Serializable;

/**
* Created by li124 on 2017/2/25.
*/

public class ItemInfo implements Serializable {
private String name;
private int health;
private int power;
private int sen;
public ItemInfo(String name,int health,int power,int sen){
this.name=name;
this.health=health;
this.power=power;
this.sen=sen;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public int getHealth(){
return health;
}
public void setHealth(int health){
this.health=health;
}
public int getPower(){
return power;
}
public void setPower(int power){
this.power=power;
}
public int getSen(){
return sen;
}
public void setSen(int sen){
this.sen=sen;
}
public String toString(){
return "[name="+name+"power="+power+"sen="+sen+"]";
}
}


MainActivity.java里的代码有变化,重新贴出来

package com.eee.eeeee;
import android.app.Activity;
import android.content.Intent;
import android.inputmethodservice.Keyboard;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
import java.util.Timer;
import java.util.TimerTask;
import static android.R.attr.data;
import static android.R.attr.logo;
import static com.eee.eeeee.R.styleable.View;

public class MainActivity extends AppCompatActivity{
public EditText et1,et2;
public RadioButton rb1,rb2;
public static final int RC=1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 =(Button)findViewById(R.id.BB1);
Button b2 =(Button)findViewById(R.id.BB2);
et1 =(EditText)findViewById(R.id.et_UN);
et2 =(EditText)findViewById(R.id.et_PW);
rb1 =(RadioButton)findViewById(R.id.man);
rb2 =(RadioButton)findViewById(R.id.woman);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent =new Intent(MainActivity.this,SecACT.class);
intent.putExtra("un",et1.getText().toString().trim());
startActivity(intent);

}
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Intent intent =new Intent(MainActivity.this,SecACT.class);
passData();
Toast.makeText(MainActivity.this,"Regist Successful!",Toast.LENGTH_SHORT).show();
//startActivity(intent);

}
});
// b2.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// startActivity(new Intent(MainActivity.this,SecACT.class));
// finish();
// }
// });
}
// @Override
// protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// if(requestCode==RC){
// switch (resultCode){
// case RESULT_OK:
// String dataString=data.getStringExtra("ED");
// Log.i("----------->", dataString);
// }
// }
// }

public void passData() {
/**Intent**/
Intent intent = new Intent(MainActivity.this, SecACT.class);
intent.putExtra("un", et1.getText().toString().trim());
intent.putExtra("pd", et2.getText().toString().trim());
/**Bundle**/
// Bundle bundle =new Bundle();
// bundle.putString("un",et1.getText().toString().trim());
// bundle.putString("pd",et2.getText().toString().trim());
String sex = "";
if (rb1.isChecked()) {
sex = "Man";
} else if (rb2.isChecked()) {
sex = "Woman";
}
/**Intent**/
intent.putExtra("sex", sex);
startActivity(intent);
}
// /**Bundle**/
//// bundle.putString("sex",sex);
//// intent.putExtras(bundle);
// }
/**Double Click BackButton to Exit**/
private long firstTime=0;
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode){
case KeyEvent.KEYCODE_BACK:
long secondTime=System.currentTimeMillis();
if(secondTime-firstTime>2000){
Toast.makeText(MainActivity.this,"Press Again to Exit!",Toast.LENGTH_SHORT).show();
firstTime=secondTime;
return true;
}else{
finish();
}
break;
}
return super.onKeyUp(keyCode, event);
}
//one way to

}


SecAct.java里的代码有变化,也重新贴出来

package com.eee.eeeee;

import android.os.Bundle;
import android.content.Intent;
import android.inputmethodservice.Keyboard;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import static com.eee.eeeee.R.styleable.View;
/**
* Created by li124 on 2017/2/24.
*/

public class SecACT extends AppCompatActivity {
public TextView tv1,tv2,tv3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secact);
Butto
be9c
n b1=(Button)findViewById(R.id.back1);
// intent.putExtra("ED","Hello World");
// setResult(RESULT_OK,intent);
// b1.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
//
// finish();
// }
// });

tv1=(TextView)findViewById(R.id.name);
tv2=(TextView)findViewById(R.id.pass);
tv3=(TextView)findViewById(R.id.sex);
Intent i =getIntent();
String name =i.getStringExtra("un");
String pass = i.getStringExtra("pd");
String sex =i.getStringExtra("sex");
tv1.setText("Your UserName: "+name);
tv2.setText("Your PassWord: "+pass+" (Please Do not tell Others)");
tv3.setText("Your Sex: "+sex);
/**Bundle**/
// Bundle bundle=intent.getExtras();
// String name=bundle.getString("un");
// String pass=bundle.getString("pd");
// String sex1=bundle.getString("sex");
// tv1.setText("Your UserName: "+name);
// tv2.setText("Your PassWord: "+pass+" (Please Do not tell Others)");
// tv3.setText("Your Sex: "+sex1);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Intent intent=new Intent(SecACT.this,CharAct.class);
intent.putExtra("name",tv1.getText().toString().trim());
if(getData().equals("Woman")){
intent.putExtra("code","2");
}else if(getData().equals("Man")){
intent.putExtra("code","1");
}
Log.i("---->",getData());
startActivity(intent);

}
});

}
public String getData(){
Intent in =getIntent();
String x=in.getStringExtra("sex");
return x;
}
}



CharAct.java

package com.eee.eeeee;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import cn.itcast.domain.ItemInfo;

import static com.eee.eeeee.R.drawable.soldier76;

/**
* Created by li124 on 2017/2/25.
*/

public class CharAct extends AppCompatActivity {
private ProgressBar hp,pp,sp;
private TextView sh,stvp,ss;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_char);
sh=(TextView)findViewById(R.id.status_hea);
stvp=(TextView)findViewById(R.id.status_pow);
ss=(TextView)findViewById(R.id.status_sen);
Button b=(Button)findViewById(R.id.button);
TextView tv=(TextView)findViewById(R.id.name);
ImageView iv =(ImageView)findViewById(R.id.iv);
TextView tv2=(TextView)findViewById(R.id.CN);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(CharAct.this,ShopAct.class);
startActivityForResult(intent,1);
}
});
Intent i =getIntent();
String name=i.getStringExtra("name");
String code=i.getStringExtra("code");
Log.i("--->",code);
if(code.equals("1")) {
tv.setText(name + "`s Character is Soldier76");
iv.setBackground(getDrawable(R.drawable.soldier76));
tv2.setText("Soldier76");
}else if(code.equals("2")){
tv.setText(name + "`s Character is Mercy");
iv.setBackground(getDrawable(R.drawable.angel));
tv2.setText("Mercy");
}
initProgress();
}
private void initProgress(){
int x=0;
hp=(ProgressBar)findViewById(R.id.pro_health);
pp=(ProgressBar)findViewById(R.id.pro_power);
sp=(ProgressBar)findViewById(R.id.pro_sen);
sh=(TextView)findViewById(R.id.status_hea);
stvp=(TextView)findViewById(R.id.status_pow);
ss=(TextView)findViewById(R.id.status_sen);
Button b=(Button)findViewById(R.id.button);
hp.setMax(100);
pp.setMax(100);
sp.setMax(100);
hp.setProgress(30);
pp.setProgress(20);
sp.setProgress(80);
sh.setText(hp.getProgress()+"");
stvp.setText(pp.getProgress()+"");
ss.setText(sp.getProgress()+"");
x=hp.getProgress();
judegeStatus(x);

}

@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
if(data!=null){
if(resultCode==1){
if(requestCode==1){
ItemInfo info =(ItemInfo)data.getSerializableExtra("equip");
updateProgress(info);
}
}
}
}
private void updateProgress(ItemInfo info){
int x=0;
int p1=hp.getProgress();
int p2=pp.getProgress();
int p3=sp.getProgress();
hp.setProgress(p1+info.getHealth());
pp.setProgress(p2+info.getPower());
sp.setProgress(p3+info.getSen());
sh.setText(hp.getProgress()+"");
stvp.setText(pp.getProgress()+"");
ss.setText(sp.getProgress()+"");
x=hp.getProgress();
judegeStatus(x);

}
public void judegeStatus(int x){
TextView tx=(TextView)findViewById(R.id.status);
if(x>=50){
tx.setTextColor(Color.rgb(255, 255, 0));
tx.setText("Good");
}else if(x>=90&&x<=100){
tx.setTextColor(Color.rgb(0, 255, 0));
tx.setText("Wonderful");
}else if(x<25&&x>=0){
tx.setTextColor(Color.rgb(255, 0, 0));
tx.setText("Weak");
}

}

}


ShopAct.java
package com.eee.eeeee;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import cn.itcast.domain.ItemInfo;

/**
* Created by li124 on 2017/2/25.
*/

public class ShopAct extends AppCompatActivity implements View.OnClickListener{
private ItemInfo itemInfo;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shop);
itemInfo =new ItemInfo("Tactical eyepiece",80,100,60);
findViewById(R.id.rr).setOnClickListener(this);
TextView h=(TextView)findViewById(R.id.health);
TextView p=(TextView)findViewById(R.id.power);
TextView s=(TextView)findViewById(R.id.sen);
TextView n=(TextView)findViewById(R.id.item);
n.setText(itemInfo.getName()+"");
h.setText("Health++"+itemInfo.getHealth());
p.setText("Power++"+itemInfo.getPower());
s.setText("Sen++"+itemInfo.getSen());

}
@Override
public void onClick(View v){
switch (v.getId()){
case R.id.rr:
Intent intent =new Intent();
intent.putExtra("equip",itemInfo);
setResult(1,intent);
finish();
break;
}
}
}


以上就是这个程序的源代码,今天学习了数据的存储以后,准备对这个程序进行丰富
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 日常闲扯
相关文章推荐