经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android Studio制作简单登录界面
来源:cnblogs  作者:秃兔TuT  时间:2024/4/15 14:56:13  对本文有异议

实现目标

应用线性布局设计登录界面,要求点击输入学号时弹出数字键盘界面,点击输入密码时弹出字母键盘,出现的文字、数字、尺寸等全部在values文件夹下相应.xml文件中设置好,使用时直接引用。当用户名或密码为空,显示一个提示信息“用户名与密码不能为空!”,当用户名和密码匹配,显示“登录成功”。

效果图如下:

实现过程

新建项目

新建一个项目如图所示:

UI设计

1.新建login.xml,选择线性布局

步骤如下:

设计登录页面

LinearLayout是线性布局,布局中的组件按照垂直或者水平方向进行排列

gravity:设置自身内部元素的对齐方式

layout_gravity:用来控制该控件在包含该控件的父控件中的位置

本设计采用垂直线性布局,如图所示:

控件类型: EditText 是一个允许用户输入和编辑文本的控件。

android:id: 这个属性为控件设置了一个唯一的ID(@+id/ed2),使得开发者可以在Java中通过这个ID来引用这个控件。

android:layout_width 和 android:layout_height: 这些属性定义了控件的宽度和高度。531dp 指定了宽度为531设备独立像素,wrap_content 表示高度会根据内容的大小自动调整。

实现代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:id="@+id/login"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:padding="25dp"
  8. android:background="@color/white"
  9. tools:context="com.example.myapplication1.LoginActivity"
  10. android:orientation="vertical"
  11. android:weightSum="1">
  12. <TextView
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:text="@string/login_page_title"
  16. android:textSize="@dimen/text_size_large"
  17. android:textColor="@android:color/black"
  18. android:layout_gravity="center_horizontal"/>
  19. <LinearLayout
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content"
  22. android:orientation="vertical"
  23. android:gravity="center"
  24. android:layout_weight="0.55">
  25. <LinearLayout
  26. android:layout_width="300dp"
  27. android:layout_height="wrap_content"
  28. android:orientation="horizontal">
  29. <TextView
  30. android:layout_width="@dimen/label_width"
  31. android:layout_height="wrap_content"
  32. android:text="@string/student_id_label"
  33. android:textSize="@dimen/text_size_medium"
  34. android:textColor="@android:color/black"/>
  35. <EditText
  36. android:id="@+id/ed1"
  37. android:layout_width="531dp"
  38. android:layout_height="wrap_content"
  39. android:minHeight="48dp"
  40. android:padding="12dp"
  41. android:hint="@string/student_id_hint"
  42. android:inputType="number"
  43. android:textColor="@color/black"
  44. android:textColorHint="@android:color/darker_gray"
  45. android:visibility="visible" />
  46. </LinearLayout>
  47. <LinearLayout
  48. android:layout_width="300dp"
  49. android:layout_height="wrap_content"
  50. android:orientation="horizontal">
  51. <TextView
  52. android:layout_width="@dimen/label_width"
  53. android:layout_height="wrap_content"
  54. android:text="@string/password_label"
  55. android:textSize="@dimen/text_size_medium"
  56. android:textColor="@android:color/black"/>
  57. <EditText
  58. android:id="@+id/ed2"
  59. android:layout_width="531dp"
  60. android:layout_height="wrap_content"
  61. android:minHeight="48dp"
  62. android:padding="12dp"
  63. android:hint="@string/password_hint"
  64. android:inputType="text"
  65. android:textColor="@color/black"
  66. android:textColorHint="@android:color/darker_gray"
  67. android:visibility="visible" />
  68. </LinearLayout>
  69. </LinearLayout>
  70. <Button
  71. android:layout_width="@dimen/login_button_width"
  72. android:layout_height="wrap_content"
  73. android:text="@string/login_button_text"
  74. android:textSize="@dimen/text_size_button"
  75. android:id="@+id/bt"
  76. android:layout_gravity="center_horizontal" />
  77. </LinearLayout>

2.将文本、数字和尺寸等资源从布局文件中移动到values文件夹下的相应.xml文件中并引用,需要按照以下步骤操作:

文本(字符串)资源:在values文件夹下的strings.xml文件中定义。

尺寸资源:在values文件夹下的dimens.xml文件中定义。

颜色资源:已经在colors.xml中定义,可以继续添加新的颜色或使用已有的颜色。

具体代码如下:

strings.xml

  1. <resources>
  2. <string name="login_page_title">登录页面</string>
  3. <string name="student_id_hint">请输入学号</string>
  4. <string name="password_hint">请输入密码</string>
  5. <string name="student_id_label">学号:</string>
  6. <string name="password_label">密码:</string>
  7. <string name="login_button_text">登录</string>
  8. </resources>

