android Intent 使用方法案例

intent 用于两个界面之间的数据传输,现在我们用一个案例来看一下,怎么运用intent来实现,两个页面之间的数据传输。

首先,我们先来看一下,效果图,第一张呢,是开始界面,点击按钮出现第二个界面;接着点击第二个界面,第一界面上的数据发生了变化。
这里写图片描述这里写图片描述这里写图片描述

那么,接下来我们就来实现这个操作。
第一步:
我们创建一个android项目,来给主界面设计一下布局。代码如下;

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="45dp"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="主人,快给小宝宝买装备吧"
        android:textSize="18sp"
        android:gravity="center_horizontal"/>
    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="45dp">
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >

            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="生命值:"
                />

            <ProgressBar
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="0dp"
                android:layout_weight="3"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="5dp"
                android:layout_height="wrap_content"
                android:id="@+id/progressBar" />

            <TextView
                android:id="@+id/tv1"
                android:layout_marginLeft="20dp"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="80"
                />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp">

            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="攻击力:"
                />

            <ProgressBar
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="0dp"
                android:layout_weight="3"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="5dp"
                android:id="@+id/progressBar1" />

            <TextView
                android:id="@+id/tv2"
                android:layout_marginLeft="20dp"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="100"
                />
        </TableRow>
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp">

            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="敏捷:"
                />

            <ProgressBar
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="0dp"
                android:layout_weight="3"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:layout_marginLeft="20dp"
                android:id="@+id/progressBar2" />

            <TextView
                android:id="@+id/tv3"
                android:layout_marginLeft="20dp"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="30"
                />
        </TableRow>
    </TableLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="25dp"
        >

        <Button
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:onClick="click1"
            android:text="主人购买装备"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:onClick="click2"
            android:text="小宝宝购买装备"/>
    </LinearLayout>
</LinearLayout>

在主界面的设计中呢,我用了LinearLayout布局,设置orientation属性为vertical(垂直)。加入一个ImageView(可以选择自己喜欢的图片放在res文件夹下的为drawable文件夹中,通过src属性引用你要的图片)、TextView。设置Layou_gravity属性,让这两个空件,水平居中;再者为了美观,可以给ImageButton设置MargiTop属性。

下面是以一个表格布局TableLayout。加入三个TableRow ,三个TableRow中都放了两个TextView和一个ProgressBar。根据自己的喜好,调节一下它们之间的距离。

第二步:商品界面。代码如下。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/r1">

    <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_life"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="13sp"
            android:text="生命值"/>

        <TextView
            android:id="@+id/tv_atk"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="13sp"
            android:text="攻击力"/>

        <TextView
            android:id="@+id/tv_quick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="13sp"
            android:text="敏捷"/>
    </LinearLayout>
</RelativeLayout>

第三步:
就是在MainActivity中写入点击方法了。主要思路是,我们点击购买装备的按钮,跳出第二个购买装备的页面。然后点击第二页面,第二个页面关闭,并且第一个界面上的数据发生变化。这其中的主要代码如下:

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(data!=null){
            if (resultCode==1){
                if (requestCode==1){
                    info Info = (info) data.getSerializableExtra("equipment");
                    updateProgress(Info);
                }
            }
        }
    }

    private void updateProgress(info Info){
        int progress1 = progressBar1.getProgress();
        int progress2 = progressBar2.getProgress();
        int progress3 = progressBar3.getProgress();

        progressBar1.setProgress(progress1+Info.getLife());
        progressBar2.setProgress(progress2+Info.getAttack());
        progressBar3.setProgress(progress3+Info.getQuick());

       tv_life.setText(progressBar1.getProgress()+"");
        tv_atk.setText(progressBar2.getProgress()+"");
        tv_quick.setText(progressBar3.getProgress()+"");
    }
}
//购物Activity中的代码:
 public void onClick(View v){
        switch (v.getId()){
            case R.id.r1:
                Intent intent = new Intent();
                intent.putExtra("equipment",Info);
                setResult(1,intent);
                finish();
                break;
        }

onActivityResult这个方法,是当第二个界面关闭时,接受它传过来的数据。
在这之前我们还要写个商品类,方便初始化购物界面上的数据。

package com.android.daimain;

import java.io.Serializable;

/**
 * Created by Administrator on 2017/3/27.
 */

public class info implements Serializable{
    private String name;
    private int attack;
    private int life;
    private int quick;

    public info(String name, int attack, int life, int quick) {
        this.name = name;
        this.attack = attack;
        this.life = life;
        this.quick = quick;
    }

    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 getQuick() {
        return quick;
    }

    public void setQuick(int quick) {
        this.quick = quick;
    }


}

以下是完整代码:

package com.example.equipment;

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

import com.android.daimain.info;

public class MainActivity extends AppCompatActivity {
    private ProgressBar progressBar1;
    private ProgressBar progressBar2;
    private ProgressBar progressBar3;
    private TextView tv_atk;
    private TextView tv_life;
    private TextView tv_quick;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressBar1 = (ProgressBar)findViewById(R.id.progressBar);
        progressBar2 = (ProgressBar)findViewById(R.id.progressBar1);
        progressBar3 = (ProgressBar)findViewById(R.id.progressBar2);
        progressBar1.setMax(1000);
        progressBar2.setMax(1000);
        progressBar3.setMax(1000);
        tv_life = (TextView) findViewById(R.id.tv1);
        tv_atk = (TextView) findViewById(R.id.tv2);
        tv_quick = (TextView) findViewById(R.id.tv3);

    }

    public void click1(View v){
        Intent intent = new Intent(this,ShoppingActivity.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){
                    info Info = (info) data.getSerializableExtra("equipment");
                    updateProgress(Info);
                }
            }
        }
    }

    private void updateProgress(info Info){
        int progress1 = progressBar1.getProgress();
        int progress2 = progressBar2.getProgress();
        int progress3 = progressBar3.getProgress();

        progressBar1.setProgress(progress1+Info.getLife());
        progressBar2.setProgress(progress2+Info.getAttack());
        progressBar3.setProgress(progress3+Info.getQuick());

       tv_life.setText(progressBar1.getProgress()+"");
        tv_atk.setText(progressBar2.getProgress()+"");
        tv_quick.setText(progressBar3.getProgress()+"");
    }
}

购物界面

package com.example.equipment;

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

public class ShoppingActivity extends AppCompatActivity implements View.OnClickListener{
   private info Info;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        Info = new info("金剑",100,20,20);
        findViewById(R.id.r1).setOnClickListener(this);
        TextView tv_name = (TextView)findViewById(R.id.tv_name);
        TextView tv_atk = (TextView)findViewById(R.id.tv_atk);
        TextView tv_life = (TextView)findViewById(R.id.tv_life);
        TextView tv_quick = (TextView)findViewById(R.id.tv_quick);

        tv_name.setText(Info.getName()+"");
        tv_atk.setText("攻击力+"+Info.getAttack());
        tv_life.setText("生命值+"+Info.getLife());
        tv_quick.setText("敏捷度+"+Info.getQuick());
    }

    public void onClick(View v){
        switch (v.getId()){
            case R.id.r1:
                Intent intent = new Intent();
                intent.putExtra("equipment",Info);
                setResult(1,intent);
                finish();
                break;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值