• Android实验:Activity界面基础


    前言

    我们都知道,activity是Android中最重要的组件之一,关于activity的具体内容在这里就不多赘述,主打的就是一个主次分明,想具体了解可以阅读笔者前面的文章或者这篇文章,笔者认为描述的很详细
    linkhttp://t.csdnimg.cn/YXOph

    实验目的

    1、 掌握Activity的基本功能;
    2、 掌握preference的基本功能;
    3、 掌握断点的设置,调试程序;

    实验内容

    任务1:通过intent实现跳转,完成Activity之间的跳转;
    任务2:intent数据的传递;
    任务3:采用用preference实现随数据的存储;
    任务4:掌握在虚拟机和真机环境下,对程序的调试;

    实验要求

    1、实现Android界面,并通过intent实现跳转,界面显示学生的姓名,学号,email.
    2、要求intent的实现传递姓名,学号,email等数据,到第二个activity;
    3、同时要求可以在虚拟机及手机上运行结果;
    4、采用preference实现对如上数据的存储,存储及读取。
    5、学会如何设置断点,并学会debug模式下,调试程序。

    代码实现

    mainActivity

    //mainActivity
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.text.Editable;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    
    public class mainActivity extends Activity {
        //定义控件变量
        private EditText etName;//姓名输入框
        private EditText etNumber; // 学号输入框
        private EditText etEmail; // email输入框
        private Button btnSubmit; // 提交按钮
    
        @SuppressLint({"WrongViewCast", "MissingInflatedId"})
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // 初始化控件变量
            etName = findViewById(R.id.et_name);
            etNumber = findViewById(R.id.et_number);
            etEmail = findViewById(R.id.et_email);
            btnSubmit = findViewById(R.id.btn_submit);
    
            // 设置按钮的点击事件监听器
            btnSubmit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 获取输入框中的数据
                    String name = etName.getText().toString();
                    String number = etNumber.getText().toString();
                    String email = etEmail.getText().toString();
    
                    // 创建一个intent对象,用于跳转到ResultActivity
                    Intent intent = new Intent(mainActivity.this, ResultActivity.class);
    
                    // 将数据放入intent中,使用键值对的形式
                    intent.putExtra("name", name);
                    intent.putExtra("number", number);
                    intent.putExtra("email", email);
    
                    // 启动ResultActivity
                    startActivity(intent);
                }
            });
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    ResultActivity

    // ResultActivity.java
    // 这个类是程序的第二个界面,用于显示从MainActivity传递过来的数据
    // 采用preference实现对数据的存储和读取
    
    
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class ResultActivity extends mainActivity {
    
        // 定义控件变量
        private TextView tvName; // 姓名显示框
        private TextView tvNumber; // 学号显示框
        private TextView tvEmail; // email显示框
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_result);
    
            // 初始化控件变量
            tvName = findViewById(R.id.tv_name);
            tvNumber = findViewById(R.id.tv_number);
            tvEmail = findViewById(R.id.tv_email);
    
            // 获取从MainActivity传递过来的intent对象
            Intent intent = getIntent();
    
            // 从intent中获取数据,使用相同的键名
            String name = intent.getStringExtra("name");
            String number = intent.getStringExtra("number");
            String email = intent.getStringExtra("email");
    
            // 将数据显示在控件上
            tvName.setText("姓名:" + name);
            tvNumber.setText("学号:" + number);
            tvEmail.setText("邮箱:" + email);
    
            // 获取一个SharedPreferences对象,用于存储数据
            SharedPreferences sp = getSharedPreferences("student_info", MODE_PRIVATE);
    
            // 获取一个SharedPreferences.Editor对象,用于编辑数据
            SharedPreferences.Editor editor = sp.edit();
    
            // 将数据写入SharedPreferences中,使用相同的键名
            editor.putString("name", name);
            editor.putString("number", number);
            editor.putString("email", email);
    
            // 提交数据的修改
            editor.apply();
    
            // 调用show方法,从SharedPreferences中读取并打印数据
            show();
        }
    
        public void show() {
            // 获取一个SharedPreferences对象,用于读取数据
            SharedPreferences sp = getSharedPreferences("student_info", MODE_PRIVATE);
    
            // 从SharedPreferences中获取数据,使用相同的键名,如果没有找到,则使用默认值""
            String name = sp.getString("name", "");
            String number = sp.getString("number", "");
            String email = sp.getString("email", "");
    
            // 打印数据到控制台上,方便调试
            System.out.println("姓名:" + name);
            System.out.println("学号:" + number);
            System.out.println("邮箱:" + email);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73

    activity_main

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!-- 姓名输入框 -->
        <EditText
            android:id="@+id/et_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入姓名" />
    
        <!-- 学号输入框 -->
        <EditText
            android:id="@+id/et_number"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:hint="请输入学号" />
    
        <!-- email输入框 -->
        <EditText
            android:id="@+id/et_email"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:hint="请输入邮箱" />
    
        <!-- 提交按钮 -->
        <Button
            android:id="@+id/btn_submit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="提交" />
    </LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    activity_result

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:ignore="ExtraText">
        android:orientation="vertical"
        android:padding="16dp">
    
        <!-- 姓名显示框 -->
        <TextView
            android:id="@+id/tv_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <!-- 学号显示框 -->
        <TextView
            android:id="@+id/tv_number"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <!-- email显示框 -->
        <TextView
            android:id="@+id/tv_email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    当然别忘记在mainifest中注册自己写好的页面,不然识别不到哦
    在这里插入图片描述

    结果展示

    在这里插入图片描述
    在这里插入图片描述
    点击提交
    提交后

  • 相关阅读:
    React踩坑记录:Error: Minified React error #31;
    iwebsec靶场 SQL注入漏洞通关笔记11-16进制编码绕过
    2022牛客多校#6 C. Forest
    一种基于目标检测实现黑花屏分类任务的方案
    计算机毕设(附源码)JAVA-SSM辽宁省高考志愿智能辅助填报系统
    k8s,30分钟部署一个kubernetes集群
    Mysql.索引详解
    论文实验图片局部放大工具(可批量操作)
    MYSQL
    DOM、、
  • 原文地址:https://blog.csdn.net/m0_72471315/article/details/134403222