Android开发之登录界面实现

Android开发之登录界面实现

在《Android开发之第一个APP》的基础上,我们实现登陆的功能。

1 新建一个活动

右键单击包名-New-Activity-Gallery。
新建活动
依旧选择空白活动“Empty Activity”
活动模板
活动名称为“MainActivity”,勾选Generate Layout File,生成默认的布局文件;不要勾选Launcher Activity,这个选项是定义当前活动为主活动。(注:主活动是程序启动时默认显示的活动)
活动名
新的活动我们在布局文件里添加一个,用来显示“登陆成功”字样。
登录成功界面

2实现登录功能

2.1用到的控件

TextView,EditText,Button

2.2涉及到的知识点

按钮的点击事件监听器,读取EditText编辑框的内容,Toast消息提醒,和利用Intent跳转到另一个活动。
2.2.1定义编辑框和按钮组件,并映射到布局文件(以按钮为例)。
private Button btn_login;
private EditText edt_name;
private EditText edt_password;

btn_login=(Button)findViewById(R.id.btn_login);
edt_name=(EditText)findViewById(R.id.edt_name);
edt_password=(EditText)findViewById(R.id.edt_password);
2.2.2按钮的单击事件监听器。
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//添加逻辑
}
});
2.2.3获取EditText内容并与定义好的字符串比较
private String Name=“android”;
private String Password=“android”;
定义edt_name对象的getText()方法,其返回值类型为:CharSequence(一个描述字符串结构的接口,有String,StringBuffer,StringBuilder类),再调用toString()方法转换成字符串,最后调用equals方法进行比较。
edt_name.getText().toString().equals(Name)
2.2.4消息提醒
Toast.makeText(LoginActivity.this, “用户名错误!”, Toast.LENGTH_SHORT).show();
第一个参数为当前活动上下文,第二个参数为显示的文本内容,第三个参数为文本显示时间,有LENGTH_SHORT和LENGTH_LONG两种。
2.2.5 使用Intent跳转到另一个活动
Intent intent=new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
首先利用Intent方法构建一个Intent对象,Intent方法的参数有两个,第一个是当前活动上下文,第二个是要跳转的活动的类;最后调用startActivity方法启动活动。

3代码及效果图

布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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.asus.login.LoginActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="99dp"
        android:gravity="center_vertical|center_horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="账号"
            android:textSize="20dp"/>
        <EditText
            android:id="@+id/edt_name"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:hint="请输入用户名"
            android:textSize="15dp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout"
        android:layout_alignParentStart="true"
        android:gravity="center_vertical|center_horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码"
            android:textSize="20dp"/>
        <EditText
            android:id="@+id/edt_password"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:hint="请输入密码"
            android:textSize="15dp"
            android:inputType="textPassword"/>
    </LinearLayout>
    <Button
        android:id="@+id/btn_login"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="登陆"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

Java类文件

package com.example.asus.login;

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 LoginActivity extends AppCompatActivity {
    private String Name="android";
    private String Password="android";

    private Button btn_login;
    private EditText edt_name;
    private EditText edt_password;

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

        btn_login=(Button)findViewById(R.id.btn_login);
        edt_name=(EditText)findViewById(R.id.edt_name);
        edt_password=(EditText)findViewById(R.id.edt_password);

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(edt_name.getText().toString().equals(Name)){
                    if(edt_password.getText().toString().equals(Password)){
                        Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                        startActivity(intent);
                    }else{
                        Toast.makeText(LoginActivity.this, "密码错误!", Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(LoginActivity.this, "用户名错误!", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

效果图
登录界面
登录成功

发布了2 篇原创文章 · 获赞 2 · 访问量 1457

猜你喜欢

转载自blog.csdn.net/weixin_43879841/article/details/104408876