点击虚拟机的登录按钮

发布时间:2025-06-24 17:40:53  作者:北方职教升学中心  阅读量:363


  • 3. 在建立好所有联系后,我们就可以开始实现功能了
  • 4. 新建一个视图
  • 5.验证是否成功:
  • 前言

    赞美开源精神,分析学习经历,帮助后来者进步。但Language 要是 Java,Build configuration language 要是 Groovy DSL (build.gradle)
    创建项目第3步

  • 最后选择Finish,等待项目价值,如果你是完全第一次加载Android项目,恭喜你,可以去吃个饭了。 RegisterButton_01 = findViewById(R.id.RegisterButton); // 注册按钮 }

    3. 在建立好所有联系后,我们就可以开始实现功能了

    • 先明确登录功能,确保用户输入的账号和密码时正确时,进行登录功能(其实就是跳转到其他界面)。 Log_in_01 = findViewById(R.id.Log_in); // 登录按钮 //在绑定完成后,我们需要一个变量来接收这个绑定以便使用。

    4. 新建一个视图

    1. 在左侧的Project窗口,选中layout–>右击–>New–Activity–>Empty Views Activity
      在这里插入图片描述
    2. 更改名字并确定(Finish)
      在这里插入图片描述
    3. 我们就可以看到一个对应的Java文件和xml文件生成了
      请添加图片描述
    4. 更改activity_show.xml视图,以便查看登录功能的实现。

      package com.example.login_01;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import androidx.appcompat.app.AppCompatActivity;public class MainActivity extends AppCompatActivity {   //创建属性(全局变量)来保存 登录和注册按钮   private Button Log_in_01,RegisterButton_01;   @Override   protected void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       setContentView(R.layout.activity_main);       findView();       // 设置登录按钮的点击事件       Log_in_01.setOnClickListener(new View.OnClickListener() {           @Override           public void onClick(View view) {               login();           }       });   }   //我习惯写成一个方法,再放入类的入口。

      1. 认识初始文件:

      package com.example.login_01;import android.os.Bundle;import androidx.appcompat.app.AppCompatActivity;public class MainActivity extends AppCompatActivity {   @Override   protected void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       setContentView(R.layout.activity_main);   }}

      请添加图片描述

      1. 其中 setContentView(R.layout.activity_main);的activity_main对应的是xml文件的名称。 //1.最简单的实现,只是用于理解。 //3.使用后端接口,功能的完成在后端完成。

        Android环境

        IDE:Android studio
        Gradle,JDK的环境:
        gradle:8.1.0
        Gradle Version:8.0
        JDK:Java 17.0.6
        创建项目时环境:
        Language:Java
        Minimum SDK:26(最低SDK)
        Build configuration language :Groovy DSL (build.gradle)

        创建项目

        1. 打开Android studio

        2. 点击file --> New --> New Project
          创建项目第1步

        3. 选择:Empty Views Activity(空白模板)–> Next
          创建项目第2步

        4. 名称,路径根据自己情况自定义。请添加图片描述

        5. 点击虚拟机的登录按钮。
          • 在屏幕的最右边,选中Device Manager
            请添加图片描述

          • 点击Create Device请添加图片描述

          • 进行下载Android虚拟机
            请添加图片描述

          • 点击完成请添加图片描述

        6. 运行程序:请添加图片描述
        7. 运行后的初始界面,输入固定值账号密码。 private void findView() { //其中Log_in是xml文件中的 android:id="@+id/Log_in" //这样我们就绑定了一个控件了,其余控件的绑定也是一样的,使用findViewById(R.id.控件的具体id)方法。我们先来完成最简单的实现。当账号和密码都为我们想要的值时,进行具体操作 if(username.equals("xxx") && password.equals("xxxx")){//这里就是账号和密码都为我们想要的值 //进行具体的操作,使用Intent跳转其他界面 Show Intent intent = new Intent(MainActivity.this, Show.class); startActivity(intent);} //2.使用数据库比对,刚开始学习可以使用Android本地数据库SQlite,或者MySQL等主流数据库。
          • 1. 认识初始文件:
          • 2. 了解Java文件和xml文件的关联后,我们怎么找到控件的关联呢?这就需要请到findViewById()这个方法和xml文件中的android:id="@+id/XXX"。下一章我将分享我的基于数据库版本的登录注册功能的实现。这是项目生成时和创建xml文件时自动生成的,不用修改。下面是代码。
            public class MainActivity extends AppCompatActivity {   //创建属性(全局变量)来保存 登录和注册按钮   private Button Log_in_01,RegisterButton_01;   @Override   protected void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       setContentView(R.layout.activity_main);       findView();   }   //我习惯写成一个方法,再放入类的入口。因为是最简单实现,这个其他文档说。       Log_in_01 = findViewById(R.id.Log_in);  // 登录按钮       //在绑定完成后,我们需要一个变量来接收这个绑定以便使用。

        2. 了解Java文件和xml文件的关联后,我们怎么找到控件的关联呢?这就需要请到findViewById()这个方法和xml文件中的android:id=“@+id/XXX”。

        目录

        • 前言
        • Android环境
        • 创建项目
        • 认识项目
        • 登录功能的XML(activity_main.xml):
        • Java实现具体的功能:
          • 首先,我们需要先把Java文件和xml文件中的控件关联起来。
          • onCreate方法是每个类的入口,功能的实现都要放入这里。 private void findView() { //其中Log_in是xml文件中的 android:id="@+id/Log_in" //这样我们就绑定了一个控件了,其余控件的绑定也是一样的,使用findViewById(R.id.控件的具体id)方法。如果显示入如下图所示,那么恭喜你,成功完成了最基础的登录功能。
            请添加图片描述

          <?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"    android:background="#E1D8D8"    tools:context=".MainActivity">    <ScrollView        android:layout_width="match_parent"        android:layout_height="match_parent">        <androidx.constraintlayout.widget.ConstraintLayout            android:layout_width="match_parent"            android:layout_height="match_parent">            <TextView                android:id="@+id/textView0"                android:layout_width="match_parent"                android:layout_height="60dp"                android:layout_marginTop="120dp"                android:text="登录注册功能演示"                android:textSize="45dp"                android:gravity="center"                android:textStyle="bold"                android:textColor="@color/black"                app:layout_constraintEnd_toEndOf="parent"                app:layout_constraintStart_toStartOf="parent"                app:layout_constraintTop_toTopOf="parent" />            <EditText                android:id="@+id/login_username"                android:layout_width="300dp"                android:layout_height="50dp"                android:layout_marginTop="60dp"                android:layout_marginBottom="30dp"                android:inputType="text"                android:hint="请输入学号/工号"                android:gravity="center"                app:layout_constraintEnd_toEndOf="parent"                app:layout_constraintHorizontal_bias="0.497"                app:layout_constraintStart_toStartOf="parent"                app:layout_constraintTop_toBottomOf="@+id/textView0" />            <EditText                android:id="@+id/login_password"                android:layout_width="300dp"                android:layout_height="50dp"                android:layout_marginTop="30dp"                android:layout_marginBottom="30dp"                android:inputType="textPassword"                android:hint="请输入密码"                android:gravity="center"                app:layout_constraintEnd_toEndOf="parent"                app:layout_constraintStart_toStartOf="parent"                app:layout_constraintTop_toBottomOf="@+id/login_username" />            <Button                android:id="@+id/Log_in"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginTop="50dp"                android:elevation="5dp"                android:text="登录"                app:layout_constraintBottom_toBottomOf="parent"                app:layout_constraintEnd_toStartOf="@+id/RegisterButton"                app:layout_constraintStart_toStartOf="parent"                app:layout_constraintTop_toBottomOf="@+id/login_password" />            <Button                android:id="@+id/RegisterButton"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginTop="50dp"                android:elevation="5dp"                android:text="注册"                app:layout_constraintBottom_toBottomOf="parent"                app:layout_constraintEnd_toEndOf="parent"                app:layout_constraintStart_toEndOf="@id/Log_in"                app:layout_constraintTop_toBottomOf="@+id/login_password" />        </androidx.constraintlayout.widget.ConstraintLayout>    </ScrollView></androidx.constraintlayout.widget.ConstraintLayout> 

          Java实现具体的功能:

          首先,我们需要先把Java文件和xml文件中的控件关联起来。 RegisterButton_01 = findViewById(R.id.RegisterButton); // 注册按钮 } //实现登录功能的方法 private void login(){ // 从输入框获取账号和密码 // 这里演示在需要的时候,再绑定对应控件:并使用ToString方法把类型转换为我们需要的类型 String username = ToString(findViewById(R.id.login_username)); // 获取账号 String password = ToString(findViewById(R.id.login_password)); // 获取密码 //在获取完成用户的输入值后,进行登录功能的实现。

        • 加载项目完成,建议把Android视图改成Project视图
          在这里插入图片描述
          在这里插入图片描述

      认识项目

      请添加图片描述

      登录功能的XML(activity_main.xml):

      • EditText用于获取用户的输入

      • TextView只是提示

      • Button按钮用于确定登录和注册

      • 注意每个控件都有个: android:id="@+id/XXX"的语句,下面要用到。请添加图片描述