实验二 Activity组件通信


前言

使用intent在两个Activity之间实现数据的传递。
即 a activity———数据———> b activity。
传递的方式有显示和隐式两种。


一、实验目的及要求

(1)掌握显示启动和隐式启动的方式
(2)掌握Activity间的数据通信

二、实验内容及步骤

任务一:根据下述要求实现对应程序。

完成启动界面的设计,要求采用合理布局,使界面效果与图1所示结果保持一致。( “男”单选按钮默认选中,学院下拉列表框的内容为:信息技术学院、外国语学院、机电学院、商学院、艺术设计学院、珠宝学院、新闻传播学院);左列标签名字体大小为24sp;点击“使用显示启动”按钮和“使用隐式启动”按钮,均能跳转至界面2,如图2所示,前者使用显示启动方式,后者使用隐式启动方式,两种启动方式均将界面1中的数据传递至界面2中。

界面 1布局:
在这里插入图片描述

第一个Activity中:

布局文件代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="15dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="学号:"
            android:textSize="24sp" />

        <EditText
            android:id="@+id/et_id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名:"
            android:textSize="24sp" />

        <EditText
            android:id="@+id/et_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>


    <RadioGroup
        android:id="@+id/rg_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="性别:"
            android:textSize="24sp" />

        <RadioButton
            android:id="@+id/rb_man"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="男"></RadioButton>

        <RadioButton
            android:id="@+id/rb_woman"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"></RadioButton>
    </RadioGroup>

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="学院:"
            android:textSize="24sp" />

        <Spinner
            android:id="@+id/spin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:entries="@array/spinnerdata"
            android:spinnerMode="dialog"></Spinner>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="专业:"
            android:textSize="24sp" />

        <EditText
            android:id="@+id/et_pf"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="班级:"
            android:textSize="24sp" />

        <EditText
            android:id="@+id/et_class"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

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

        <Button
            android:id="@+id/btn_show"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="使用显示启动" />

        <Button
            android:id="@+id/btn_hitshow"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="使用隐式启动" />

    </LinearLayout>

</LinearLayout>

注意:这里使用传递数据给Spinner是通过数组资源文件进行设定的。

资源如下:

<resources>
    <string name="app_name">test</string>

    <string-array name="spinnerdata">
        <item>信息技术学院</item>
        <item>外国语学院</item>
        <item>机电学院</item>
        <item>商学院</item>
        <item>艺术设计学院</item>
        <item>珠宝学院</item>
        <item>新闻传播学院</item>

    </string-array>

</resources>

MainActivity中填写:

package com.example.test2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button buttonShow, buttonHitShow;
    private EditText editTextId, editTextName, editTextPf, editTextClass;
    private RadioGroup radioGroup;
    private Spinner spinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getViewId();
        buttonShow.setOnClickListener(this);
        buttonHitShow.setOnClickListener(this);
    }

    private void getViewId() {
        buttonShow = findViewById(R.id.btn_show);
        buttonHitShow = findViewById(R.id.btn_hitshow);
        editTextId = findViewById(R.id.et_id);
        editTextName = findViewById(R.id.et_name);
        radioGroup = findViewById(R.id.rg_1);
        spinner = findViewById(R.id.spin);
        editTextPf = findViewById(R.id.et_pf);
        editTextClass = findViewById(R.id.et_class);
    }

    @Override
    public void onClick(View v) {
        String editTextId_s = editTextId.getText().toString();
        String editTextName_s = editTextName.getText().toString();
        String editTextPf_s = editTextPf.getText().toString();
        String editTextClass_s = editTextClass.getText().toString();
        String spinner_s = spinner.getSelectedItem().toString();

        switch (v.getId()) {
            case R.id.btn_show:
                Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                switch (radioGroup.getCheckedRadioButtonId()) {
                    case R.id.rb_man:
                        intent.putExtra("sexData", "男");
                        break;
                    case R.id.rb_woman:
                        intent.putExtra("sexData", "女");
                        break;
                }
                intent.putExtra("editTextId_s", editTextId_s);
                intent.putExtra("editTextName_s", editTextName_s);
                intent.putExtra("editTextPf_s", editTextPf_s);
                intent.putExtra("editTextClass_s", editTextClass_s);
                intent.putExtra("data_s", spinner_s);

                startActivity(intent);
                break;

            case R.id.btn_hitshow:
                Intent intent1 = new Intent();
                intent1.setAction("com.example.mainactivity2");

                switch (radioGroup.getCheckedRadioButtonId()) {
                    case R.id.rb_man:
                        intent1.putExtra("sexData", "男");
                        break;
                    case R.id.rb_woman:
                        intent1.putExtra("sexData", "女");
                        break;

                }
                intent1.putExtra("editTextId_s", editTextId_s);
                intent1.putExtra("editTextName_s", editTextName_s);
                intent1.putExtra("editTextPf_s", editTextPf_s);
                intent1.putExtra("editTextClass_s", editTextClass_s);
                intent1.putExtra("data_s", spinner_s);

                startActivity(intent1);
                break;
        }
    }
}

