Android实现登录注册页面(上)

简单的Android开发登录注册,这个是没有连数据库的。

首先,新建项目,新建一个登录页面LoginActivity和注册页面RegisterActivity。

下面是登录页面的代码: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"
    tools:context=".LoginActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:gravity="center_vertical">
 
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="账号:"
            android:textSize="25sp" />
 
        <EditText
            android:id="@+id/et_account"
            android:layout_width="match_parent"
            android:hint="请输入用户名或手机号"
            style="@style/MyEditStyle"
            android:layout_marginLeft="10dp"
            android:inputType="text"></EditText>
    </LinearLayout>
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:gravity="center_vertical">
 
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:"
            android:textSize="25sp" />
 
        <EditText
            android:id="@+id/et_password"
            android:layout_width="match_parent"
            android:hint="请输入密码"
            style="@style/MyEditStyle"
            android:layout_marginLeft="10dp"
            android:inputType="numberPassword"></EditText>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        >
        <CheckBox
            android:id="@+id/cb_remember"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="记住密码"></CheckBox>
    </LinearLayout>
    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        style="@style/MyBtnStyle"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"></Button>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorPrimary"
        android:text="还没有账号?"
        android:layout_gravity="right"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"
        android:onClick="toRegister"
        ></TextView>
 
</LinearLayout>

效果如图:

下面是注册页面的代码:activity_register.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"
    tools:context=".RegisterActivity"
    android:orientation="vertical">
 
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:gravity="center_vertical">
 
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="账  号:"
            android:textSize="25sp" />
 
        <EditText
            android:id="@+id/et_account"
            android:layout_width="match_parent"
            android:hint="请输入用户名或手机号"
            style="@style/MyEditStyle"
            android:layout_marginLeft="10dp"
            android:inputType="text"></EditText>
    </LinearLayout>
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:gravity="center_vertical">
 
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密  码:"
            android:textSize="25sp" />
 
        <EditText
            android:id="@+id/et_password"
            android:layout_width="match_parent"
            android:hint="请输入密码"
            style="@style/MyEditStyle"
            android:layout_marginLeft="10dp"
            android:inputType="numberPassword"></EditText>
    </LinearLayout>
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:gravity="center_vertical">
 
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="确认密码:"
            android:textSize="25sp" />
 
        <EditText
            android:id="@+id/et_password_Confirm"
            android:layout_width="match_parent"
            android:hint="请再次输入密码"
            style="@style/MyEditStyle"
            android:layout_marginLeft="10dp"
            android:inputType="numberPassword"></EditText>
    </LinearLayout>
 
    <Button
        android:id="@+id/btn_register"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="注册"
        style="@style/MyBtnStyle"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"></Button>
 
    <CheckBox
        android:id="@+id/cb_agree"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorPrimary"
        android:text="还没有账号?"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"></CheckBox>
 
</LinearLayout>

效果如图:

最后,在LoginActivity.java中,加入一串代码:getSupportActionBar().setTitle("登录");

在RegisterActivity.java中,加入一串代码:getSupportActionBar().setTitle("注册");

public class LoginActivity extends AppCompatActivity {
 
    public static final int REQUEST_CODE_REGISTER = 1;
    private static final String TAG="tag";
    private Button btnLogin;
    private EditText etAccount,etPassword;
    private CheckBox cbRemember;
    private String userName="a";
    private String pass="123";
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        getSupportActionBar().setTitle("登录");
     }
}

上面的代码是LoginActivity.java中的代码,其实要添加的只有一句,其他都写出来是为了让读者能看懂,具体写在哪里。这段代码的作用是: 使标题栏那边显示的文字是登录/注册,而不是一串默认的英文

下面两张图片,左边是未添加代码的效果,右边是添加代码后的效果

写到这里,页面整体布局大致完成了,下面,你们需要添加部分细节,来使得颜色和样式跟我一致。我在上面的xml文件代码中,有下面这个代码:

style="@style/MyEditStyle"和style="@style/MyBtnStyle"

你们在前面写的时候,可能会报错,很正常,因为这是引用控件样式的代码,你们需要设置一下这个样式,然后引用,就不会报错啦。

这是设置EditText和Button控件的样式,首先新建style.xml文件。在values中右键,New--Values Resource File,文件名为style。在style.xml文件中,写如下代码:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 
    <style name="MyBtnStyle">
        <item name="android:textColor">@color/white</item>
        <item name="android:textSize">25sp</item>
        <item name="android:background">@drawable/btn_bg_selector</item>
        <item name="android:layout_marginTop">20dp</item>
        <item name="android:layout_marginRight">20dp</item>
        <item name="android:layout_marginLeft">20dp</item>
    </style>
 
    <style name="MyEditStyle">
        <item name="android:textSize">18sp</item>
        <item name="android:background">@drawable/edit_text_bg</item>
        <item name="android:paddingLeft">10dp</item>
        <item name="android:layout_height">50dp</item>
    </style>
 
</resources>

这里面又引用了样式,是设置输入框的边框和按钮的背景颜色。继续下面的步骤:drawable右键--New--Drawable Resource File,文件名为:btn_bg_selector,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/colorPrimary"></item>
    <item android:state_pressed="false" android:drawable="@color/colorPrimaryDark"></item>
 
</selector>

drawable右键--New--Drawable Resource File,文件名为:edit_text_bg,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    >
 
    <stroke android:width="2dp" android:color="@color/colorPrimary"></stroke>
    <corners android:radius="10dp"></corners>
 
</shape>

最后,再调整一下导航栏和标题栏的颜色,就OK啦

colors.xml文件中,添加绿色这个颜色,如下代码,

