赞
踩
目录
提前展示项目结构:
在layout目录下面创建activity_login.xml和activity_main.xml文件
activity_login.xml完整代码
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:padding="16dp"
- tools:context=".MainActivity">
-
- <EditText
- android:id="@+id/et_username"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="用户名" />
-
- <EditText
- android:id="@+id/et_password"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="密码"
- android:inputType="textPassword" />
-
- <Button
- android:id="@+id/btn_login"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="登录" />
-
- <Button
- android:id="@+id/btn_register"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="注册" />
-
- </LinearLayout>
activity_main.xml完整代码
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <ListView
- android:id="@+id/listview"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"/>
- </LinearLayout>
- package com.example.damn;
-
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
-
- public class MainActivity extends AppCompatActivity {
-
- private EditText etUsername;
- private EditText etPassword;
- private Button btnLogin;
- private Button btnRegister;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
-
- etUsername = findViewById(R.id.et_username);
- etPassword = findViewById(R.id.et_password);
- btnLogin = findViewById(R.id.btn_login);
- btnRegister = findViewById(R.id.btn_register);
-
- btnLogin.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // 在这里处理登录逻辑
- String username = etUsername.getText().toString();
- String password = etPassword.getText().toString();
- if (username.isEmpty() || password.isEmpty()) {
- Toast.makeText(MainActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
- } else {
- // 调用登录接口,验证用户名和密码是否正确
- }
- }
- });
-
- btnRegister.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // 在这里处理注册逻辑
- String username = etUsername.getText().toString();
- String password = etPassword.getText().toString();
- if (username.isEmpty() || password.isEmpty()) {
- Toast.makeText(MainActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
- } else {
- // 调用注册接口,将用户名和密码发送到服务器进行注册
- }
- }
- });
- }
- }
AndroidManifest.xml
在main目录下改写AndroidManifest.xml,将主程序入口改成MainActivity
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools">
-
- <application
- android:allowBackup="true"
- android:dataExtractionRules="@xml/data_extraction_rules"
- android:fullBackupContent="@xml/backup_rules"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/Theme.Damn"
- tools:targetApi="31">
- <activity
- android:name=".MainActivity"
- android:exported="true">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
-
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
-
- </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,并将其设置为应用的主入口。这样,我们就完成了一个简单的登录/注册界面的设计。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。