Android四大组件详解之Activity数据传递及回传(初级)

内容:Activity数据传递及回传

功能:

  • 注册后,将注册信息显示在另一个页面(数据传递
  • 重新封装一个工具类,通过数据回传将数值加给基础数据(数据回传)

用到的知识点:

  • 页面的跳转
  • 页面间数据的传递
  • 页面数据的回传
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    MainActivity
    <LinearLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginTop="10dp"
        android:orientation="vertical">
       <RelativeLayout
           android:id="@+id/regist_username"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_margin="5dp">
           <TextView
               android:id="@+id/tv_username"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_centerVertical="true"
               android:text="用户名"
               android:textSize="20dp" />
           <EditText
               android:id="@+id/et_name"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:layout_marginLeft="5dp"
               android:layout_toRightOf="@id/tv_username"
               android:hint="请输入用户名"
               android:textSize="16dp" />
       </RelativeLayout>
        <RelativeLayout
            android:id="@+id/regist_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp">
            <TextView
                android:id="@+id/tv_password"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:text="密    码"
                android:textSize="20dp" />
            <EditText
                android:id="@+id/et_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_toRightOf="@id/tv_password"
                android:hint="请输入密码"
                android:inputType="textPassword"
                android:textSize="16dp" />
        </RelativeLayout>
    </LinearLayout>
    <Button
        android:id="@+id/button_send"
        android:layout_width="160dp"
        android:layout_height="48dp"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_below="@id/layout"
        android:text="注册"
        android:textSize="20dp"
        android:textStyle="bold" />
</RelativeLayout>

ShopActivity

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#307f7f7f"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="5dp">
    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:background="#4b8b91"/>
        <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/tv_name"
         android:text="商品名称"
         android:layout_marginLeft="20dp"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_life"
            android:text="生命值"
            android:textSize="13sp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_attack"
            android:text="攻击力"
            android:textSize="13sp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_speed"
            android:text="速度"
            android:textSize="13sp"/>
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

ShowActivity

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="15dp"
    android:layout_marginTop="30dp"
    android:background="#66dc89"
    android:orientation="horizontal"
    android:padding="15dp">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:orientation="vertical"
        android:paddingLeft="20dp">
        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名"
            android:textColor="#ffffff"
            android:textSize="18sp"
            android:textStyle="bold"/>
        <TextView
            android:id="@+id/tv_password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="密    码"
            android:textColor="#ffffff"
            android:textSize="18sp"
            android:textStyle="bold"/>
        </LinearLayout>
    </LinearLayout>
    <TextView
        android:id="@+id/pet_dialog_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="25dp"
        android:text="主人,快给宝宝购买装备吧"
        android:textSize="20sp" />
    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="50dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="5dp">
    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_height="wrap_content"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:text="生命值"
            android:textColor="@android:color/black"
            android:textSize="18sp" />
        <ProgressBar
            android:id="@+id/progressbar1"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="2"/>
        <TextView
            android:id="@+id/tv_life_progress"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:text="0"
            android:textColor="#000000"
            android:textSize="18sp" />
    </TableRow>
        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_height="wrap_content"
                android:layout_width="0dip"
                android:layout_weight="1"
                android:text="攻击力"
                android:textColor="@android:color/black"
                android:textSize="18sp" />
            <ProgressBar
                android:id="@+id/progressbar2"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="2"/>
            <TextView
                android:id="@+id/tv_attack_progress"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_gravity="center"
                android:text="0"
                android:textColor="#000000"
                android:textSize="18sp" />
        </TableRow>
        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_height="wrap_content"
                android:layout_width="0dip"
                android:layout_weight="1"
                android:text="敏    捷"
                android:textColor="@android:color/black"
                android:textSize="18sp" />
            <ProgressBar
                android:id="@+id/progressbar3"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="2"/>
            <TextView
                android:id="@+id/tv_speed_progress"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_gravity="center"
                android:text="0"
                android:textColor="#000000"
                android:textSize="18sp" />
        </TableRow>
    </TableLayout>
    <Button
        android:id="@+id/button_baby"
        android:layout_width="160dp"
        android:layout_height="48dp"
        android:layout_gravity="center"
        android:text="立即购买 Go!"
        android:textSize="18sp"
        android:onClick="click"
        android:textStyle="bold” />
</LinearLayout>

==MainActivity.java ==

public class MainActivity extends AppCompatActivity {
    private EditText et_password;
    private Button button_send;
    private EditText et_name;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_name =(EditText) findViewById(R.id.et_name);
        et_password =(EditText) findViewById(R.id.et_password);
        button_send = (Button) findViewById(R.id.button_send);
        button_send.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                passData();
            }
        });
    }
    public void passData(){
        Intent intent = new Intent(this,ShowActivity.class);
        intent.putExtra("name",et_name.getText().toString().trim());
        intent.putExtra("password",et_password.getText().toString().trim());
        startActivity(intent);
    }
}

==ShopActivity.java ==

public class ShopActivity extends AppCompatActivity implements
View.OnClickListener{
    private ItemInfo itemInfo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);
        itemInfo = new ItemInfo("金剑",100,20,20);
        findViewById(R.id.r1).setOnClickListener(this);
        TextView mLifeTV = (TextView) findViewById(R.id.tv_life);
        TextView mNameTV = (TextView) findViewById(R.id.tv_name);
        TextView mSpeedTV = (TextView) findViewById(R.id.tv_speed);
        TextView mAttackTV = (TextView) findViewById(R.id.tv_attack);
        mLifeTV.setText("生命值+"+ itemInfo.getLife());
        mNameTV.setText(itemInfo.getName()+"");
        mSpeedTV.setText("敏捷度+"+ itemInfo.getSpeed());
        mAttackTV.setText("攻击力+"+ itemInfo.getAttack());
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.r1:
                Intent intent = new Intent();
                intent.putExtra("equipment",itemInfo);
                setResult(1, intent);
                finish();
                break;
        }
    }
}

==ShowActivity.java ==

public class ShowActivity extends AppCompatActivity {
    private ProgressBar mProgressBar1;
    private ProgressBar mProgressBar2;
    private ProgressBar mProgressBar3;
    private TextView mLifeTV;
    private TextView mAttackTV;
    private TextView mSpeedTV;
    private TextView tv_name;
    private TextView tv_password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        String password =intent.getStringExtra("password");
        tv_name =(TextView) findViewById(R.id.tv_name);
        tv_password =(TextView) findViewById(R.id.tv_password);
        tv_name.setText("用户名:"+name);
        tv_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);
        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);
    }
    public void click(View view) {
        Intent intent = new Intent(this,ShopActivity.class);
        startActivityForResult(intent, 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");
                    updataProgress(info);
                }
            }
        }
    }
    private  void updataProgress(ItemInfo info) {
        int Progress1 = mProgressBar1.getProgress();
        int Progress2 = mProgressBar1.getProgress();
        int Progress3 = mProgressBar1.getProgress();
        mProgressBar1.setProgress(Progress1 + info.getLife());
        mProgressBar2.setProgress(Progress2 + info.getAttack());
        mProgressBar3.setProgress(Progress3 + info.getSpeed());
        mLifeTV.setText(mProgressBar1.getProgress() + "");
        mAttackTV.setText(mProgressBar2.getProgress()+"");
        mSpeedTV.setText(mProgressBar3.getProgress()+"");
    }
}

ItemInfo .java

public class ItemInfo implements Serializable {
    private String name;
    private int attack;
    private int life;
    private int speed;
    public ItemInfo(String name,int attack,int life,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;
    }
}
  • 2
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值