首页 > 其他分享 >每日总结 3.2

每日总结 3.2

时间:2023-03-02 19:01:02浏览次数:29  
标签:总结 tel String 每日 3.2 import android password name

今天学习了注册界面和数据库的链接。

<?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:orientation="vertical"
    android:background="@color/grey7d">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="用户名(学号):"
            android:layout_marginTop="30dp"
            android:textSize="25sp"
            android:textColor="#000000"
            android:layout_weight="1"/>
        <EditText
            android:id="@+id/userName"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="请输入用户名"
            android:layout_marginTop="30dp"
            android:textSize="20sp"
            android:textColor="#2196F3"
            android:layout_weight="2"/>
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="密码:"
            android:textSize="25sp"
            android:layout_marginTop="30dp"
            android:textColor="#000000"
            android:layout_weight="1"/>
        <EditText
            android:id="@+id/userpassword"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:hint="请输入密码"
            android:textSize="20sp"
            android:textColor="#2196F3"
            android:layout_weight="2"
            android:inputType="textWebPassword"/>
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="姓名:"
            android:layout_marginTop="30dp"
            android:textSize="25sp"
            android:textColor="#000000"
            android:layout_weight="1"/>
        <EditText
            android:id="@+id/userNames"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="请输入姓名"
            android:layout_marginTop="30dp"
            android:textSize="20sp"
            android:textColor="#2196F3"
            android:layout_weight="2"/>
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="手机号:"
            android:layout_marginTop="30dp"
            android:textSize="25sp"
            android:textColor="#000000"
            android:layout_weight="1"/>
        <EditText
            android:id="@+id/usertel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="请输入手机号"
            android:layout_marginTop="30dp"
            android:textSize="20sp"
            android:textColor="#2196F3"
            android:layout_weight="2"/>
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="班级:"
            android:layout_marginTop="30dp"
            android:textSize="25sp"
            android:textColor="#000000"
            android:layout_weight="1"/>
        <EditText
            android:id="@+id/userclass"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="请输入班级"
            android:layout_marginTop="30dp"
            android:textSize="20sp"
            android:textColor="#2196F3"
            android:layout_weight="2"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
            android:textColor="#000000"
            android:layout_marginTop="40dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:textSize="25sp"
            android:textAllCaps="false"
            android:text="确认"
            android:id="@+id/reday"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <Button
            android:textColor="#000000"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:textSize="25sp"
            android:textAllCaps="false"
            android:text="取消"
            android:id="@+id/back"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>

</LinearLayout>
package com.example.clock;

import static com.example.clock.R.id.userNames;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;


import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.clock.db.DatabaseHelper;
import com.example.clock.jiben.User;
import com.example.clock.jiben.User;

import java.util.ArrayList;

public class Register extends AppCompatActivity {

    private DatabaseHelper mSQLite;

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

        //找到各个控件
        Button btn_ready = findViewById(R.id.reday);
        Button btn_back = findViewById(R.id.back);
        EditText ed_name = findViewById(R.id.userName);
        EditText ed_password = findViewById(R.id.userpassword);
         EditText ed_name1 = findViewById(R.id.userNames);
         EditText ed_tel= findViewById(R.id.usertel);
        EditText ed_class = findViewById(R.id.userclass);

        //注册监听事件
        btn_ready.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取输入的用户名和密码
                String name = ed_name.getText().toString().trim();
                String password = ed_password.getText().toString().trim();
                String name1 =ed_name1.getText().toString().trim();
                String tel = ed_tel.getText().toString().trim();
                String classs=ed_class.getText().toString().trim();

                //获取数据库数据,判断用户名是否已存在
                ArrayList<User> data = mSQLite.getAllDATA();
                boolean flag = false;
                for(int i = 0; i < data.size(); i++){
                    User userdata = data.get(i);
                    if(name.equals(userdata.getName())){
                        flag = true;
                        break;
                    }else{
                        flag = false;
                    }
                }
                //判断用户名和密码是否为空
                if(!TextUtils.isEmpty(name)&&!TextUtils.isEmpty(password)){
                    if(!flag){
                        mSQLite.insert(name, password,name1,tel,classs);
                        Intent intent1 = new Intent(Register.this, Login.class);
                        startActivity(intent1);
                        finish();
                        Toast.makeText(Register.this, "注册成功", Toast.LENGTH_SHORT).show();
                    }
                    else{
                        Toast.makeText(Register.this, "用户名已被注册", Toast.LENGTH_SHORT).show();
                    }
                }
                else{
                    Toast.makeText(Register.this, "用户名与密码不能为空", Toast.LENGTH_SHORT).show();
                }
            }
        });

        //监听返回按钮
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent2 = new Intent(Register.this, Login.class);
                startActivity(intent2);
                finish();
            }
        });

        mSQLite = new DatabaseHelper(Register.this);
    }
}
package com.example.clock.db;



