Android experiment: Activity interface basics

Preface

We all know that activity is one of the most important components in Android. I won’t go into details about the specific content of activity here. The main thing is to distinguish the priorities. If you want to know more about it, you can read the author’s previous article or The author thinks this article is very detailed
linkhttp://t.csdnimg.cn/YXOph

Purpose

1. Master the basic functions of Activity;
2. Master the basic functions of preference;
3. Master the setting of breakpoints and debug programs;

Experiment content

Task 1: Jump through intent to complete the jump between activities;
Task 2: Transfer of intent data;
Task 3 : Use preference to store random data;
Task 4: Master the debugging of programs in virtual machine and real machine environments;

Experimental requirements

1. Implement the Android interface and jump through the intent. The interface displays the student's name, student number, and email.
2. The intent implementation is required to pass the name, student number, Email and other data, go to the second activity;
3. At the same time, it is required to run the results on a virtual machine and a mobile phone;
4. Use preference to implement the above data storage, storage and reading.
5. Learn how to set breakpoints and learn to debug programs in debug mode.

Code

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


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

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>

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>

Of course, don’t forget to register the page you wrote in mainifest, otherwise it won’t be recognized.
Insert image description here

Results display

Insert image description here
Insert image description here
Click submit
After submission

Guess you like

Origin blog.csdn.net/m0_72471315/article/details/134403222