• 移动终端开发实验五——对话框的使用


    运行结果

     

    实验目的

    1. 掌握Toast组件的使用方法;
    2. 掌握AertDialog组件的使用方法;
    3. 掌握对话框按钮监听器的使用方法;
    4. 能够熟练应用各种布局管理器和控件进行界面设计。

    实验内容及步骤

                设计一款APP的登录和注册功能,在登录界面点击“登录”按钮,对输入的用户名、密码进行验证(要求:用户名以邮箱登录,符合邮箱格式;密码必须以字母、数字和符号组合,并以字母开头),对不符合格式要求的通过toast组件进行提示;在登录界面点击“注册”按钮,弹出注册对话框进行注册操作(对话框中确定和取消按钮暂不需事件处理)。

    步骤:

    • Layout文件夹中建立登录布局文件activity_login.xml和注册布局文件activity_register.xml
    • 为“登录”按钮编写单击事件,实现登录验证(下列已给出部分代码,请同学们补充完整)
    • 为“注册”按钮编写单击事件,实现注册界面显示(下列已给出部分代码,请同学们补充完整)

    参考资料:

    1)以下是drawable文件,需复制到drawable文件夹。
    hat.png:注册图标
    login_logo.png:登录图标
    

    图片资源链接 提取码:1234

    1. Button register_btn = findViewById(R.id.register_btn);
    2. Button login_btn = findViewById(R.id.login_btn);
    3. register_btn.setOnClickListener(new View.OnClickListener() {
    4.     @Override
    5.     public void onClick(View v) {
    6.         RelativeLayout loginForm = (RelativeLayout) getLayoutInflater()
    7.                 .inflate(R.layout.activity_register, null);
    8. //创建AlertDialog并显示,设置布局文件为activity_register、图标为hat.png、标题为“快速注册”、按钮包括“取消”和“注册”
    9.            }
    10. });
    11. login_btn.setOnClickListener(new View.OnClickListener() {
    12.     @Override
    13.     public void onClick(View v) {
    14.         TextView t1=findViewById(R.id.login_password);
    15.         Pattern pattern = Pattern.compile("[_0-9a-z]+");
    16.         boolean tf = pattern.matcher(t1.getText()).matches();
    17.         if(!tf) {
    18.             //创建Toast提示“密码格式错误”,设置显示位置“居中显示”、字体颜色“红色”、显示时间3.5秒(Toast.LENGTH_LONG)
    19.         }
    20.     }
    21. });

    1、将图片复制到对应的文件夹

     2、新建layout文件

     并命名为rigister_layout.xml(命名将来要一致)

    3、在activity_main.xml中做登陆注册首页布局

     【参考代码】

    1. "1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. tools:context=".MainActivity"
    8. android:orientation="vertical"
    9. android:gravity="center">
    10. <ImageView
    11. android:id="@+id/img"
    12. android:layout_width="150dp"
    13. android:layout_height="150dp"
    14. android:src="@drawable/login_logo"
    15. android:scaleType="centerCrop"
    16. android:layout_gravity="center_horizontal"/>
    17. <LinearLayout
    18. android:layout_width="wrap_content"
    19. android:layout_height="wrap_content">
    20. <TextView
    21. android:id="@+id/zh_tv"
    22. android:layout_below="@+id/img"
    23. android:layout_width="wrap_content"
    24. android:layout_height="60dp"
    25. android:textSize="20dp"
    26. android:text="账号:"/>
    27. <EditText
    28. android:id="@+id/zh_et"
    29. android:layout_width="200dp"
    30. android:layout_height="60dp"
    31. android:hint="请输入账号(email)"
    32. android:inputType="textEmailAddress"
    33. android:singleLine="true"/>
    34. LinearLayout>
    35. <LinearLayout
    36. android:layout_width="wrap_content"
    37. android:layout_height="wrap_content">
    38. <TextView
    39. android:id="@+id/ps_tv"
    40. android:layout_below="@+id/zh_tv"
    41. android:layout_width="wrap_content"
    42. android:layout_height="60dp"
    43. android:textSize="20dp"
    44. android:text="密码:"/>
    45. <EditText
    46. android:id="@+id/ps_et"
    47. android:layout_below="@+id/zh_et"
    48. android:layout_toRightOf="@+id/ps_tv"
    49. android:layout_width="200dp"
    50. android:layout_height="60dp"
    51. android:hint="请输入密码"
    52. android:inputType="textPassword"
    53. android:singleLine="true"/>
    54. LinearLayout>
    55. <LinearLayout
    56. android:layout_width="wrap_content"
    57. android:layout_height="wrap_content">
    58. <Button
    59. android:id="@+id/login_btn"
    60. android:layout_width="wrap_content"
    61. android:layout_height="wrap_content"
    62. android:text="登录"/>
    63. <Button
    64. android:layout_marginLeft="80dp"
    65. android:id="@+id/register_btn"
    66. android:layout_width="wrap_content"
    67. android:layout_height="wrap_content"
    68. android:text="注册"/>
    69. LinearLayout>
    70. LinearLayout>

     4、在register_layout.xml中做对话框内部布局

     【参考代码】

    1. "1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:layout_width="wrap_content"
    4. android:layout_height="wrap_content"
    5. android:orientation="vertical"
    6. android:gravity="center"
    7. android:layout_marginTop="20dp">
    8. <LinearLayout
    9. android:layout_width="wrap_content"
    10. android:layout_height="wrap_content">
    11. <TextView
    12. android:id="@+id/nc_tv"
    13. android:layout_width="wrap_content"
    14. android:layout_height="60dp"
    15. android:text="昵称:"
    16. android:textSize="20dp"/>
    17. <EditText
    18. android:id="@+id/nc_et"
    19. android:layout_width="200dp"
    20. android:layout_height="60dp"
    21. android:hint="请输入用户名" />
    22. LinearLayout>
    23. <LinearLayout
    24. android:layout_width="wrap_content"
    25. android:layout_height="wrap_content">
    26. <TextView
    27. android:id="@+id/zh_tv"
    28. android:layout_width="wrap_content"
    29. android:layout_height="60dp"
    30. android:text="账号:"
    31. android:textSize="20dp"/>
    32. <EditText
    33. android:id="@+id/zh_et"
    34. android:layout_width="200dp"
    35. android:layout_height="60dp"
    36. android:hint="请输入正确的邮箱"
    37. android:inputType="textEmailAddress"/>
    38. LinearLayout>
    39. <LinearLayout
    40. android:layout_width="wrap_content"
    41. android:layout_height="wrap_content">
    42. <TextView
    43. android:id="@+id/ps_tv"
    44. android:layout_width="wrap_content"
    45. android:layout_height="60dp"
    46. android:text="密码:"
    47. android:textSize="20dp"
    48. />
    49. <EditText
    50. android:id="@+id/ps_et"
    51. android:layout_width="200dp"
    52. android:layout_height="60dp"
    53. android:hint="请输入密码"
    54. android:inputType="textPassword"/>
    55. LinearLayout>
    56. LinearLayout>

    不用做标题,因为待会java代码会设置对话框的标题栏字样。

    5、编写JAVA代码

    【参考代码】

    1. package com.example.a111;
    2. import androidx.appcompat.app.AlertDialog;
    3. import androidx.appcompat.app.AppCompatActivity;
    4. import android.graphics.Color;
    5. import android.os.Bundle;
    6. import android.view.Gravity;
    7. import android.view.View;
    8. import android.widget.Button;
    9. import android.widget.LinearLayout;
    10. import android.widget.RelativeLayout;
    11. import android.widget.TextView;
    12. import android.widget.Toast;
    13. import java.util.regex.Pattern;
    14. public class MainActivity extends AppCompatActivity {
    15. @Override
    16. protected void onCreate(Bundle savedInstanceState) {
    17. super.onCreate(savedInstanceState);
    18. setContentView(R.layout.activity_main);
    19. Button register_btn = findViewById(R.id.register_btn);
    20. Button login_btn = findViewById(R.id.login_btn);
    21. register_btn.setOnClickListener(new View.OnClickListener() {
    22. @Override
    23. public void onClick(View v) {
    24. LinearLayout loginForm = (LinearLayout) getLayoutInflater()
    25. .inflate(R.layout.register_layout, null);
    26. //创建AlertDialog并显示,设置布局文件为activity_register、图标为hat.png、标题为“快速注册”、按钮包括“取消”和“注册”
    27. AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
    28. builder.setIcon(R.drawable.hat)
    29. .setTitle("快速注册")
    30. .setView(loginForm)
    31. .setNegativeButton("取消",null)
    32. .setPositiveButton("注册",null)
    33. .create()
    34. .show();
    35. }
    36. });
    37. login_btn.setOnClickListener(new View.OnClickListener() {
    38. @Override
    39. public void onClick(View v) {
    40. TextView t1=findViewById(R.id.ps_et);
    41. Pattern pattern = Pattern.compile("[_0-9a-z]+");
    42. boolean tf = pattern.matcher(t1.getText()).matches();
    43. if(!tf) {
    44. //创建Toast提示“密码格式错误”,设置显示位置“居中显示”、字体颜色“红色”、显示时间3.5秒(Toast.LENGTH_LONG)
    45. Toast toast=new Toast(MainActivity.this);
    46. toast.setGravity(Gravity.CENTER,0,0);
    47. TextView textView=new TextView(MainActivity.this);
    48. textView.setTextSize(14f);
    49. textView.setTextColor(Color.RED);
    50. textView.setText("密码格式错误");
    51. toast.setDuration(Toast.LENGTH_LONG);
    52. toast.setView(textView);
    53. toast.show();
    54. }
    55. }
    56. });
    57. }
    58. }

    【代码详解】

    1、创建AlertDialog并显示,设置布局文件为activity_register、图标为hat.png、标题为“快速注册”、按钮包括“取消”和“注册”

    1. AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
    2. builder.setIcon(R.drawable.hat)
    3. .setTitle("快速注册")
    4. .setView(loginForm)
    5. .setNegativeButton("取消",null)
    6. .setPositiveButton("注册",null)
    7. .create()
    8. .show();

  • 相关阅读:
    GBase8s jdbc连接超时参数说明
    队列与堆栈:原理、区别、算法效率和应用场景的探究
    React SSG - 也写个 Demo 吧
    深入剖析如何设计订单超时自动取消的功能
    PerfView专题 (第十二篇):对 C# 下的 SDK 类库进行监控(大结局)
    2023.09.09青少年软件编程(Python)等级考试试卷(四级)
    340招募线上被试 | 大学生完美主义、就业焦虑情况调查
    一文搞懂JavaScript数组的特性
    《栈和队列》的模拟实现(顺序栈) (链队列)
    20221206英语学习
  • 原文地址:https://blog.csdn.net/m0_52177571/article/details/127712733