任务二:在第二个活动中进行操作

点击界面1中的按钮后,跳转至界面2,将界面1中输入的内容传递至界面2的ListView中显示,效果如图2所示。(要求: 列表文字大小为28sp,图片使用Vector格式,从上到下依次使用credit_card_black,account_box_black,wc_black,account_balance_black,school_black,assignment_ind_black)

界面2布局:

在这里插入图片描述

第二个Activity中:

布局文件代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/lv_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

子项item布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher_background" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello"
        android:textSize="28sp" />


</LinearLayout>

MainActivity中代码:

package com.example.test2;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        ListView lv_data = findViewById(R.id.lv_1);

        SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.activity_main2_item, new String[]{"title", "image"},
                new int[]{R.id.title, R.id.image});
        lv_data.setAdapter(adapter);


    }

    private List<Map<String, Object>> getData() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        Intent intent = getIntent();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("title", intent.getStringExtra("editTextId_s"));

        map.put("image", R.drawable.credit_card_black);
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("title", intent.getStringExtra("editTextName_s"));

        map.put("image", R.drawable.account_box_black);
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("title", intent.getStringExtra("sexData"));
        map.put("image", R.drawable.wc_black);
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("title", intent.getStringExtra("data_s"));
        map.put("image", R.drawable.account_balance_black);
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("title", intent.getStringExtra("editTextPf_s"));
        map.put("image", R.drawable.school_black);
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("title", intent.getStringExtra("editTextClass_s"));
        map.put("image", R.drawable.assignment_ind_black);
        list.add(map);


        return list;
    }
}

注意:在第二个Activity中需要自行指定一个图片

在这里插入图片描述
在这里插入图片描述


  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
西 安 邮 电 大 学 (计算机学院) 课内实验报告 实验名称: 界面设计:基本组件 专 业: 网络工程 班 级: 姓 名: 学 号: 指导教师: 日 期: 2017年4月20日 一.实验目的 1. 掌握常用组件在布局文件中的设置 2. 掌握在java程序中获取组件值 3. 掌握对组件值得验证 4. 掌握基本常用的监听器,和事件处理 5. 掌握将组件值提交到下一个Activity活动的方法 实验环境 JDK的版本: "1.8.0_40" IDE: eclipse 4.6.1 模拟器: 夜神模拟器 三.实验内容 1. 补充完成下例空缺处,完成注册界面、部门列表框、 单击确定检查 提交成功、接受界面 四.实验过程及分析 1. 设计UI界面 1. 编写布局代码,如下 <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical"> <!-- 主布局中添加文本框和输入框 --> <TextView android:text = "@string/user" android:layout_width="match_parent" android:layout_height="wrap_content"/> <EditText android:id="@+id/name" android:layout_width="200sp" android:layout_height="wrap_content" android:hint="@string/inputuser"/> <!-- 在主布局添加文本框和密码框 --> <TextView android:text = "@string/password" android:layout_width="match_parent" android:layout_height="wrap_content"/> <EditText android:id="@+id/password" android:layout_width="200sp" android:layout_height="wrap_content" android:inputType="textPassword"/> <!-- 在主布局添加性别文本和复选框 --> <TextView android:text="@string/sex" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <RadioGroup android:id="@+id/sex" android:orientation = "horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <!-- 设置默认选择的是女 --> <RadioButton android:id="@+id/man" android:text="@string/man" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <RadioButton android:id="@+id/woman" android:checked="true" android:text="@string/woman" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RadioGroup> <!-- 在主布局添加联系电话文本框和输入框 --> <TextView android:text="@string/phone" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <EditText android:id="@+id/phone" android:inputType="text"phone" android:layout_width="200sp" android:layout_height="wrap_content"/> <!-- 在主布局中添加部门文本框和列表框 --> <TextView an

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猿代码_QQ_3014067949

给小编价格鸡腿吧~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值