登录,注册,记住账号

package com.hanqi.kaoshi;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.hanqi.kaoshi.Zhuce.ZhuCeDAO;
import com.hanqi.kaoshi.Zhuce.ZhuCePOJO;

public class ZhuceActivity extends AppCompatActivity {
    EditText et_username;
    EditText et_userpassword,et_userpassword1;
    ZhuCeDAO zcd=new ZhuCeDAO(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhuce);
        et_username=(EditText)findViewById(R.id.et_username);
        et_userpassword=(EditText)findViewById(R.id.et_userpassword);
        et_userpassword1=(EditText)findViewById(R.id.et_userpassword1);
    }
    public void bt1_OnClick(View v)
    {
        String username=et_username.getText().toString();
        String userpassword=et_userpassword.getText().toString();
        if(username==null||username.trim().length()==0)
        {
            Toast.makeText(ZhuceActivity.this, "用户名不能为空,请正确填写用户名称", Toast.LENGTH_SHORT).show();
            return;
        }else if (zcd.getname(et_username.getText().toString()).size()>0) {
            Toast.makeText(ZhuceActivity.this, "用户已注册,请重新注册", Toast.LENGTH_SHORT).show();
        }else if (username!=null&&userpassword.trim().length()==0)
        {
            Toast.makeText(ZhuceActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
        }else if (et_userpassword.getText().toString().equals(et_userpassword1.getText().toString()))
        {
            ZhuCePOJO zhuce = new ZhuCePOJO(username,userpassword);
            long l = zcd.insert(zhuce);
            if (l > 0) {
                Toast.makeText(ZhuceActivity.this, "注册成功", Toast.LENGTH_SHORT).show();

            } else {
                Toast.makeText(ZhuceActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
            }
        }else
        {
            Toast.makeText(ZhuceActivity.this, "两次密码输入不一致,请重新输入", Toast.LENGTH_SHORT).show();
        }
    }
    public void bt2_OnClick(View v)
    {
        Intent intent=new Intent(this,Denglu.class);
        startActivity(intent);
    }

}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.hanqi.kaoshi.ZhuceActivity"
    android:background="@drawable/bg1"
    android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="30dp"
        android:textSize="30sp"
        android:textColor="#000000"
        android:gravity="center"
        android:text="考试系统注册界面"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="用户名称"
        android:textColor="#000000"
        android:textSize="20sp"
        android:id="@+id/et_username"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="用户密码"
        android:textColor="#000000"
        android:inputType="textPassword"
        android:textSize="20sp"
        android:id="@+id/et_userpassword"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="确认密码"
        android:textColor="#000000"
        android:inputType="textPassword"
        android:textSize="20sp"
        android:id="@+id/et_userpassword1"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        <Button
            android:layout_width="wrap_content"
            android:layout_marginRight="40dp"
            android:background="#00B2EE"
            android:textColor="#000000"
            android:layout_height="wrap_content"
            android:id="@+id/bt_denglu"
            android:padding="3dp"
            android:onClick="bt1_OnClick"
            android:drawableLeft="@drawable/login32x32"
            android:text="确定"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:onClick="bt2_OnClick"
            android:padding="3dp"
            android:background="#00B2EE"
            android:layout_gravity="center_horizontal"
            android:drawableLeft="@drawable/login_exit32x32"
            android:text="取消"/>
    </LinearLayout>
    <ListView
        android:layout_width="200dp"
        android:layout_height="0dp"
        android:id="@+id/lv_1"
        android:layout_weight="1"></ListView>


</LinearLayout>
View Code
package com.hanqi.kaoshi.Zhuce;

/**
 * Created by Administrator on 2016/7/2.
 */
public class ZhuCePOJO {
    private long id;
    private String name;
    private String password;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public ZhuCePOJO(long id, String name, String password) {
        this.id = id;
        this.name = name;
        this.password = password;
    }

    public ZhuCePOJO(String name, String password) {
        this.name = name;
        this.password = password;
    }
}
View Code
package com.hanqi.kaoshi.Zhuce;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/7/2.
 */
public class ZhuCeDAO {
    private DBHelper dh;
    private final String TABLE_NAME="t_zhuce";
    public ZhuCeDAO(Context context)
    {
        dh=new DBHelper(context);
    }
    //
    public long insert(ZhuCePOJO zhuce)
    {
        long rtn=0;
        SQLiteDatabase sd=dh.getReadableDatabase();
        ContentValues cv=new ContentValues();
        cv.put("name",zhuce.getName());
        cv.put("password",zhuce.getPassword());
        rtn=sd.insert(TABLE_NAME, null, cv);
        sd.close();
        return rtn;
    }
    //
    public int delete(long id)
    {
        SQLiteDatabase sd=dh.getReadableDatabase();
        int rtn=0;
        rtn=sd.delete(TABLE_NAME, "_id=?", new String[]{id + ""});
        sd.close();
        return rtn;
    }
    //
    public int update(ZhuCePOJO zhuce)
    {  int rtn=0;
        SQLiteDatabase sd=dh.getReadableDatabase();
        ContentValues cv=new ContentValues();
        cv.put("name",zhuce.getName());
        cv.put("password",zhuce.getPassword());
        rtn=sd.update(TABLE_NAME, cv, "_id=?", new String[]{zhuce.getId() + ""});
        sd.close();
        return rtn;
    }
    //查姓名,用于输入姓名自动提示
    public ArrayList<String> getStringname()
    {
        ArrayList<String> arrayList=new ArrayList<>();

        SQLiteDatabase sqLiteDatabase=dh.getWritableDatabase();

        Cursor cursor=sqLiteDatabase.query(TABLE_NAME, null, null, null, null, null, null);

        while (cursor.moveToNext())
        {
            ZhuCePOJO zhuCe=new ZhuCePOJO(cursor.getLong(0),cursor.getString(1),cursor.getString(2));

            arrayList.add(cursor.getString(1));
        }

        sqLiteDatabase.close();

        return arrayList;
    }

    //查姓名
    public ArrayList<ZhuCePOJO> getname(String string)
    {
        ArrayList<ZhuCePOJO> arrayList=new ArrayList<>();

        SQLiteDatabase sqLiteDatabase=dh.getWritableDatabase();

        Cursor cursor=sqLiteDatabase.query(TABLE_NAME, null, "name=? ", new String[]{string}, null, null, null);

        while (cursor.moveToNext())
        {
            ZhuCePOJO zhuCe=new ZhuCePOJO(cursor.getLong(0),cursor.getString(1),cursor.getString(2));

            arrayList.add(0,zhuCe);
        }

        sqLiteDatabase.close();

        return arrayList;
    }

    //查全部
    public ArrayList<ZhuCePOJO> getAll(String string,String string1)
    {
        ArrayList<ZhuCePOJO> arrayList=new ArrayList<>();

        SQLiteDatabase sqLiteDatabase=dh.getWritableDatabase();

        Cursor cursor=sqLiteDatabase.query(TABLE_NAME, null, "name=? and password=? ",
                new String[]{string,string1}, null, null, null);

        while (cursor.moveToNext())
        {
            ZhuCePOJO zhuCe=new ZhuCePOJO(cursor.getLong(0),cursor.getString(1),cursor.getString(2));

            arrayList.add(0,zhuCe);
        }

        sqLiteDatabase.close();

        return arrayList;
    }

}
View Code
package com.hanqi.kaoshi.Zhuce;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * Created by Administrator on 2016/7/2.
 */
public class DBHelper extends SQLiteOpenHelper{

    public DBHelper(Context context)
    {
        super(context, "zhuce2.db",null, 1);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql="CREATE TABLE t_zhuce " +
                "(_id  INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
                "name TEXT ,password  VARCHAR(20))";
        db.execSQL(sql);
        String sql2="CREATE TABLE t_exam " +
                "(_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
                " examname TEXT , name TEXT , score INTEGER , " +
                " starttime TEXT)";
        db.execSQL(sql2);

        Log.e("TAG", "表创建成功");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
View Code
package com.hanqi.kaoshi;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import com.hanqi.kaoshi.Zhuce.ZhuCeDAO;

import java.util.ArrayList;

public class Denglu extends AppCompatActivity {
    AutoCompleteTextView actv_name;
    EditText ed_password;
    SharedPreferences sp;
    Button bt_denglu;
    CheckBox cb_1;
    String name,pw, return_name,return_pw;
    ZhuCeDAO zhuCeDAO=new ZhuCeDAO(this);
    ArrayAdapter<String> arrayAdapter;
    //自动提示数组
    ArrayList<String> arrayList;
    SharedPreferences sp1;
    SharedPreferences.Editor editor;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_denglu);
        actv_name=(AutoCompleteTextView)findViewById(R.id.actv_name);
        ed_password=(EditText)findViewById(R.id.ed_password);
        cb_1=(CheckBox)findViewById(R.id.cb_1);
        sp1=getSharedPreferences("data1",MODE_PRIVATE);
        if(sp1.getBoolean("ISCHECK",true))
        {
            cb_1.setChecked(true);
            actv_name.setText(sp1.getString("name",""));
            ed_password.setText(sp1.getString("password",""));
        }
        bt_denglu=(Button)findViewById(R.id.bt_denglu);
        arrayList=zhuCeDAO.getStringname();
        arrayAdapter=new ArrayAdapter<String>(Denglu.this, android.R.layout.simple_expandable_list_item_1, arrayList);
        actv_name.setAdapter(arrayAdapter);


        //得到用户输入的用户名和密码
        //需要注册才能登录的功能,
        bt_denglu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                name = actv_name.getText().toString();
                pw = ed_password.getText().toString();
                sp = getSharedPreferences("data", MODE_PRIVATE);
                cb_1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        Boolean isChecked1 = cb_1.isChecked();
                        editor.putBoolean("ISCHECK", isChecked1);
                        editor.commit();
                    }
                });
                return_name = sp.getString("name", "");
                if (name.trim().length() == 0 || pw.trim().length() == 0) {
                    Toast.makeText(Denglu.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
                } else if (zhuCeDAO.getname(name).size() > 0)
                {
                    if (zhuCeDAO.getAll(name, pw).size() > 0)
                    {
                        if(cb_1.isChecked())
                    {
                        //记住用户名、密码、
                        editor = sp1.edit();
                        editor.putString("name", name);
                        editor.putString("password",pw);
                        editor.commit();
                    }
                        Intent intent = new Intent(Denglu.this, MainActivity.class);
                        intent.putExtra("name", name);
                        startActivity(intent);
                        finish();

                    }else {
                        Toast.makeText(Denglu.this, "密码错误", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(Denglu.this, "用户未注册", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }
    public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
        if (cb_1.isChecked()) {
            Toast.makeText(Denglu.this, "记住密码已选中", Toast.LENGTH_SHORT).show();
            sp1.edit().putBoolean("IsCheck", true).commit();
        } else {
            Toast.makeText(Denglu.this, "记住密码没有被选中", Toast.LENGTH_SHORT).show();
            sp1.edit().putBoolean("IsCheck", false).commit();
        }
    }




    public void bt_zhuce (View v)
    {
        Intent intent=new Intent(this,ZhuceActivity.class);
        startActivity(intent);
    }



}
原文地址:https://www.cnblogs.com/jiang2538406936/p/5678425.html