Android studio实现app登录界面

 更新时间:2022年4月23日 22:17  点击:430 作者:Run

本文实例为大家分享了Android studio设计app登录界面,供大家参考,具体内容如下

UI界面设计

在设计登录界面时,可以使用不同布局方式来实现该功能,通常情况下使用的是LinearLayout(线性布局)和TableLayout(表格布局),在本文中使用线性布局。登陆界面需要几项必不可少的控件,如下所示:

1、TextView:用于显示标题和“用户名"和"密码"的提示;

标题设置

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录页面"
        android:textSize="30dp"
        android:textColor="@android:color/black"
        android:layout_gravity="center_horizontal"/>

"用户名"提示

<TextView
       android:layout_width="65dp"
       android:layout_height="wrap_content"
       android:text="用户名:"
       android:textSize="16dp"
       android:textColor="@android:color/black"/>

"密码"提示:

<TextView
       android:layout_width="65dp"
       android:layout_height="wrap_content"
       android:text="密码:"
       android:textSize="16dp"
       android:textColor="@android:color/black"/>

2、EditView:用于输入"用户名"和"密码";

"用户名"输入框:

<EditText
       android:layout_width="264dp"
       android:layout_height="wrap_content"
       android:id="@+id/ed1"
       android:hint="请输入用户名"               
       android:textColor="@android:color/black"/>

"密码"输入框:

<EditText
       android:layout_width="264dp"
       android:layout_height="wrap_content"
       android:id="@+id/ed2"
       android:hint="请输入密码"
       android:textColor="@android:color/black"/>

3、Button:用于控制登录。

Button登录按钮:

<Button
        android:layout_width="285dp"
        android:layout_height="wrap_content"
        android:text="登录"
        android:textSize="20dp"
        android:id="@+id/bt"
        android:layout_gravity="center_horizontal" />/>

本文使用三层LinearLayout互相嵌套,第一层LinearLayout中包括标题(TextView)和第二层LinearLayout以及登录按钮(Button)。第一层LinearLayout使用垂直分布,即android:orientation=“vertical”。
第二层LinearLayout中嵌套两个第三层LinearLayout,且第二层LinearLayout为垂直分布,即android:orientation=“vertical”。
第三层的两个LinearLayout中各包含一个TextView和一个EditView,第三层LinearLayout为水平分布,即android:orientation=“horizontal”。

总的UI设计如下所示。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.activities.MainActivity"
    android:orientation="vertical"
    android:weightSum="1">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录页面"
        android:textSize="30dp"
        android:textColor="@android:color/black"
        android:layout_gravity="center_horizontal"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_weight="0.55">
        <LinearLayout
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="65dp"
                android:layout_height="wrap_content"
                android:text="用户名:"
                android:textSize="16dp"
                android:textColor="@android:color/black"/>

            <EditText
                android:layout_width="264dp"
                android:layout_height="wrap_content"
                android:id="@+id/ed1"
                android:hint="请输入用户名"
                android:textColor="@android:color/black"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="65dp"
                android:layout_height="wrap_content"
                android:text="密码:"
                android:textSize="16dp"
                android:textColor="@android:color/black"/>
            <EditText
                android:layout_width="264dp"
                android:layout_height="wrap_content"
                android:id="@+id/ed2"
                android:hint="请输入密码"
                android:textColor="@android:color/black"/>
        </LinearLayout>
    </LinearLayout>


    <Button
        android:layout_width="285dp"
        android:layout_height="wrap_content"
        android:text="登录"
        android:textSize="20dp"
        android:id="@+id/bt"
        android:layout_gravity="center_horizontal" />/>
</LinearLayout>

效果如图所示。

Java代码编写

若用户输入用户名或密码有误时,弹窗提示“用户名或密码输入有误,请更正后重新输入!”。
若用户没有输入用户名或密码,弹窗提示“用户名与密码不能为空!”。
当用户名与密码同时输入正确是,方可进入系统。

package com.example.activities;

import android.content.Intent;
import android.support.v7.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 usertext;
    private EditText passtext;
    private Button loginbutton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        usertext=(EditText)this.findViewById(R.id.ed1);
        passtext=(EditText)this.findViewById(R.id.ed2);
        loginbutton=(Button)this.findViewById(R.id.bt);
        loginbutton.setOnClickListener(new ButtonListener());
    }
    private class ButtonListener implements View.OnClickListener{
        @Override
        public void onClick(View v){
            String user=usertext.getText().toString();
            String pass=passtext.getText().toString();
            if (user.equals("")||pass.equals("")){
                Toast.makeText(MainActivity.this,"用户名与密码不能为空!",Toast.LENGTH_SHORT).show();
            }
            else if (user.equals("denglu")&&pass.equals("0000")){
                Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(MainActivity.this,TwoActivity.class);
                startActivity(intent);
            }
            else{
                Toast.makeText(MainActivity.this,"用户名或密码输入有误,请更正后重新输入!",Toast.LENGTH_SHORT).show();
            }
        }
    }
}

点击登录按钮之后,进入下一个界面,这时需在Manifest中添加第二个Activity的名称

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.activities">

    <application
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".TwoActivity"></activity>
    </application>

</manifest>

第二个界面的Activity需调用第二个xlm的layout,如下所示

