装备选择

本文介绍了一个Android应用程序如何通过启动另一个Activity来请求并接收数据的过程。具体实现包括使用Intent传递对象数据,通过onActivityResult处理返回结果,并更新主Activity的UI。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们在一个主界面点下按钮之后打开另外一个Activity并要求这个Activity返回一个数据,数据返回之后主窗体再对数据进行处理。

以下是其主页面:
这里写图片描述

下面是代码文件:

<?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" />

    <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>
        <!-->>>>>>>>>>>>>>>>>>>>3<<<<<<<<<<<<<<<<<<<<<<<<-->
        <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>
    <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>



Class:

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.ProgressBar;
import android.widget.TextView;

import com.edu.domain.info;

public class MainActivity extends AppCompatActivity  {

    private ProgressBar p1;
    private ProgressBar p2;
    private ProgressBar p3;
    private TextView t1;
    private TextView t2;
    private TextView t3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //找到控件
        p1 = (ProgressBar) findViewById(R.id.progressBar1);
        p2 = (ProgressBar) findViewById(R.id.progressBar2);
        p3 = (ProgressBar) findViewById(R.id.progressBar3);
        t1 = (TextView) findViewById(R.id.life);
        t2 = (TextView) findViewById(R.id.atk);
        t3 = (TextView) findViewById(R.id.quick);
        Button btn1=(Button)findViewById(R.id.zhu);
        btn1.setOnClickListener(new MyListener());
        p1.setMax(1000);
        p2.setMax(1000);
        p3.setMax(1000);
        //p1.setProgress(200);
    }

    class MyListener implements View.OnClickListener {

        public void onClick(View v) {
            Intent intent =new Intent(MainActivity.this,shoppingActivity.class);
            startActivityForResult(intent ,1);//要求打开的窗体返回一个数据

        }
    }
            //购买完毕后执
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (data != null) {
            if (resultCode == 10) {
                if (requestCode == 1) {
                    //info info =(info)data.getExtras().get("info");
                    info info = (info) data.getSerializableExtra("info");
                    //更新
                    updateProgressBar(info);
               }
            }
        }
    }
    private void updateProgressBar(info info) {

        System.out.println("调用了update");
        //获取原进度
        int pro1 = p1.getProgress();
        int pro2 = p2.getProgress();
        int pro3 = p3.getProgress();
        //更新进度
        p1.setProgress(pro1+info.getLife());
        p2.setProgress(pro2+info.getAttack());
        p3.setProgress(pro3+info.getQuick());
        //更新TextView
        t1.setText(p1.getProgress()+"");
        t2.setText(p2.getProgress()+"");
        t3.setText(p3.getProgress()+"");

    }

}


我们创建一个打开shoppingActivityIntent(意图),然后用 startActivityForResult(Intent intent, Int requestCode);开启这个意图并设置一个requestCode(请求码)要求这个Activity带一返回值。

转到ShoppingActivity:

这里写图片描述

