Activity界面基本实验

【实验目的】

  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模式下,调试程序。

【实验结果】

AndroidManifest.xml

  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  2.     package="com.example.studentinfoapp">
  3.     <application
  4.         android:allowBackup="true"
  5.         android:icon="@mipmap/ic_launcher"
  6.         android:label="@string/app_name"
  7.         android:roundIcon="@mipmap/ic_launcher_round"
  8.         android:supportsRtl="true"
  9.         android:theme="@style/Theme.StudentInfoApp">
  10.         <activity android:name=".MainActivity"
  11.             android:exported="true"> <!-- 设置为true或false,取决于你的需求 -->
  12.             <intent-filter>
  13.                 <action android:name="android.intent.action.MAIN" />
  14.                 <category android:name="android.intent.category.LAUNCHER" />
  15.             </intent-filter>
  16.         </activity>
  17.         <activity android:name=".SecondActivity"
  18.             android:exported="true">
  19.             <!-- 如果需要从其他应用启动,可以添加以下intent-filter -->
  20.             <intent-filter>
  21.                 <action android:name="android.intent.action.VIEW" />
  22.                 <category android:name="android.intent.category.DEFAULT" />
  23.                 <!-- 可以添加数据类型等其他配置 -->
  24.             </intent-filter>
  25.         </activity>
  26.     </application>
  27. </manifest>

MainActivity.java

  1. package com.example.studentinfoapp;
  2. import android.content.Intent;
  3. import android.content.SharedPreferences;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.EditText;
  8. import androidx.annotation.Nullable;
  9. import androidx.appcompat.app.AppCompatActivity;
  10. public class MainActivity extends AppCompatActivity {
  11.     private EditText etName, etStudentId, etEmail;
  12.     private Button btnNext;
  13.     @Override
  14.     protected void onCreate(@Nullable Bundle savedInstanceState) {
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.activity_main);
  17.         etName = findViewById(R.id.etName);
  18.         etStudentId = findViewById(R.id.etStudentId);
  19.         etEmail = findViewById(R.id.etEmail);
  20.         btnNext = findViewById(R.id.btnNext);
  21.         btnNext.setOnClickListener(new View.OnClickListener() {
  22.             @Override
  23.             public void onClick(View v) {
  24.                 // 从输入框中获取数据
  25.                 String name = etName.getText().toString();
  26.                 String studentId = etStudentId.getText().toString();
  27.                 String email = etEmail.getText().toString();
  28.                 // 使用SharedPreferences存储数据
  29.                 SharedPreferences sharedPreferences = getSharedPreferences("StudentInfo", MODE_PRIVATE);
  30.                 SharedPreferences.Editor editor = sharedPreferences.edit();
  31.                 editor.putString("NAME", name);
  32.                 editor.putString("STUDENT_ID", studentId);
  33.                 editor.putString("EMAIL", email);
  34.                 editor.apply();
  35.                 // 创建Intent并传递数据到SecondActivity
  36.                 Intent intent = new Intent(MainActivity.this, SecondActivity.class);
  37.                 intent.putExtra("NAME", name);
  38.                 intent.putExtra("STUDENT_ID", studentId);
  39.                 intent.putExtra("EMAIL", email);
  40.                 startActivity(intent);
  41.             }
  42.         });
  43.     }
  44. }

SecondActivity.java

  1. package com.example.studentinfoapp;
  2. import static com.example.studentinfoapp.R.*;
  3. import android.annotation.SuppressLint;
  4. import android.content.Intent;
  5. import android.content.SharedPreferences;
  6. import android.os.Bundle;
  7. import android.widget.TextView;
  8. import androidx.annotation.Nullable;
  9. import androidx.appcompat.app.AppCompatActivity;
  10. public class SecondActivity extends AppCompatActivity {
  11.     private TextView tvName, tvStudentId, tvEmail;
  12.     @SuppressLint("MissingInflatedId")
  13.     @Override
  14.     protected void onCreate(@Nullable Bundle savedInstanceState) {
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.activity_second);
  17.         tvName = findViewById(R.id.tvName);
  18.         tvStudentId = findViewById(R.id.tvStudentId);
  19.         tvEmail = findViewById(R.id.tvEmail);
  20.         // 使用Intent传递的数据来更新UI
  21.         Intent intent = getIntent();
  22.         String nameFromIntent = ((Intent) intent).getStringExtra("NAME");
  23.         String studentIdFromIntent = intent.getStringExtra("STUDENT_ID");
  24.         String emailFromIntent = intent.getStringExtra("EMAIL");
  25.         // 显示从Intent获取的数据
  26.         tvName.setText("Name: " + nameFromIntent);
  27.         tvStudentId.setText("Student ID: " + studentIdFromIntent);
  28.         tvEmail.setText("Email: " + emailFromIntent);
  29.        
  30.     }
  31. }

Activity_main.xml

  1. <LinearLayout
  2.     xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical"
  6.     android:padding="16dp">
  7.     <EditText
  8.         android:id="@+id/etName"
  9.         android:layout_width="match_parent"
  10.         android:layout_height="wrap_content"
  11.         android:hint="Enter Name"/>
  12.     <EditText
  13.         android:id="@+id/etStudentId"
  14.         android:layout_width="match_parent"
  15.         android:layout_height="wrap_content"
  16.         android:hint="Enter Student ID"/>
  17.     <EditText
  18.         android:id="@+id/etEmail"
  19.         android:layout_width="match_parent"
  20.         android:layout_height="wrap_content"
  21.         android:hint="Enter Email"/>
  22.     <Button
  23.         android:id="@+id/btnNext"
  24.         android:layout_width="wrap_content"
  25.         android:layout_height="wrap_content"
  26.         android:text="Next"/>
  27. </LinearLayout>

Activity_second.xml

  1. <LinearLayout
  2.     xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical"
  6.     android:padding="16dp">
  7.     <TextView
  8.         android:id="@+id/tvName"
  9.         android:layout_width="wrap_content"
  10.         android:layout_height="wrap_content"
  11.         android:text="Name: "/>
  12.     <TextView
  13.         android:id="@+id/tvStudentId"
  14.         android:layout_width="wrap_content"
  15.         android:layout_height="wrap_content"
  16.         android:text="Student ID: "/>
  17.     <TextView
  18.         android:id="@+id/tvEmail"
  19.         android:layout_width="wrap_content"
  20.         android:layout_height="wrap_content"
  21.         android:text="Email: "/>
  22. </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解析失败。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值