【实验目的】
- 掌握Activity的基本功能;
- 掌握preference的基本功能;
- 掌握断点的设置,调试程序;
【实验内容】
任务1:通过intent实现跳转,完成Activity之间的跳转;
任务2:intent数据的传递;
任务3:采用用preference实现随数据的存储;
任务4:掌握在虚拟机和真机环境下,对程序的调试;
【实验要求】
1、实现Android界面,并通过intent实现跳转,界面显示学生的姓名,学号,email.
2、要求intent的实现传递姓名,学号,email等数据,到第二个activity;
3、同时要求可以在虚拟机及手机上运行结果;
4、采用preference实现对如上数据的存储,存储及读取。
5、学会如何设置断点,并学会debug模式下,调试程序。
【实验结果】
AndroidManifest.xml
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.studentinfoapp">
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/Theme.StudentInfoApp">
- <activity android:name=".MainActivity"
- android:exported="true"> <!-- 设置为true或false,取决于你的需求 -->
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity android:name=".SecondActivity"
- android:exported="true">
- <!-- 如果需要从其他应用启动,可以添加以下intent-filter -->
- <intent-filter>
- <action android:name="android.intent.action.VIEW" />
- <category android:name="android.intent.category.DEFAULT" />
- <!-- 可以添加数据类型等其他配置 -->
- </intent-filter>
- </activity>
- </application>
- </manifest>
MainActivity.java
- package com.example.studentinfoapp;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import androidx.annotation.Nullable;
- import androidx.appcompat.app.AppCompatActivity;
- public class MainActivity extends AppCompatActivity {
- private EditText etName, etStudentId, etEmail;
- private Button btnNext;
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- etName = findViewById(R.id.etName);
- etStudentId = findViewById(R.id.etStudentId);
- etEmail = findViewById(R.id.etEmail);
- btnNext = findViewById(R.id.btnNext);
- btnNext.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // 从输入框中获取数据
- String name = etName.getText().toString();
- String studentId = etStudentId.getText().toString();
- String email = etEmail.getText().toString();
- // 使用SharedPreferences存储数据
- SharedPreferences sharedPreferences = getSharedPreferences("StudentInfo", MODE_PRIVATE);
- SharedPreferences.Editor editor = sharedPreferences.edit();
- editor.putString("NAME", name);
- editor.putString("STUDENT_ID", studentId);
- editor.putString("EMAIL", email);
- editor.apply();
- // 创建Intent并传递数据到SecondActivity
- Intent intent = new Intent(MainActivity.this, SecondActivity.class);
- intent.putExtra("NAME", name);
- intent.putExtra("STUDENT_ID", studentId);
- intent.putExtra("EMAIL", email);
- startActivity(intent);
- }
- });
- }
- }
SecondActivity.java
- package com.example.studentinfoapp;
- import static com.example.studentinfoapp.R.*;
- import android.annotation.SuppressLint;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.widget.TextView;
- import androidx.annotation.Nullable;
- import androidx.appcompat.app.AppCompatActivity;
- public class SecondActivity extends AppCompatActivity {
- private TextView tvName, tvStudentId, tvEmail;
- @SuppressLint("MissingInflatedId")
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_second);
- tvName = findViewById(R.id.tvName);
- tvStudentId = findViewById(R.id.tvStudentId);
- tvEmail = findViewById(R.id.tvEmail);
- // 使用Intent传递的数据来更新UI
- Intent intent = getIntent();
- String nameFromIntent = ((Intent) intent).getStringExtra("NAME");
- String studentIdFromIntent = intent.getStringExtra("STUDENT_ID");
- String emailFromIntent = intent.getStringExtra("EMAIL");
- // 显示从Intent获取的数据
- tvName.setText("Name: " + nameFromIntent);
- tvStudentId.setText("Student ID: " + studentIdFromIntent);
- tvEmail.setText("Email: " + emailFromIntent);
- }
- }
Activity_main.xml
- <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="16dp">
- <EditText
- android:id="@+id/etName"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Enter Name"/>
- <EditText
- android:id="@+id/etStudentId"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Enter Student ID"/>
- <EditText
- android:id="@+id/etEmail"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Enter Email"/>
- <Button
- android:id="@+id/btnNext"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Next"/>
- </LinearLayout>
Activity_second.xml
- <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="16dp">
- <TextView
- android:id="@+id/tvName"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Name: "/>
- <TextView
- android:id="@+id/tvStudentId"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Student ID: "/>
- <TextView
- android:id="@+id/tvEmail"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Email: "/>
- </LinearLayout>
5、学会如何设置断点,并学会debug模式下,调试程序。
点击按钮跳转
知识点总结:
Activity基础:
了解Activity的生命周期,特别是 onCreate 方法,它是Activity创建时最先调用的方法。
Intent传递数据:
学会了如何使用Intent在Activity之间传递数据,包括如何使用 putExtra 方法添加额外的数据和 getStringExtra 方法检索数据。
SharedPreferences存储数据:
学会了如何使用 SharedPreferences 来存储和检索键值对数据,这对于保存用户的设置或少量数据非常有用。
XML布局:
掌握了如何在XML中定义用户界面,包括EditText、Button和TextView等基本控件的使用。
事件处理:
学会了如何处理用户交互事件,例如给按钮设置点击监听器。
调试技巧:
了解了如何在Android Studio中设置断点和使用调试模式来调试应用程序。
心得体会:
布局设计:
设计布局时,要考虑到不同设备的屏幕尺寸和分辨率。使用 dp(密度无关像素)单位而不是 px(像素)可以更好地适应不同屏幕。
用户体验:
用户体验非常重要,确保按钮和输入框的大小适中,位置合理,以便用户容易操作。
数据传递:
在传递数据时,要注意键值对的匹配,任何小的拼写错误都可能导致数据无法正确传递。
代码调试:
调试是开发过程中不可或缺的一部分。设置断点和单步执行代码可以帮助理解代码的执行流程,以及快速定位问题。
资源管理:
使用 SharedPreferences 存储少量数据是方便的,但对于大量数据或复杂数据结构,可能需要考虑使用数据库或其他存储方式。
代码维护:
良好的代码注释和结构可以提高代码的可读性和可维护性。
错误处理:
在实际开发中,要考虑到错误处理,例如用户输入为空或输入格式不正确的情况。
XML编码问题:
要注意XML编码的正确性,错误的实体编码会导致XML解析失败。