登陆界面详细代码

本文档详细介绍了如何在安卓平台上构建登录、注册和用户信息显示界面。首先,创建登录页面布局文件login.xml及对应的Login.java代码,接着创建注册页面布局和Register.java,然后是用户信息显示界面registerok.xml及RegisterokActivity.java,最后是登录成功界面的Activity1.java。每个步骤都包含关键代码实现界面交互和数据传递。
摘要由CSDN通过智能技术生成

登陆界面详细代码

1)创建“登录页面”程序
创建一个名为“登录页面”的布局文件login.xml,该文件用于展示登录信息。程序界面如下图所示:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_centerVertical="true"
                android:background="@drawable/login">

    <TextView
            android:id="@+id/jiemian"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:text= "登陆界面"
            android:textSize="40sp"
            android:textColor="#FFFFFF"
            android:background="#FF8F03"
            />
    <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名:"
            android:layout_marginTop="20dp"
            android:layout_below="@id/jiemian"
            android:textSize="30sp"
            android:textStyle="bold"
            android:textColor="#FF8000"/>
    <EditText
            android:id="@+id/username"
            android:layout_width="300dp"
            android:layout_height="40dp"
            android:layout_toRightOf="@id/name"
            android:layout_below="@id/jiemian"
            android:layout_marginTop="20dp"
            android:hint="请输入昵称"
            android:minLines="1"
            android:textSize="15sp"
            />
    <TextView
            android:id="@+id/edit_password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_below="@id/name"
            android:text="密    码:"
            android:textSize="30sp"
            android:textStyle="bold"
            android:textColor="#FF8000"/>
    <EditText
            android:id="@+id/password"
            android:layout_width="300dp"
            android:layout_height="40dp"
            android:layout_below="@id/name"
            android:layout_marginTop="20dp"
            android:hint="请输入密码"
            android:inputType="textPassword"
            android:minLines="1"
            android:textSize="15sp"
            android:layout_toRightOf="@id/edit_password"/>

    <Button
            android:id="@+id/submit"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="40dp"
android:layout_centerHorizontal="true"
            android:layout_below="@id/password"

            android:text="提交"
            android:textSize="30sp"
            android:textStyle="bold"/>
    <Button
            android:id="@+id/reg"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/submit"
            android:layout_marginTop="40dp"
            android:text="注册"
            android:layout_below="@id/password"
            android:textSize="30sp"
            android:textStyle="bold"
    android:onClick="Click"/>



</RelativeLayout>

创建“登录页面”的界面交互代码Login.java,该程序实现用户信息的输入与显示,并通过点击按钮实现界面的切换。关键代码如下:

package com.itheima.mylogin;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class Login extends AppCompatActivity {
   
private EditText unameEt, upassEt;
private Button submit,register;
@Override
public void onCreate(Bundle savedInstanceState) {
   
    super.onCreate(savedInstanceState);

    setContentView(R.layout.login);
    unameEt = (EditText) findViewById(R.id.username);
    upassEt = (EditText) findViewById(R.id.password);//register-findViewByld(R.id.register);
    submit = (Button) findViewById(R.id.submit);
    submit.setOnClickListener(
            new View.OnClickListener() {
   
                public void onClick(View v) {
   
                    //TODO Auto-generated method stub
                    String username = unameEt.getText().toString();
                    String password = upassEt.getText().toString();
                    SharedPreferences references = getSharedPreferences("account", Context.MODE_PRIVATE);
                    String name = references.getString("username", "");
                    String psw = references.getString("password", "");
                    Log.i("name", name );
                    if (username.isEmpty() | password.isEmpty()) {
   
                        String sb = "用户名和密码不能为空";
                        Toast.makeText(Login.this, sb.toString(), Toast.LENGTH_LONG).show();
                    } else if (name.equals(username) && psw.equals(password)) {
   
                        Intent intent1 = new Intent(Login.this, LoginOk.class);
                        startActivity(intent1);
                    } else {
   
                        String sb = "请输入正确的用户名和密码"
  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值