Android 数据的查询,添加(用于登录注册)(付代码)

首先添加按钮并在设置点击事件:

 添加后怎么查看有没有添加成功:

查询数据:

 我们模仿一下QQ登录界面。

1首先我们创建3个activity,登录页面,欢迎页面,主页面。

欢迎页面:

 

 

package com.example.class7;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class Login_Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        EditText usernameText=findViewById(R.id.editTextTextPersonName);
        EditText passwordText=findViewById(R.id.editTextTextPersonName2);
        findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username=usernameText.getText().toString();
                String password=passwordText.getText().toString();
                if(username.equals("crq")&&password.equals("123456")){
                    //todo
                    SharedPreferences sharedPreferences=getSharedPreferences("user1",MODE_PRIVATE);
                    SharedPreferences.Editor editor=sharedPreferences.edit();
                    editor.putString("username",username);
                    editor.putBoolean("islogin",true);
                    editor.commit();

                    Intent intent=new Intent(Login_Activity.this,MainActivity.class);
                    startActivity(intent);

                }
                else{
                    Toast.makeText(Login_Activity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

 

package com.example.class7;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

public class Welcom_Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcom);
        //等待几秒钟创建线程
        Thread thread=new Thread()
        {
            @Override
            public void run() {
                super.run();
                try {
                    Thread.sleep(5000);
                    SharedPreferences sharedPreferences = getSharedPreferences("user1", MODE_PRIVATE);
                    Boolean isLogin=sharedPreferences.getBoolean("islogin",false);
                    if(isLogin)
                    {
                        Intent intent=new Intent(Welcom_Activity.this,MainActivity.class);
                        startActivity(intent);
                    }
                    else{
                        Intent intent=new Intent(Welcom_Activity.this,Login_Activity.class);
                        startActivity(intent);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        thread.start();


    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Android Studio 登录注册代码示例: MainActivity.java ``` public class MainActivity extends AppCompatActivity { EditText editTextUsername, editTextPassword; Button buttonLogin, buttonRegister; SQLiteDatabase db; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editTextUsername = findViewById(R.id.editTextUsername); editTextPassword = findViewById(R.id.editTextPassword); buttonLogin = findViewById(R.id.buttonLogin); buttonRegister = findViewById(R.id.buttonRegister); db = openOrCreateDatabase("UsersDB", MODE_PRIVATE, null); db.execSQL("CREATE TABLE IF NOT EXISTS users(username VARCHAR, password VARCHAR)"); buttonLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String username = editTextUsername.getText().toString().trim(); String password = editTextPassword.getText().toString().trim(); Cursor cursor = db.rawQuery("SELECT * FROM users WHERE username=? AND password=?", new String[]{username, password}); if (cursor.moveToFirst()) { Toast.makeText(getApplicationContext(), "Login successful!", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), "Invalid username or password.", Toast.LENGTH_SHORT).show(); } } }); buttonRegister.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String username = editTextUsername.getText().toString().trim(); String password = editTextPassword.getText().toString().trim(); if (username.equals("") || password.equals("")) { Toast.makeText(getApplicationContext(), "Please fill all fields.", Toast.LENGTH_SHORT).show(); } else { db.execSQL("INSERT INTO users VALUES('" + username + "', '" + password + "')"); Toast.makeText(getApplicationContext(), "Registration successful!", Toast.LENGTH_SHORT).show(); } } }); } } ``` activity_main.xml ``` <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" tools:context="com.example.loginregister.MainActivity"> <EditText android:id="@+id/editTextUsername" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:hint="Username" /> <EditText android:id="@+id/editTextPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/editTextUsername" android:layout_marginTop="16dp" android:hint="Password" android:inputType="textPassword" /> <Button android:id="@+id/buttonLogin" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/editTextPassword" android:layout_marginTop="16dp" android:text="Login" /> <Button android:id="@+id/buttonRegister" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/buttonLogin" android:layout_marginTop="16dp" android:text="Register" /> </RelativeLayout> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值