Android开发——实现简易登录功能

devtools/2024/10/18 0:35:24/

登录界面(mainActivity.xml)

<?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"android:id="@+id/wjh"  设置id,给mainActivity.java文件调用。android:background="@drawable/picture"android:orientation="vertical"><TextViewandroid:id="@+id/textView1"android:layout_width="match_parent"android:layout_height="143dp"android:text="@string/login"android:textSize="100sp"android:textColor="@color/black"android:gravity="center"></TextView><LinearLayoutandroid:layout_width="match_parent"android:layout_height="570dp"android:orientation="vertical"> 设置布局竖直排列<Spaceandroid:layout_width="match_parent"android:layout_height="100dp" /><EditTextandroid:id="@+id/editTextText2" android:layout_width="300dp"android:layout_height="wrap_content"android:ems="10"android:inputType="text"android:hint="@string/username"android:textColor="@color/black"android:textStyle="bold"android:layout_gravity="center"android:gravity="center"/><Spaceandroid:layout_width="match_parent"android:layout_height="100dp" /><EditTextandroid:id="@+id/editTextTextPassword6"android:layout_width="300sp"android:layout_height="wrap_content"android:ems="10"android:textStyle="bold"android:inputType="textPassword"android:hint="@string/password"android:gravity="center"android:layout_gravity="center"android:textSize="20sp"/><Spaceandroid:layout_width="match_parent"android:layout_height="100dp" /><Buttonandroid:id="@+id/button"android:layout_width="150dp"android:layout_height="83dp"android:layout_gravity="center"android:text="@string/button"android:onClick="Jump"android:textColor="@color/cardview_light_background"android:background="@drawable/p2"/></LinearLayout></LinearLayout>

效果图

 

跳转界面(activity_main2.xml)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/success"android:textColor="@color/black"android:textStyle="bold"android:textSize="60sp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid:id="@+id/textView"android:layout_width="200dp"android:layout_height="100dp"android:text="@string/success"android:textColor="@color/black"android:textSize="40sp"android:textStyle="bold"app:layout_constraintBottom_toTopOf="@+id/textView1"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.497"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintVertical_bias="1.0" /><Spaceandroid:layout_width="wrap_content"android:layout_height="200dp"app:layout_constraintBottom_toTopOf="@+id/textView"app:layout_constraintTop_toTopOf="parent"tools:layout_editor_absoluteX="199dp" /></androidx.constraintlayout.widget.ConstraintLayout>

如何将两个activity结合 

编辑mainActivity2.java文件

package com.example.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;onCreate() 方法是 Android 活动(Activity)的生命周期方法之一,它在活动被创建时调用。其主要作用是初始化活动的用户界面,设置布局内容和相关资源,并执行一些必要的初始化操作。
在 onCreate() 方法中,通常会执行以下操作:
1.设置布局:调用 setContentView() 方法设置活动的布局文件,即指定要在屏幕上显示的用户界面。
2.获取 Intent 数据:如果该活动需要从其他活动接收数据,通常会通过 getIntent() 方法获取传递过来的 Intent 对象,并从中提取所需的数据。
3.初始化界面元素:通过调用 findViewById() 方法初始化界面中的各种 UI 元素,例如按钮、文本框、图像视图等。
4.设置事件监听器:为界面元素设置事件监听器,以便在用户与之交互时执行相应的操作。
5.执行其他初始化操作:执行其他必要的初始化操作,例如数据加载、网络请求、权限检查等public class mainActivity2 extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main2); 引入mainActivity2.xml,使得两个文件相互关联。Intent intent = getIntent();intent用来存放数据,进行对数据的操作,在 Android 中,Intent 是用于在不同组件之间传递数据的对象String userInput = intent.getStringExtra("userInput");取到与自己通信的activity传递来的数据,intent内部存储数据采用的是key-value结构。此时的key是userInputTextView textView = findViewById(R.id.textView);与自己的activity.xml的文本框绑定textView.setText(userInput);将文本设置进文本框}
}

