• 【快速解决】实验三 简单注册的实现《Android程序设计》实验报告


    目录

    前言 

    实验要求

    实验三 简单注册的实现

    实验目的:

    实验内容:

    实验提示: 无

    三、遇到的问题总结(如果有问题,请总结。如果没问题请写“无”)

    正文开始

    第一步建立项目

     第二步选择empty views activity点击next

    ​编辑

     第三步起名字,点击finish

    第四步对 activity _main.xml文件操作进行布局

    第五步,建立两个新文件,建立方法如下

    LoadingActivity.java中的代码

     ResultActivity.java中的代码

    activity_loading.xml代码 

     activity result.xml代码 

     MainActivity.java代码

    结语 


    前言 

    不会写《Android程序设计》实验报告没关系,小光手把手教你怎么写。

    下来我们先看一下,这次老师布置的实验要求(●'◡'●)

    实验要求

    实验三 简单注册的实现

    实验环境:Android Studio

    实验目的:
    1. 熟悉Android常见界面控件的使用,能够熟练掌握至少七种常见控件。
    2. 在自己搭建的开发环境完成一个注册页面的效果。
    实验内容:

    请按照以下要求完成操作,要求如下:

    1. 通过简单控件实现一个类似注册界面的效果;本界面为参考页面样式。

    最少实现七种控件,其中涉及至少2个页面:

    1. 文本必选:TextView、EditText
    2. 图片二选一:ImageView、ImageButton
    3. 按钮必选:Button、RadioButton、CheckBox
    4. 可选:togglebutton
    5. 进度条三选一:ProgressBar、SeekBar、RatingBar

    实验提示: 无
    • 程序源代码(只贴 .java和对应的.xml程序)

    • 运行结果界面
    三、遇到的问题总结(如果有问题,请总结。如果没问题请写“无”)

    注意:

     这篇文章中小光会带你们将老师布置的实验内容这部分,详细的讲解一下,大家可以直接复制代码,也可以学习一下怎么写,在这个基础上加以拓展,这样就不会查重了。

    正文开始

    小光的文章百分百可以成功!得到了大家的广泛认可

    第一步建立项目

     第二步选择empty views activity点击next
     第三步起名字,点击finish

    见到下面的页面我们的项目就算新建成功了 (是不是很简单),下来让我们开始,进行实验内容的实现。

    第四步对 activity _main.xml文件操作进行布局

     老师要求我们的布局是长这个样子的,我们看一下👇👇👇

    我知道大家肯定不会对页面进行布局,那我们就废话不多说,这里直接复制我帮大家写好的布局即可。

    下面展示一下我的布局

     activity _main.xml中的代码如下,直接复制即可

    1. "1.0" encoding="utf-8"?>
    2. <LinearLayout 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="10dp"
    7. android:background="@drawable/bk">
    8. <LinearLayout
    9. android:layout_width="match_parent"
    10. android:layout_height="wrap_content"
    11. android:orientation="horizontal"
    12. android:layout_marginTop="50dp">
    13. <ImageButton
    14. android:id="@+id/weixin"
    15. android:layout_width="0dp"
    16. android:layout_height="91dp"
    17. android:layout_gravity="center"
    18. android:layout_weight="1"
    19. android:background="@drawable/qq"
    20. android:contentDescription="微信注册" />
    21. <ImageButton
    22. android:id="@+id/qq"
    23. android:layout_width="0dp"
    24. android:layout_height="match_parent"
    25. android:layout_gravity="center"
    26. android:layout_marginLeft="15dp"
    27. android:layout_weight="1"
    28. android:background="@drawable/weixin"
    29. android:contentDescription="QQ注册" />
    30. LinearLayout>
    31. <Button
    32. android:id="@+id/dianzi"
    33. android:layout_width="378dp"
    34. android:layout_height="wrap_content"
    35. android:layout_gravity="center"
    36. android:background="@drawable/bg_username"
    37. android:text="使用电子邮箱注册"
    38. android:textColor="#FFFFFF"
    39. android:textSize="18sp"
    40. android:layout_marginTop="5dp"/>
    41. <EditText
    42. android:id="@+id/et_username"
    43. android:layout_width="match_parent"
    44. android:layout_height="50dp"
    45. android:hint="用户名"
    46. android:textColor="#00FFA1"
    47. android:textSize="16sp"
    48. android:maxLines="1"
    49. android:padding="10dp"
    50. android:layout_marginTop="30dp"
    51. android:background="@drawable/bg_transparent"/>
    52. <EditText
    53. android:id="@+id/et_password"
    54. android:layout_width="match_parent"
    55. android:layout_height="50dp"
    56. android:hint="密码"
    57. android:textColor="#00FFA1"
    58. android:textSize="16sp"
    59. android:maxLines="1"
    60. android:padding="10dp"
    61. android:inputType="textPassword"
    62. android:background="@drawable/bg_transparent"
    63. android:layout_marginTop="25dp"/>
    64. <RadioGroup
    65. android:id="@+id/rg_gender"
    66. android:layout_width="match_parent"
    67. android:layout_height="wrap_content"
    68. android:orientation="horizontal"
    69. android:layout_marginTop="50dp">
    70. <TextView
    71. android:id="@+id/tv_gender"
    72. android:layout_width="wrap_content"
    73. android:layout_height="wrap_content"
    74. android:text="性别:"
    75. android:textColor="#FFFFFF"
    76. android:textSize="20sp"/>
    77. <RadioButton
    78. android:id="@+id/rb_male"
    79. android:layout_width="wrap_content"
    80. android:layout_height="wrap_content"
    81. android:text="男"
    82. android:textColor="#FFFFFF"/>
    83. <RadioButton
    84. android:id="@+id/rb_female"
    85. android:layout_width="wrap_content"
    86. android:layout_height="wrap_content"
    87. android:text="女"
    88. android:textColor="#FFFFFF"/>
    89. RadioGroup>
    90. <LinearLayout
    91. android:layout_width="match_parent"
    92. android:layout_height="wrap_content"
    93. android:orientation="horizontal"
    94. android:layout_marginTop="5dp">
    95. <TextView
    96. android:id="@+id/tv_hobby"
    97. android:layout_width="wrap_content"
    98. android:layout_height="wrap_content"
    99. android:text="兴趣爱好:"
    100. android:textColor="#FFFFFF"
    101. android:textSize="20sp"/>
    102. <CheckBox
    103. android:id="@+id/checkbox_sing"
    104. android:layout_width="wrap_content"
    105. android:layout_height="wrap_content"
    106. android:text="唱歌"
    107. android:textColor="#FFFFFF"/>
    108. <CheckBox
    109. android:id="@+id/checkbox_dance"
    110. android:layout_width="wrap_content"
    111. android:layout_height="wrap_content"
    112. android:text="跳舞"
    113. android:textColor="#FFFFFF"/>
    114. <CheckBox
    115. android:id="@+id/checkbox_read"
    116. android:layout_width="wrap_content"
    117. android:layout_height="wrap_content"
    118. android:text="读书"
    119. android:textColor="#FFFFFF"/>
    120. LinearLayout>
    121. <Button
    122. android:id="@+id/btn_submit"
    123. android:layout_width="match_parent"
    124. android:layout_height="wrap_content"
    125. android:background="@drawable/but_1"
    126. android:text="提交"
    127. android:textColor="#FFFFFF"
    128. android:textSize="18sp"/>
    129. LinearLayout>

     然后你们看到的就会是这个结果,不要怕,小光会带你们解决

    问题就算这样子,没有图片,还有红色报错。没关系,小光下来教大家快速解决。

     这里大家先按照下面的操作来做

    (1)对着图片新建立xml文件

    最终建立好后的样子是这样的,这里注意文件名一定要一模一样

    这里建立好文件就行了,(为了方便大家)文件里面什么都不用写。这样 activity _main.xml就可以找到我们代码中的文件了。

    这样要是大家相加布局文件中的内容我会把内容放在最后,大家直接复制就行。

    然后加入背景图片(背景图片加入自己的背景图)

    这里主要是文件名字要一样,这样就可以直接用了。

     这里我们加一些输入框的背景(不然都看不清了)

     对应代码如下:

    1. <shape xmlns:android="http://schemas.android.com/apk/res/android">
    2. <solid android:color="#80CCCCCC" />
    3. <corners android:radius="8dp" />
    4. shape>

    效果展示

    这样我们的布局就做完了。下面的内容就非常简单了,写到这里我已经写累了,相信大家也看累了,那么下来我们就直接我能复制,加快我们的速度吧,gogogo(●'◡'●)

    第五步,建立两个新文件,建立方法如下

    第一个LoadingActivity 

    第二个ResultActivity  

    下载开始直接复制即可,无脑复制,快速结束战斗(估计要5花分钟)

    LoadingActivity.java中的代码
    1. package com.example.myapplication;
    2. import androidx.appcompat.app.AppCompatActivity;
    3. import android.os.Bundle;
    4. import android.widget.ImageButton;
    5. import android.widget.ProgressBar;
    6. import android.os.Handler;
    7. import android.widget.TextView;
    8. import android.content.Intent;
    9. public class LoadingActivity extends AppCompatActivity {
    10. private ProgressBar progressBar;
    11. @Override
    12. protected void onCreate(Bundle savedInstanceState) {
    13. super.onCreate(savedInstanceState);
    14. setContentView(R.layout.activity_loading);
    15. // 获取MainActivity传递的数据
    16. Intent intent = getIntent();
    17. if (intent != null) {
    18. String username = intent.getStringExtra("USERNAME");
    19. String password = intent.getStringExtra("PASSWORD");
    20. String gender = intent.getStringExtra("GENDER");
    21. String hobbies = intent.getStringExtra("HOBBIES");
    22. // 在这里可以根据需要使用这些数据
    23. // 显示ProgressBar
    24. progressBar = findViewById(R.id.progress_bar);
    25. // 在onCreate中添加
    26. Handler handler = new Handler();
    27. handler.postDelayed(new Runnable() {
    28. @Override
    29. public void run() {
    30. // 跳转到ResultActivity并传递数据
    31. Intent resultIntent = new Intent(LoadingActivity.this, ResultActivity.class);
    32. resultIntent.putExtra("USERNAME", username);
    33. resultIntent.putExtra("PASSWORD", password);
    34. resultIntent.putExtra("GENDER", gender);
    35. resultIntent.putExtra("HOBBIES", hobbies);
    36. startActivity(resultIntent);
    37. // 关闭当前Loading页面
    38. finish();
    39. }
    40. }, 3000); // 延迟3秒
    41. }
    42. }
    43. }

     复制完后会看见这样子

     ResultActivity.java中的代码
    1. package com.example.myapplication;
    2. import androidx.appcompat.app.AppCompatActivity;
    3. import android.os.Bundle;
    4. import android.widget.TextView;
    5. import android.content.Intent;
    6. public class ResultActivity extends AppCompatActivity {
    7. @Override
    8. protected void onCreate(Bundle savedInstanceState) {
    9. super.onCreate(savedInstanceState);
    10. setContentView(R.layout.activity_result);
    11. // 获取传递的数据
    12. Intent intent = getIntent();
    13. if (intent != null) {
    14. String username = intent.getStringExtra("USERNAME");
    15. String password = intent.getStringExtra("PASSWORD");
    16. String gender = intent.getStringExtra("GENDER");
    17. String hobbies = intent.getStringExtra("HOBBIES");
    18. // 将数据显示在 TextView 中
    19. TextView textUsername = findViewById(R.id.text_username);
    20. TextView textPassword = findViewById(R.id.text_password);
    21. TextView textGender = findViewById(R.id.text_gender);
    22. TextView textHobbies = findViewById(R.id.text_hobbies);
    23. textUsername.setText("Username: " + username);
    24. textPassword.setText("Password: " + password);
    25. textGender.setText("Gender: " + gender);
    26. textHobbies.setText("Hobbies: " + hobbies);
    27. }
    28. }
    29. }

    activity_loading.xml代码 
    1. "1.0" encoding="utf-8"?>
    2. <LinearLayout 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:gravity="center">
    7. <ProgressBar
    8. android:id="@+id/progress_bar"
    9. android:layout_width="wrap_content"
    10. android:layout_height="wrap_content"/>
    11. <TextView
    12. android:layout_width="wrap_content"
    13. android:layout_height="wrap_content"
    14. android:text="加载中..."/>
    15. LinearLayout>

     activity result.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/text_username"
    9. android:layout_width="wrap_content"
    10. android:layout_height="wrap_content"
    11. android:text="Username: "
    12. android:textSize="18sp"
    13. android:textColor="#000000"/>
    14. <TextView
    15. android:id="@+id/text_password"
    16. android:layout_width="wrap_content"
    17. android:layout_height="wrap_content"
    18. android:text="Password: "
    19. android:textSize="18sp"
    20. android:textColor="#000000"/>
    21. <TextView
    22. android:id="@+id/text_gender"
    23. android:layout_width="wrap_content"
    24. android:layout_height="wrap_content"
    25. android:text="Gender: "
    26. android:textSize="18sp"
    27. android:textColor="#000000"/>
    28. <TextView
    29. android:id="@+id/text_hobbies"
    30. android:layout_width="wrap_content"
    31. android:layout_height="wrap_content"
    32. android:text="Hobbies: "
    33. android:textSize="18sp"
    34. android:textColor="#000000"/>
    35. LinearLayout>

     MainActivity.java代码
    1. package com.example.myapplication;
    2. import androidx.activity.result.contract.ActivityResultContracts;
    3. import androidx.appcompat.app.AppCompatActivity;
    4. import android.content.Intent;
    5. import android.os.Bundle;
    6. import android.os.Parcelable;
    7. import android.view.View;
    8. import android.widget.Button;
    9. import android.widget.EditText;
    10. import android.widget.RadioGroup;
    11. import android.widget.RadioButton;
    12. import android.widget.Toast;
    13. import android.widget.CheckBox;
    14. import android.widget.ImageButton;
    15. import android.os.SystemClock;
    16. public class MainActivity extends AppCompatActivity {
    17. private EditText etUsername, etPassword;
    18. private RadioGroup rgGender;
    19. private RadioButton rbMale, rbFemale;
    20. private CheckBox checkboxSing, checkboxDance, checkboxRead;
    21. @Override
    22. protected void onCreate(Bundle savedInstanceState) {
    23. super.onCreate(savedInstanceState);
    24. setContentView(R.layout.activity_main);
    25. // 初始化视图
    26. etUsername = findViewById(R.id.et_username);
    27. etPassword = findViewById(R.id.et_password);
    28. rgGender = findViewById(R.id.rg_gender);
    29. rbMale = findViewById(R.id.rb_male);
    30. rbFemale = findViewById(R.id.rb_female);
    31. checkboxSing = findViewById(R.id.checkbox_sing);
    32. checkboxDance = findViewById(R.id.checkbox_dance);
    33. checkboxRead = findViewById(R.id.checkbox_read);
    34. // 假设有一个按钮点击事件触发传递数据到 ResultActivity
    35. Button submitButton = findViewById(R.id.btn_submit);
    36. submitButton.setOnClickListener(new View.OnClickListener() {
    37. @Override
    38. public void onClick(View v) {
    39. // 获取输入的文本
    40. String username = etUsername.getText().toString();
    41. String password = etPassword.getText().toString();
    42. // 获取选中的性别
    43. String gender = "";
    44. int selectedId = rgGender.getCheckedRadioButtonId();
    45. if (selectedId == R.id.rb_male) {
    46. gender = "男";
    47. } else if (selectedId == R.id.rb_female) {
    48. gender = "女";
    49. }
    50. // 获取选中的兴趣爱好
    51. StringBuilder hobbies = new StringBuilder();
    52. if (checkboxSing.isChecked()) {
    53. hobbies.append("唱歌 ");
    54. }
    55. if (checkboxDance.isChecked()) {
    56. hobbies.append("跳舞 ");
    57. }
    58. if (checkboxRead.isChecked()) {
    59. hobbies.append("读书");
    60. }
    61. // 创建一个Intent传递数据到ResultActivity
    62. Intent intent = new Intent(MainActivity.this, LoadingActivity.class);
    63. intent.putExtra("USERNAME", username);
    64. intent.putExtra("PASSWORD", password);
    65. intent.putExtra("GENDER", gender);
    66. intent.putExtra("HOBBIES", hobbies.toString());
    67. // 启动 ResultActivity
    68. startActivity(intent);
    69. }
    70. });
    71. }
    72. }

     很好,复制到这里,你的实验就算是完成了,接下来让我们运行看看效果吧

    点击运行 

    这样就结束了,感谢大家的观看,百分百成功,不会出现运行不了的问题。但一定要按照小光的代码复制,全部复制粘贴就行了。这里强调一下,文件的名字,所有的名字必须都和展示的图片中一模一样,不然会报错。

    结语 

    关注小光,小光帮你写实验报告(不是真的帮你写,就是我写好,你直接复制拿走的那种)也可以看看小光的其他文章,小光是全能的。

    🌌点击下方个人名片,交流会更方便哦~(欢迎到博主主页加入我们的 CodeCrafters联盟一起交流学习↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓   

  • 相关阅读:
    Nautilus Chain 联合香港数码港举办 BIG DEMO DAY活动,释放何信号?
    Java之多态数组
    【计算机视觉】BYOL 讲解
    基于微信小程序的宠物医院诊所小程序源码
    测试用例设计方法-场景法详解
    BUUCTF misc 专题(112)[MRCTF2020]pyFlag
    8.对象与锁标记
    ESP32 MicroPython 蜂鸣器及传感器的使用⑦
    【linux】进程的概念与控制
    算法笔记——LCR
  • 原文地址:https://blog.csdn.net/VLOKL/article/details/134476422