<color name="colorPrimary">@color/green_500</color>
    <color name="colorPrimaryDark">@color/green_700</color>
    <color name="colorAccent">#E64A19</color>
 
    <color name="green_200">#A5D6A7</color>
    <color name="green_500">#4CAF50</color>
<color name="green_700">#4CAF50</color>

themes.xml文件中,只需要修改前两个item的内容,其他代码是为了你们参照一下位置,别改错了。代码如下:

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.TraditionalCulture" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryVariant">@color/colorPrimaryDark</item>
        <item name="colorOnPrimary">@color/white</item>
        <item name="colorAccent">@color/colorAccent</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
</resources>

到这里,你的登录注册页面就完成啦!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android Studio连接SQLite数据库的登录注册实现

    1.先看一下项目目录: 2.新建一个AS项目,创建如上图所示的目录结构,然后添加内容: (1)修改添加布局文件: activity_main.xml: <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android&quo

  • Android登录注册功能 数据库SQLite验证

    本文实例为大家分享了Android登录注册功能的具体代码,供大家参考,具体内容如下 展示效果 代码区 MainActivity(登录方法) public class MainActivity extends AppCompatActivity { @BindView(R.id.editText) EditText editText; @BindView(R.id.editText2) EditText editText2; @BindView(R.id.button) Button button

  • Android实现注册登录界面的实例代码

    本文讲述了在linux命令下导出导入.sql文件的方法.分享给大家供大家参考,具体如下: AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="online.geekgalaxy.layoutlearn

  • Android设计登录界面、找回密码、注册功能

    本文实例为大家分享了Android 登录.找回密码.注册功能的实现代码,供大家参考,具体内容如下 1.数据库的设计 我在数据库中添加了两张表,一张表用来存储用户信息,诸如用户名,密码,手机号等,可任意添加.另一张表用来存储上一个登录用户的账户信息,我是为了方便才另外创建了一张表去存储,而且这张表我设计了它只能存储一条信息,每次的存储都是对上一条记录的覆盖.事实上,我尝试过在存储用户信息的那张表内添加一个标识,用来标记上一次登录的是哪一个帐号,但是这样做的话,每次改变标识都需要遍历整张表,十分的麻

  • Android实现闪屏及注册和登录界面之间的切换效果

    在没给大家介绍正文之前先给大家说下实现思路: 先分别实现闪屏.注册界面.登录界面的活动,再用Intent将相关的活动连接起来,实现不同活动之间的跳转.此次试验代码较多,我只列出主要代码,详细的代码可用底部的下载链接下载. 一.实验效果图: 二.主要代码: (1)WelcomeActivity.Java(这部分代码实现的是第一页的欢迎页面) package com.example.flashscreendemo; import android.app.Activity; import androi

  • Android Studio连接MySql实现登录注册(附源代码)

    本文主要介绍了Android Studio连接MySql实现登录注册,分享给大家,具体如下: 一.创建工程 1.创建一个空白工程 2.随便起一个名称 3.设置网络连接权限 <uses-permission android:name="android.permission.INTERNET"/> 二.引入Mysql驱动包 1.切换到普通Java工程 2.在libs当中引入MySQL的jar包 将mysql的驱动包复制到libs当中 三.编写数据库和dao以及JDBC相关代码

  • Android客户端实现注册、登录详解(2)

    上文中介绍了安卓客户端与服务器交互,实现注册功能,Android客户端实现注册/登录详解(一) 本文将继续介绍App与服务器的交互实现登录和自动登录的功能,上文说到请求服务器进行注册主要是通过POST请求携带参数实现,起作用的主要代码: StringRequest request=new StringRequest(Method.POST, url, new Listener<String>() { //请求成功 @Override public void onResponse(String

  • Android开发之注册登录方法示例

    本文所述,继续上一篇关于Android端向服务器端发送数据的方法进一步完善注册登录的方法,由于版本问题出现一点瑕疵,今天经过调试已经解决,在这里给大家介绍一下. 在Android4.0以后版本的对于网络权限要求变得严格,致使上一篇所述的案例无法将数据发送到服务器端,当你一点击发送数据,Android控制台就会报错,错误当然是很让人头疼,基本上都是关于http的错误,所以可以肯定是Android虚拟机向服务器发送数据时出现了错误,经过一番检查与测试后才知道,4.0之后的版本,主线程中不允许调用网络

  • Android实现登录注册功能封装

    我们都知道Android应用软件基本上都会用到登录注册功能,那么对一个一个好的登录注册模块进行封装就势在必行了.这里给大家介绍一下我的第一个项目中所用到的登录注册功能的,已经对其进行封装,希望能对大家有帮助,如果有什么错误或者改进的话希望各位可以指出. 我们都知道登录注册系列功能的实现有以下几步: 注册账号 登录账号 (第三方账号登录) 记住密码 自动登录 修改密码 大体的流程如下 对于需要获取用户登录状态的操作,先判断用户是否已经登录. 如果用户已经登录,则继续后面的操作,否则,跳转到登录页面

  • Android客户端实现注册、登录详解(1)

    我们在开发安卓App时难免要与服务器打交道,尤其是对于用户账号信息的注册与登录更是每个Android开发人员必须掌握的技能,本文将对客户端的注册/登录功能的实现进行分析,不到之处还请指出. 在这里我们仅讨论客户端如何请求服务器进行注册,而服务器在收到客户端请求后进行的一系列操作并不在本文所述范围内,有兴趣大家可以参考 请求服务器 客户端在进行用户信息的注册和登录时一般使用post请求(携带参数)去服务器.以volley框架请求服务器为例,真正与服务器进行交互的就是如下代码: StringRequ

随机推荐