AndroidManifest.xml中注册新加的activity

</activity><activity android:name=".mainActivity2"/>

activity_main.xml 

<Buttonandroid:id="@+id/button"android:layout_width="150dp"android:layout_height="83dp"android:layout_gravity="center"android:text="@string/button"android:onClick="Jump" 注册Jump方法,在java文件中定义实现android:textColor="@color/cardview_light_background"android:background="@drawable/p2"/>

mainActivity.java (实现点击按钮的方法)

    public void Jump(View view){EditText editText = findViewById(R.id.editTextText2);String userInput = editText.getText().toString();取到main_activity.xml中的输入的用户名一栏的数据Intent intent = new Intent(this, mainActivity2.class);创建intent对象进行数据交互intent.putExtra("userInput", userInput);发送数据,设置key值为userInput,value为取到的数据startActivity(intent);执行}


http://www.ppmy.cn/devtools/7075.html

相关文章

设计模式|访问者模式(Visitor Pattern)

文章目录 结构举例优缺点优点缺点代码示例常见面试题访问者模式是一种行为设计模式,它允许在不改变已有类的情况下定义一组新的操作。 这些操作通常分散在不同的类中,但是希望能够对这些类的对象进行统一的处理。 访问者模式的核心思想是将操作从对象结构中分离出来,使得可以…

力扣62. 不同路径

Problem: 62. 不同路径 文章目录 题目描述思路复杂度Code 题目描述 思路 1.定义状态&#xff1a;定义二维数组dp[i][j]&#xff0c;用于表示记录当前到矩阵(i,j)位置处有多少不同路径&#xff1b; 2.状态初始化&#xff1a;由题目易知初始时位于矩阵的右上角&#xff0c;而且机…

UE5 Prediction 预测

在介绍预测功能前&#xff0c;先问个问题&#xff0c;为啥要有这个功能&#xff1f; 这个功能是在网络游戏所需的&#xff0c;单机游戏不需要。网络游戏主要牵扯到一个网络交互的问题&#xff0c;客户端和服务器之间交互是有延迟的&#xff0c;如果将操作数据提交等待服务器返回…

java中的继承

java中的继承 继承的关键字&#xff08;extends&#xff09; 文章目录 java中的继承方法重写方法重载super关键字 方法重写 两个方法的方法签名必须一致&#xff08;方法名和参数一样&#xff09;如果父类的方法的权限修饰符是private&#xff0c;那么该方法不能重写&#xf…

小试牛刀!

1.从双倍数组中还原原数组&#xff08;力扣&#xff0c;vector&#xff09; java式c解法。 class Solution { public:vector<int> findOriginalArray(vector<int>& changed) {int n changed.size();if(n % 2 1) return {};map<int, int> mp;for(int c…

单例模式详解

什么是单例模式 首先&#xff0c;单例模式是一种设计模式&#xff0c;按字面意思&#xff0c;指一个类只能创建一个对象&#xff0c;当创建出多个对象的时候&#xff0c;就会出现报错异常 单例模式为何出现&#xff1f; 1.资源共享:某些情况下&#xff0c;多个对象都需要共享一…

【Python】如何在Ubuntu上设置Python脚本开机自启

你不知道我为什么狠下心 盘旋在你看不见的高空里 多的是 你不知道的事 蝴蝶眨几次眼睛 才学会飞行 夜空洒满了星星 但几颗会落地 我飞行 但你坠落之际 很靠近 还听见呼吸 对不起 我却没捉紧你 &#x1f3b5; 王力宏《你不知道的事》 前置要求 确保你的Ub…

伪类与为元素的区别

一、两者的定义 1.伪类&#xff08;pseudo-class&#xff09;是一个以冒号作为前缀&#xff0c;被添加到一个选择器末尾的关键字&#xff0c;当你希望样式在特定状态才被呈现到指定的元素时&#xff0c;你可以往元素的选择器后面加上对应的伪类。 2.伪元素用于创建一些不在文档…