Android-实现登录功能

文章目录

  • 布局文件
  • MainActivity类
  • 跳转页面的布局及MainActivity类
  • 测试

布局文件

border.xml


<shape xmlns:android="http://schemas.android.com/apk/res/android" >





    <stroke
            android:width="0.01dp"
            android:color="#000" />

    <padding
            android:bottom="5dp"
            android:left="5dp"
            android:right="5dp"
            android:top="5dp" />


    <corners android:radius="5dp" />

shape>

<android.support.constraint.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"
        tools:context=".MainActivity">
<LinearLayout

                android:padding="5dp"
                android:background="@drawable/border"
                android:orientation="vertical"
                android:layout_gravity="center_horizontal"
                android:layout_width="360dp"
                android:layout_height="112dp">


            <LinearLayout

                    android:orientation="horizontal"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="50dp">


                <ImageView
                        android:layout_marginRight="15dp"
                        android:layout_gravity="center_vertical"
                        android:layout_width="30dp"
                        android:layout_height="30dp" app:srcCompat="@drawable/usn" android:id="@+id/usn"/>

                
                <EditText
                        android:singleLine="true"
                        android:background="@null"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:inputType="text"
                        android:hint="用户名"
                        android:ems="10"
                        android:id="@+id/username"/>
            LinearLayout>

            
            <View
                    android:layout_height="0.5dip"
                    android:background="#686868"
                    android:layout_width="match_parent"/>
            <LinearLayout

                    android:orientation="horizontal"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="50dp">


                <ImageView
                        android:layout_marginRight="15dp"
                        android:layout_gravity="center_vertical"
                        android:layout_width="30dp"
                        android:layout_height="30dp" app:srcCompat="@drawable/pwd" android:id="@+id/密码"/>
                <EditText
                        android:singleLine="true"
                        android:background="@null"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:inputType="textPassword"
                        android:hint="密码"
                        android:ems="10"
                        android:id="@+id/password"/>
            LinearLayout>

        LinearLayout>

        <Button
                android:layout_gravity="center_horizontal"
                android:background="#EF8D89"
                android:layout_marginTop="20dp"
                android:text="登    录"
                android:onClick="userLogin"
                android:layout_width="360dp"
                android:layout_height="wrap_content" android:id="@+id/login"/>
                
android.support.constraint.ConstraintLayout>

MainActivity类

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
     
    //访问数据库的类
    SQLiteDatabase db;

    //定义常量,作为消息的key
    public final  static String MESSAGE_KEY="com.android2";

	 @Override
    protected void onCreate(Bundle savedInstanceState) {
     

        super.onCreate(savedInstanceState);

        /**
         * (参数)1、context MainActivity
         * 2、name 数据库名
         * 3、
         * 4、版本号
         */
        final DatabaseHelper databaseHelper = new DatabaseHelper(this,"emis.db",null,2);

        //获得读取数据库权限
        db = databaseHelper.getReadableDatabase();
        setContentView(R.layout.activity_main);
    }
    /*响应*/
    private void userLogin() {
     
        EditText et1 = findViewById(R.id.username);
        String username = et1.getText().toString();
        EditText et2 = findViewById(R.id.password);
        String password = et2.getText().toString();

        //游标类Cursor 负责生成读写数据库的对象
        Cursor cursor = db.rawQuery("SELECT * FROM users WHERE username=? AND password=?",new String[]{
     username,password});

        //数据库中有此数据,登录成功
        if(cursor.getCount()>0){
     
            Intent intent = new Intent(this,ReceiveActivity.class);
            intent.putExtra(MESSAGE_KEY,username);
            startActivity(intent);
        }
        else{
     
            Toast.makeText(MainActivity.this,"用户名或密码错误!",Toast.LENGTH_SHORT).show();
        }


    }
}

跳转页面的布局及MainActivity类


<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ReceiveActivity"

>

   <TextView
           android:textSize="24dp"
           android:layout_gravity="center_vertical"
           android:id="@+id/output"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           />
LinearLayout>
package com.android02;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class ReceiveActivity extends AppCompatActivity {
     

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

        //获取intent引用
        Intent intent = getIntent();

        //以MESSAGE_KEY获取获取编辑框文字
        String message = intent.getStringExtra(MainActivity.MESSAGE_KEY);

        //以id获取TextView
        TextView textView = findViewById(R.id.output);

        //显示message
        textView.setText("欢迎!"+message);


    }
}

测试

Android-实现登录功能_第1张图片

在这里插入图片描述

完成~

Android-实现登录功能_第2张图片

你可能感兴趣的:(学习笔记,android,数据库)