当前位置:   article > 正文

安卓项目:app注册/登录界面设计_创建项目,设计一个android用户登录页面,要求使用合理的布局,并且包括一些常用的控

创建项目,设计一个android用户登录页面,要求使用合理的布局,并且包括一些常用的控

目录

第一步:设计视图xml

第二步:编写登录和注册逻辑代码

运行效果展示:

总结:


提前展示项目结构:

第一步:设计视图xml

在layout目录下面创建activity_login.xml和activity_main.xml文件

activity_login.xml完整代码

  1. <?xml version="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. android:orientation="vertical"
  8. android:padding="16dp"
  9. tools:context=".MainActivity">
  10. <EditText
  11. android:id="@+id/et_username"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:hint="用户名" />
  15. <EditText
  16. android:id="@+id/et_password"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:hint="密码"
  20. android:inputType="textPassword" />
  21. <Button
  22. android:id="@+id/btn_login"
  23. android:layout_width="match_parent"
  24. android:layout_height="wrap_content"
  25. android:text="登录" />
  26. <Button
  27. android:id="@+id/btn_register"
  28. android:layout_width="match_parent"
  29. android:layout_height="wrap_content"
  30. android:text="注册" />
  31. </LinearLayout>

activity_main.xml完整代码

  1. <?xml version="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. <ListView
  6. android:id="@+id/listview"
  7. android:layout_width="wrap_content"
  8. android:layout_height="match_parent"/>
  9. </LinearLayout>

第二步:编写登录和注册逻辑代码

  1. package com.example.damn;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.EditText;
  7. import android.widget.Toast;
  8. public class MainActivity extends AppCompatActivity {
  9. private EditText etUsername;
  10. private EditText etPassword;
  11. private Button btnLogin;
  12. private Button btnRegister;
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_login);
  17. etUsername = findViewById(R.id.et_username);
  18. etPassword = findViewById(R.id.et_password);
  19. btnLogin = findViewById(R.id.btn_login);
  20. btnRegister = findViewById(R.id.btn_register);
  21. btnLogin.setOnClickListener(new View.OnClickListener() {
  22. @Override
  23. public void onClick(View v) {
  24. // 在这里处理登录逻辑
  25. String username = etUsername.getText().toString();
  26. String password = etPassword.getText().toString();
  27. if (username.isEmpty() || password.isEmpty()) {
  28. Toast.makeText(MainActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
  29. } else {
  30. // 调用登录接口,验证用户名和密码是否正确
  31. }
  32. }
  33. });
  34. btnRegister.setOnClickListener(new View.OnClickListener() {
  35. @Override
  36. public void onClick(View v) {
  37. // 在这里处理注册逻辑
  38. String username = etUsername.getText().toString();
  39. String password = etPassword.getText().toString();
  40. if (username.isEmpty() || password.isEmpty()) {
  41. Toast.makeText(MainActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
  42. } else {
  43. // 调用注册接口,将用户名和密码发送到服务器进行注册
  44. }
  45. }
  46. });
  47. }
  48. }

第三步:编写AndroidManifest.xml

在main目录下改写AndroidManifest.xml,将主程序入口改成MainActivity

  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.Damn"
  13. tools:targetApi="31">
  14. <activity
  15. android:name=".MainActivity"
  16. android:exported="true">
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23. </manifest>

运行效果展示:

登录和注册可以与数据库连用

总结:

本文主要介绍了如何设计一个安卓应用的注册/登录界面。首先,我们创建了一个新的Android项目,并在项目中添加了一个名为activity_login.xml的布局文件,用于定义登录界面的布局。接着,我们在项目目录下创建了一个名为MainActivity.java的Java类,用于处理登录/注册逻辑。

在布局文件中,我们使用了LinearLayout作为根布局,并设置了垂直方向和内边距为16dp。该布局包含两个EditText控件,分别用于输入用户名和密码。第一个EditText控件的id为et_username,提示文本为“用户名”;第二个EditText控件的id为et_password,提示文本为“密码”,并且输入类型设置为textPassword,表示输入的内容将被隐藏。

此外,还包含两个Button控件,分别用于登录和注册操作。第一个Button控件的id为btn_login,显示文本为“登录”;第二个Button控件的id为btn_register,显示文本为“注册”。

在MainActivity.java中,我们首先通过findViewById()方法获取到各个控件的引用。然后,为登录和注册按钮分别设置了点击事件监听器。在点击事件的回调方法中,我们首先检查用户名和密码是否为空,如果为空则弹出提示信息;否则,调用相应的接口进行登录或注册操作。

最后,在AndroidManifest.xml文件中添加了MainActivity,并将其设置为应用的主入口。这样,我们就完成了一个简单的登录/注册界面的设计。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/704986
推荐阅读
相关标签
  

闽ICP备14008679号