import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;

import com.example.clock.jiben.User;
import com.example.clock.jiben.User;

import java.util.ArrayList;
import java.util.jar.Attributes;

import javax.xml.namespace.NamespaceContext;

public class DatabaseHelper extends SQLiteOpenHelper {
    //创建一个数据库
    private SQLiteDatabase db;

    public DatabaseHelper(@Nullable Context context) {
        super(context, "db_test", null, 1);
        db = getReadableDatabase();
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        //在第一次创建数据库的时候,创建一些字段
        String sql = "create table user(_id integer, name varchar(50), password varchar(40),names varchar(20),tel varchar(20),class varchar(20))";
        db.execSQL(sql);//sql语句的执行函数
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //如果这个表中存在user,我们可以先把他去掉,然后重新创建
        String sql = "DROP TABLE IF EXISTS user";
        db.execSQL(sql);
        onCreate(db);
    }
    //为使项目结构更加紧凑,我们在此类中编写增删改查的函数,因为只有登录和注册界面,因此只涉及到写入数据库insert和query的操作
    public void insert(String name,String password ,String namele,String tel,String classs){
        db.execSQL("insert into user(name,password,names,tel,class)VALUES(?,?,?,?,?)",new Object[]{name,password,namele,tel,classs});
    }

    public ArrayList<User> getAllDATA(){//查询数据库
        ArrayList<User> list = new ArrayList<User>();
        //查询数据库中的数据,并将这些数据按照降序的情况排列
        Cursor cursor = db.query("user",null,null,null,null,null,"name DESC");
        while(cursor.moveToNext()){
            int index_name = cursor.getColumnIndex("name");
            int index_password = cursor.getColumnIndex("password");
            int index_names = cursor.getColumnIndex("names");
            int index_tel = cursor.getColumnIndex("tel");
            int index_classs=cursor.getColumnIndex("class");
            String name = cursor.getString(index_name);
            String password = cursor.getString(index_password);
            String names = cursor.getString(index_names);
            String tel = cursor.getString(index_tel);
            String classs = cursor.getString(index_classs);
            list.add(new User(name,password,names,tel,classs));
        }
        return list;
    }
}

 

标签:总结,tel,String,每日,3.2,import,android,password,name
From: https://www.cnblogs.com/syhxx/p/17173008.html

相关文章

  • 每日总结之可视化数据库
    在android里面下载datavasenavigate插件,然后运行虚拟机在虚拟机的文件管理器里面找到data0->data->包名->database,然后单独保存该数据库在DBbrowser里面导入该数据库......
  • 2023/3/1每日总结
    今天学习了页面的跳转以下为所写代码:Mainactivity.classpackagecom.example.myapplication;importandroidx.appcompat.app.AppCompatActivity;importandroid.conten......
  • 代码随想录算法训练营Day30 回溯算法| 332.重新安排行程 51. N皇后 37. 解数独 总结
    代码随想录算法训练营332.重新安排行程题目链接:332.重新安排行程给定一个机票的字符串二维数组[from,to],子数组中的两个成员分别表示飞机出发和降落的机场地点,对该行......
  • 2023/2/28每日总结
    今天学了一点点:把App的界面设计与代码逻辑分开的好处:使用XML文件描述APP界面,可以很方便地在AndroidStudio上预览界面效果。一个界面布局可以被多处代码复用,反过......
  • 闲话 23.3.2
    闲话APJifengc:您都已经切AGC的E了您太强了您都已经写一个美元符号了您太强了不懂发现APJ老师回家卷数奥的反常积分题太强了SoyTony:APJ是美国间谍——白......
  • 2023.3.2 JQuery介绍
    8.jQueryjQuery库,里面存在大量的Javascript函数jQuery是一个快速、简洁的JavaScript框架,它封装JavaScript常用的功能代码,提供一种简便的JavaScript设计模式,优化HTML文档......
  • 使用Visual studio 2022 工具的项目生成不同目标平台的总结
    前几天用visualstudio2022开发了一个桌面应用程序 结果出现了如下错误,“未能加载文件或程序集或它的某一个依赖项,试图加载格式不正确的的程序”  事出必有因!!! ......
  • 《5%的改变》总结
    心理学咨询案例合集,中心思想:5%的积极行动能产生正向循环,改变现状。当我们特别想要实现100%的改变的时候,可能反而一步都走不了,因为会被挫败、沮丧和自我否定压垮。但是如果......
  • 第一周总结
    第一周,进行了开学测试,对大数据进行导入,清洗,可视化展示   ......
  • 爆肝200+小时,总结出的 Creator 3.x 入门修炼指南!全免费
    大家好,我是晓衡!为了写这篇文章,给支持公众号的读者们一个交代,我准备了200+小时,公众号断更11天。我决定,不再选择逃避,不想再欺骗自己了。2023新年开工,不到十天时间,接二连三......