Android简单页面跳转

文章提供了Android应用中登录和注册页面的XML布局代码,包括LinearLayout,EditText,Button等元素的使用。MainActivity中实现了按钮点击事件,分别跳转至登录、注册页面以及首页。注册页面比登录页面多了邮箱输入项。此外,还展示了首页布局和两个按钮背景的drawable资源文件。
摘要由CSDN通过智能技术生成

代码结构

本次项目需要添加的“代码块”如下 👇

layout部分

login(登录页面)

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center|bottom">
        <ImageView
            android:layout_marginTop="100dp"
            android:layout_width="200dp"
            android:layout_height="100dp"
            android:src="@drawable/lxy"
            android:scaleType="centerCrop"/>
    </LinearLayout>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="灵心云·登录"
            android:textSize="20dp"
            android:gravity="center" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="15dp">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:hint="请输入用户名"
            android:textSize="18dp"
            android:background="@drawable/button_round"
            android:padding="10dp"
            />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:hint="输入用户密码"
            android:textSize="18dp"
            android:password="true"
            android:background="@drawable/button_round"
            android:padding="10dp"
            android:layout_marginTop="15dp"
            tools:ignore="Deprecated" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/bt1"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:text="登录"
            android:textSize="20dp"
            android:padding="10dp"
            android:textColor="#ffffff"
            android:background="@drawable/button_round2"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="15dp"/>
        <Button
            android:id="@+id/bt2"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:text="注册"
            android:textSize="20dp"
            android:padding="10dp"
            android:textColor="#ffffff"
            android:background="@drawable/button_round2"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="30dp"/>

    </LinearLayout>




</LinearLayout>

register(注册页面)

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center|bottom">
        <ImageView
            android:layout_marginTop="100dp"
            android:layout_width="200dp"
            android:layout_height="100dp"
            android:src="@drawable/lxy"
            android:scaleType="centerCrop"/>
    </LinearLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="灵心云·注册"
        android:textSize="20dp"
        android:gravity="center" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="15dp">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:hint="请输入用户名"
            android:textSize="18dp"
            android:background="@drawable/button_round"
            android:padding="10dp"
            />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:hint="输入用户密码"
            android:textSize="18dp"
            android:password="true"
            android:background="@drawable/button_round"
            android:padding="10dp"
            android:layout_marginTop="15dp"
            tools:ignore="Deprecated" />
        <EditText
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:hint="再次输入密码"
        android:textSize="18dp"
        android:password="true"
        android:background="@drawable/button_round"
        android:padding="10dp"
        android:layout_marginTop="15dp"
        tools:ignore="Deprecated" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:hint="输入邮箱地址"
            android:textSize="18dp"
            android:background="@drawable/button_round"
            android:padding="10dp"
            android:layout_marginTop="15dp"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/bt4"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:text="立即注册"
            android:textSize="20dp"
            android:padding="10dp"
            android:textColor="#ffffff"
            android:background="@drawable/button_round2"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="15dp"/>
        <Button
            android:id="@+id/bt5"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:text="返回登录"
            android:textSize="20dp"
            android:padding="10dp"
            android:textColor="#ffffff"
            android:background="@drawable/button_round2"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="30dp"/>

    </LinearLayout>




</LinearLayout>

home(首页)

<?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">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="这是首页"
        android:textSize="100dp"/>

</LinearLayout>

drawable部分

lxy.png

用于装饰的图片,自己随便加一张


button_round

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!--     设置圆角边框 -->
    <corners  android:radius="10dip"/>
    <!--设置填充色-->
    <solid android:color="#DCDCDC"/>
</shape>

button_round2

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!--     设置圆角边框 -->
    <corners  android:radius="10dip"/>
    <!--设置填充色-->
    <solid android:color="#007FFF"/>
</shape>

main部分

这里作者是自己全部粘贴过来了,代码中我已经做了相关注释,可以自己慢慢打,注意多用快捷键

MainActivity

package com.example.shixun4;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        //跳转首页
        Button button1 = (Button) findViewById(R.id.bt1);//点击登录按钮
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,home_1.class));
            }
        });
       //跳转注册
        Button button2 = (Button) findViewById(R.id.bt2);//点击注册按钮
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,register_1.class));
            }
        });
    }
}

给登录页面(login)加一个判断

login补充代码

对于用户名输入->    android:id="@+id/et1"
  
对于密码输入->    android:id="@+id/et2"

全局变量

private EditText et1,et2;

判断

获取编辑框控件对象
        et1 = (EditText) findViewById(R.id.et1);
        et2 = (EditText) findViewById(R.id.et2);
        Button button1= (Button) findViewById(R.id.bt1);

        //跳转首页
        button1 = (Button) findViewById(R.id.bt1);//点击登录按钮
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            
            public void onClick(View view){
                if (et1.getText().toString().equals("admin")&&et2.getText().toString().equals("123456")){
                    //输入正确跳转
                    startActivity(new Intent(MainActivity.this,home_1.class));
                }else {
                    //输入错误提示
                    Toast.makeText(MainActivity.this,"用户或密码错误",Toast.LENGTH_LONG).show();
                }
            }
        });

home_1

package com.example.shixun4;

import android.os.Bundle;

public class home_1 extends MainActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
    }
}



register_1

package com.example.shixun4;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class register_1 extends MainActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        //按钮4《立即注册》跳转
        Button button4 = (Button) findViewById(R.id.bt4);
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(register_1.this,MainActivity.class));
            }
        });

        //按钮《返回登录》跳转
        Button button5 = (Button) findViewById(R.id.bt5);
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(register_1.this,MainActivity.class));
            }
        });
    }
}

AndroidManifest部分

添加注册

<activity android:name=".register_1"></activity>
        <activity android:name=".home_1"></activity>

到这里基本上就OK了。如果你是Android studio,可能需要进行下面的配置


如果你的登录/注册不是下面这个样子,你需要vaues->themes


替换代码 👇

Theme.MaterialComponents.DayNight.NoActionBar.Bridge

替换位置👇

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值