点击布局时会触发它的Onclick事件:
`import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.edu.domain.info;

/**
* Created by 16041 on 2017/3/26.
*/

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;
            }

    }


}

}

触发事件时判断点击的是否是id为rl的布局,如果是就进入选择语句:

 Intent intent =new Intent();
     intent.putExtra("info",info);
     setResult(10,intent);
     finish();
     break;


  创建一个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”);
//调用我们写好的类将回传的数据更新到Activity中
updateProgressBar(info);
}
}

下面是其运行效果图:
这里写图片描述

标题“51单片机通过MPU6050-DMP获取姿态角例程”解析 “51单片机通过MPU6050-DMP获取姿态角例程”是一个基于51系列单片机(一种常见的8位微控制器)的程序示例,用于读取MPU6050传感器的数据,并通过其内置的数字运动处理器(DMP)计算设备的姿态角(如倾斜角度、旋转角度等)。MPU6050是一款集成三轴加速度计和三轴陀螺仪的六自由度传感器,广泛应用于运动控制和姿态检测领域。该例程利用MPU6050的DMP功能,由DMP处理复杂的运动学算法,例如姿态融合,将加速度计和陀螺仪的数据进行整合,从而提供稳定且实时的姿态估计,减轻主控MCU的计算负担。最终,姿态角数据通过LCD1602显示屏以字符形式可视化展示,为用户提供直观的反馈。 从标签“51单片机 6050”可知,该项目主要涉及51单片机和MPU6050传感器这两个关键硬件组件。51单片机基于8051内核,因编程简单、成本低而被广泛应用;MPU6050作为惯性测量单元(IMU),可测量设备的线性和角速度。文件名“51-DMP-NET”可能表示这是一个与51单片机及DMP相关的网络资源或代码库,其中可能包含C语言等适合51单片机的编程语言的源代码、配置文件、用户手册、示例程序,以及可能的调试工具或IDE项目文件。 实现该项目需以下步骤:首先是硬件连接,将51单片机与MPU6050通过I2C接口正确连接,同时将LCD1602连接到51单片机的串行数据线和控制线上;接着是初始化设置,配置51单片机的I/O端口,初始化I2C通信协议,设置MPU6050的工作模式和数据输出速率;然后是DMP配置,启用MPU6050的DMP功能,加载预编译的DMP固件,并设置DMP输出数据的中断;之后是数据读取,通过中断服务程序从DMP接收姿态角数据,数据通常以四元数或欧拉角形式呈现;再接着是数据显示,将姿态角数据转换为可读的度数格
MathorCup高校数学建模挑战赛是一项旨在提升学生数学应用、创新和团队协作能力的年度竞赛。参赛团队需在规定时间内解决实际问题,运用数学建模方法进行分析并提出解决方案。2021年第十一届比赛的D题就是一个典型例子。 MATLAB是解决这类问题的常用工具。它是一款强大的数值计算和编程软件,广泛应用于数学建模、数据分析和科学计算。MATLAB拥有丰富的函数库,涵盖线性代数、统计分析、优化算法、信号处理等多种数学操作,方便参赛者构建模型和实现算法。 在提供的文件列表中,有几个关键文件: d题论文(1).docx:这可能是参赛队伍对D题的解答报告,详细记录了他们对问题的理解、建模过程、求解方法和结果分析。 D_1.m、ratio.m、importfile.m、Untitled.m、changf.m、pailiezuhe.m、huitu.m:这些是MATLAB源代码文件,每个文件可能对应一个特定的计算步骤或功能。例如: D_1.m 可能是主要的建模代码; ratio.m 可能用于计算某种比例或比率; importfile.m 可能用于导入数据; Untitled.m 可能是未命名的脚本,包含临时或测试代码; changf.m 可能涉及函数变换; pailiezuhe.m 可能与矩阵的排列组合相关; huitu.m 可能用于绘制回路图或流程图。 matlab111.mat:这是一个MATLAB数据文件,存储了变量或矩阵等数据,可能用于后续计算或分析。 D-date.mat:这个文件可能包含与D题相关的特定日期数据,或是模拟过程中用到的时间序列数据。 从这些文件可以推测,参赛队伍可能利用MATLAB完成了数据预处理、模型构建、数值模拟和结果可视化等一系列工作。然而,具体的建模细节和解决方案需要查看解压后的文件内容才能深入了解。 在数学建模过程中,团队需深入理解问题本质,选择合适的数学模
以下是关于三种绘制云图或等高线图算法的介绍: 一、点距离反比插值算法 该算法的核心思想是基于已知数据点的值,计算未知点的值。它认为未知点的值与周围已知点的值相关,且这种关系与距离呈反比。即距离未知点越近的已知点,对未知点值的影响越大。具体来说,先确定未知点周围若干个已知数据点,计算这些已知点到未知点的距离,然后根据距离的倒数对已知点的值进行加权求和,最终得到未知点的值。这种方法简单直观,适用于数据点分布相对均匀的情况,能较好地反映数据在空间上的变化趋势。 二、双线性插值算法 这种算法主要用于处理二维数据的插值问题。它首先将数据点所在的区域划分为一个个小的矩形单元。当需要计算某个未知点的值时,先找到该点所在的矩形单元,然后利用矩形单元四个顶点的已知值进行插值计算。具体过程是先在矩形单元的一对对边上分别进行线性插值,得到两个中间值,再对这两个中间值进行线性插值,最终得到未知点的值。双线性插值能够较为平滑地过渡数据值,特别适合处理图像缩放、地理数据等二维场景中的插值问题,能有效避免插值结果出现明显的突变。 三、面距离反比 + 双线性插值算法 这是一种结合了面距离反比和双线性插值两种方法的算法。它既考虑了数据点所在平面区域对未知点值的影响,又利用了双线性插值的平滑特性。在计算未知点的值时,先根据面距离反比的思想,确定与未知点所在平面区域相关的已知数据点集合,这些点对该平面区域的值有较大影响。然后在这些已知点构成的区域内,采用双线性插值的方法进行进一步的插值计算。这种方法综合了两种算法的优点,既能够较好地反映数据在空间上的整体分布情况,又能保证插值结果的平滑性,适用于对插值精度和数据平滑性要求较高的复杂场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值