dimens.xml

  1. <resources>
  2. <dimen name="text_size_large">30dp</dimen>
  3. <dimen name="text_size_medium">18dp</dimen>
  4. <dimen name="login_button_width">285dp</dimen>
  5. <dimen name="login_input_width">300dp</dimen>
  6. <dimen name="label_width">65dp</dimen>
  7. <dimen name="text_size_button">20dp</dimen>
  8. </resources>

调用

1.新建一个LoginActivity进行调用,如图所示:

定义一个登录界面的行为:包含两个文本输入框(EditText)用于输入用户名和密码,以及一个按钮(Button)用于提交登录信息。

成员变量:

  • usertext 和 passtext 是EditText类型的变量,分别用于获取用户输入的用户名和密码。

onCreate方法:

  • 在onCreate方法中,首先调用super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)来初始化界面。

ButtonListener 类:

  • ButtonListener实现了View.OnClickListener接口,用于处理按钮点击事件。

  • 在其onClick方法中,首先获取usertext和passtext中的文本内容。

  • 然后,通过一系列的条件判断,检查用户名和密码是否为空,是否匹配预设的正确用户名("2021")和密码("abc")。

  • 如果用户名或密码为空,显示一个提示信息“用户名与密码不能为空!”。

  • 如果用户名和密码匹配,显示“登录成功”。

具体实现代码如下:

  1. package com.example.myapplication1;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.widget.Button;
  5. import android.widget.EditText;
  6. import android.widget.Toast;
  7. import android.view.View;
  8. import androidx.appcompat.app.AppCompatActivity;
  9. public class LoginActivity extends AppCompatActivity {
  10. private EditText usertext;
  11. private EditText passtext;
  12. private Button loginbutton;
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.login);
  17. usertext=(EditText)this.findViewById(R.id.ed1);
  18. passtext=(EditText)this.findViewById(R.id.ed2);
  19. loginbutton=(Button)this.findViewById(R.id.bt);
  20. loginbutton.setOnClickListener(new ButtonListener());
  21. }
  22. private class ButtonListener implements View.OnClickListener{
  23. @Override
  24. public void onClick(View v){
  25. String user=usertext.getText().toString();
  26. String pass=passtext.getText().toString();
  27. if (user.equals("")||pass.equals("")){
  28. Toast.makeText(LoginActivity.this,"用户名与密码不能为空!",Toast.LENGTH_SHORT).show();
  29. }
  30. else if (user.equals("2021")&&pass.equals("abc")){
  31. Toast.makeText(LoginActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
  32. }
  33. else{
  34. Toast.makeText(LoginActivity.this,"用户名或密码输入有误,请更正后重新输入!",Toast.LENGTH_SHORT).show();
  35. }
  36. }
  37. }
  38. }

配置文件

AndroidManifest.xml是整个Android应用程序的全局面描述配置文件

清单文件中通常包含以下六项信息:

  • 声明应用程序的包名: 用来识别应用程序的唯一标志

  • 描述应用程序组件

  • 确定宿主应用组件进程

  • 声明应用程序拥有的权限

  • 定义应用程序所支持API的最低等级

  • 列举应用程序必须链接的库

添加LoginActivity到 AndroidManifest.xml中

具体代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools">
  4. <application
  5. android:allowBackup="true"
  6. android:dataExtractionRules="@xml/data_extraction_rules"
  7. android:fullBackupContent="@xml/backup_rules"
  8. android:icon="@mipmap/ic_launcher"
  9. android:label="@string/app_name"
  10. android:roundIcon="@mipmap/ic_launcher_round"
  11. android:supportsRtl="true"
  12. android:theme="@style/Theme.AppCompat"
  13. tools:targetApi="31">
  14. <activity
  15. android:name=".LoginActivity"
  16. android:exported="true"
  17. android:label="@string/app_name"
  18. android:theme="@style/Theme.AppCompat">
  19. <intent-filter>
  20. <action android:name="android.intent.action.MAIN" />
  21. <category android:name="android.intent.category.LAUNCHER" />
  22. </intent-filter>
  23. </activity>
  24. </application>
  25. </manifest>

总结

以上就是简单的登录界面的设计的所有内容,简单介绍了线性布局以及相应属性的应用。

如果这篇文章对你或你的朋友有帮助的话,请多多支持和分享,让更多的人受益。同时,如果你有任何问题或疑问,也欢迎在下方留言,我会尽快回复并帮助你解决问题。让我们一起共同进步,共同学习!

原文链接:https://www.cnblogs.com/tututut/p/18125357

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号