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

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

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

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

查询数据:

 我们模仿一下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();


    }
}

 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/733080.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-27

发表评论

登录后才能评论

评论列表(0条)

保存