package com.example.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class TwoActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.twoactivity);
    }
}

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

原文出处:https://blog.csdn.net/weixin_43230707/article/details/113890

[!--infotagslink--]

相关文章

  • Android子控件超出父控件的范围显示出来方法

    下面我们来看一篇关于Android子控件超出父控件的范围显示出来方法,希望这篇文章能够帮助到各位朋友,有碰到此问题的朋友可以进来看看哦。 <RelativeLayout xmlns:an...2016-10-02
  • php中登录后跳转回原来要访问的页面实例

    在很多网站用户先访问一个要登录的页面,但当时没有登录后来登录了,等待用户登录成功之后肯定希望返回到上次访问的页面,下面我就来给大家介绍登录后跳转回原来要访问的页...2016-11-25
  • Rstudio中安装package出现的问题及解决

    这篇文章主要介绍了Rstudio中安装package出现的问题及解决方案,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-05-06
  • php中用curl模拟登录discuz以及模拟发帖

    本文章完美的利用了php的curl功能实现模拟登录discuz以及模拟发帖,本教程供参考学习哦。 代码如下 复制代码 <?php $discuz_url = &lsquo;ht...2016-11-25
  • Android开发中findViewById()函数用法与简化

    findViewById方法在android开发中是获取页面控件的值了,有没有发现我们一个页面控件多了会反复研究写findViewById呢,下面我们一起来看它的简化方法。 Android中Fin...2016-09-20
  • Android模拟器上模拟来电和短信配置

    如果我们的项目需要做来电及短信的功能,那么我们就得在Android模拟器开发这些功能,本来就来告诉我们如何在Android模拟器上模拟来电及来短信的功能。 在Android模拟...2016-09-20
  • VSCode 配置uni-app的方法

    这篇文章主要介绍了VSCode 配置uni-app的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-07-11
  • 夜神android模拟器设置代理的方法

    夜神android模拟器如何设置代理呢?对于这个问题其实操作起来是非常的简单,下面小编来为各位详细介绍夜神android模拟器设置代理的方法,希望例子能够帮助到各位。 app...2016-09-20
  • android自定义动态设置Button样式【很常用】

    为了增强android应用的用户体验,我们可以在一些Button按钮上自定义动态的设置一些样式,比如交互时改变字体、颜色、背景图等。 今天来看一个通过重写Button来动态实...2016-09-20
  • Android WebView加载html5页面实例教程

    如果我们要在Android应用APP中加载html5页面,我们可以使用WebView,本文我们分享两个WebView加载html5页面实例应用。 实例一:WebView加载html5实现炫酷引导页面大多...2016-09-20
  • uniapp微信小程序:key失效的解决方法

    这篇文章主要介绍了uniapp微信小程序:key失效的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-01-20
  • 深入理解Android中View和ViewGroup

    深入理解Android中View和ViewGroup从组成架构上看,似乎ViewGroup在View之上,View需要继承ViewGroup,但实际上不是这样的。View是基类,ViewGroup是它的子类。本教程我们深...2016-09-20
  • Android自定义WebView网络视频播放控件例子

    下面我们来看一篇关于Android自定义WebView网络视频播放控件开发例子,这个文章写得非常的不错下面给各位共享一下吧。 因为业务需要,以下代码均以Youtube网站在线视...2016-10-02
  • Android用MemoryFile文件类读写进行性能优化

    java开发的Android应用,性能一直是一个大问题,,或许是Java语言本身比较消耗内存。本文我们来谈谈Android 性能优化之MemoryFile文件读写。 Android匿名共享内存对外A...2016-09-20
  • Android设置TextView竖着显示实例

    TextView默认是横着显示了,今天我们一起来看看Android设置TextView竖着显示如何来实现吧,今天我们就一起来看看操作细节,具体的如下所示。 在开发Android程序的时候,...2016-10-02
  • Ruby on Rails实现最基本的用户注册和登录功能的教程

    这里我们主要以has_secure_password的用户密码验证功能为中心,来讲解Ruby on Rails实现最基本的用户注册和登录功能的教程,需要的朋友可以参考下...2020-06-30
  • Visual Studio 2015下载和安装图文教程

    这篇文章主要为大家详细介绍了Visual Studio 2015下载和安装图文教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-22
  • PHP中SSO Cookie登录分析和实现

    什么是SSO?单点登录SSO(Single Sign-On)是身份管理中的一部分。SSO的一种较为通俗的定义是:SSO是指访问同一服务器不同应用中的受保护资源的同一用户,只需要登录一次,即通过一个应用中的安全验证后,再访问其他应用中的受保护...2015-11-08
  • android.os.BinderProxy cannot be cast to com解决办法

    本文章来给大家介绍关于android.os.BinderProxy cannot be cast to com解决办法,希望此文章对各位有帮助呀。 Android在绑定服务的时候出现java.lang.ClassCastExc...2016-09-20
  • php有效防止同一用户多次登录

    【问题描述】:同一用户在同一时间多次登录如果不能检测出来,是危险的。因为,你无法知道是否有其他用户在登录你的账户。如何禁止同一用户多次登录呢? 【解决方案】 (1) 每次登录,身份认证成功后,重新产生一个session_id。 s...2015-11-24