Activity的数据请求与数据回传

1. 训练目标
1) 掌握组件ProgressBar的使用
2) 掌握startActivityForResult方法的使用
2.运行效果图
这里写图片描述
3.实验代码

1)主界面设计代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.a16041.app_zhuangbei.MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="45dp"
        android:layout_gravity="center_horizontal"
        android:src="@mipmap/baby"/>


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="人物属性"
        android:textSize="18sp" />


2)表格布局

<TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <!-->>>>>>>>>>>>>>>>>>>>1<<<<<<<<<<<<<<<<<<<<<<<<-->
    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        >
        <TextView
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="生命值:"
            />

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

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

    </TableRow>
        <!-->>>>>>>>>>>>>>>>>>>>2<<<<<<<<<<<<<<<<<<<<<<<<-->
        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="25dp"
            >
            <TextView
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="攻击力:"
                />

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

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

        </TableRow>

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

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

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

        </TableRow>

    </TableLayout>

3)线性布局

 <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="15dp"
        android:layout_gravity="center_horizontal"
        >

        <Button


            android:id="@+id/zhu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableRight="@android:drawable/ic_menu_add"
            android:onClick="onClick1"
            android:text="购买装备" />

    </LinearLayout>
</LinearLayout>

3.事件发生
点击布局时会触发Onclick事件
代码如下:

public class shoppingActivity extends Activity {
    info info;
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);
        RelativeLayout layout=(RelativeLayout)findViewById(R.id.rl);
       layout.setOnClickListener(new MyListener());
        //初始化显示数据
        info = new info("金剑",20,100,20);
        //找控件
        TextView tv_name =(TextView) findViewById(R.id.tv_name);
        TextView tv_life =(TextView) findViewById(R.id.tv_life);
        TextView tv_attack =(TextView) findViewById(R.id.tv_attack);
        TextView tv_speed =(TextView) findViewById(R.id.tv_speed);
        //数据显示在控件上
        tv_name.setText(info.getName());
        tv_life.setText("攻击力:"+info.getAttack());
        tv_attack.setText("敏捷度:"+info.getQuick());
        tv_speed.setText("生命值:"+info.getLife());

    }

    class MyListener implements View.OnClickListener {
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.rl:
                                //System.out.println("返回了!");
                                Intent intent =new Intent();
                                intent.putExtra("info",info);
                                //把结果返回
                                setResult(10,intent);
                                finish();
                                break;
                    default:
                        break;
                }
        }
    }
}

4.Intent
创建一个Intent (意图)用intent.putExtra()方法来储存我们想要返回的值;
利用setResult(int resultCode, Intent intent);设置一个返回码 和返回的意图。现在要用 finish();关闭当前的Activity然后返回这个Intent 。
回到MainActivity.class
用下面的方法来接受并处理返回内容:

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
1
1
        if (resultCode == 10) {    
           if (requestCode == 1) {      

               info info = (info) data.getSerializableExtra("info");                

                 updateProgressBar(info);
               }
            }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值