1. 程式人生 > >Android 登入註冊 資料庫SQLite 驗證

Android 登入註冊 資料庫SQLite 驗證

展示效果

這裡寫圖片描述

程式碼區

MainActivity(登入方法)

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.editText)
    EditText editText;
    @BindView(R.id.editText2)
    EditText editText2;
    @BindView(R.id.button)
    Button button;
    @BindView(R.id.button2)
    Button button2;
    @BindView
(R.id.activity_main) RelativeLayout activityMain; public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,16}$";//驗證密碼是否有特殊符號或長度不滿6位 private SQLiteDatabase w; private SQLiteDatabase r; private Mysqlist mys; private List<St> mdata; private String name; private String pass; @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); mys = new Mysqlist(this, "zhu_c", null, 1);//使用halper建立資料庫 r=mys.getReadableDatabase(); w=mys.getWritableDatabase(); mdata=new
ArrayList<St>(); Cursor query = r.rawQuery("select * from user_mo", null); while(query.moveToNext()){ int index1 = query.getColumnIndex("name"); int index2 = query.getColumnIndex("pass"); name = query.getString(index1); pass = query.getString(index2); mdata.add(new St(0, name, pass)); } } @OnClick({R.id.button, R.id.button2}) public void onViewClicked(View view) { switch (view.getId()) { case R.id.button: String name1 = editText.getText().toString().trim(); String pass1 = editText2.getText().toString().trim(); if (name1.equals(name)&&pass1.equals(pass)){ Toast.makeText(this,"登入成功",Toast.LENGTH_SHORT).show(); Intent intent = new Intent(this, UserActivity.class); startActivity(intent); }else{ Toast.makeText(this,"賬號與密碼輸入不正確",Toast.LENGTH_SHORT).show(); } break; case R.id.button2: Intent intent1 = new Intent(this, ZhuActivity.class); startActivity(intent1); break; } } }

Mysqlist(建立資料庫)

public class Mysqlist extends SQLiteOpenHelper {


    private String sql="create table if not exists user_mo(_id Integer primary key autoincrement,name varchar(30) not null,pass varchar(50) not null)";

    public Mysqlist(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(sql);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }
}

ZhuActivity(註冊方法)

public class ZhuActivity extends AppCompatActivity {

    @BindView(R.id.editText3)
    EditText editText3;
    @BindView(R.id.editText4)
    EditText editText4;
    @BindView(R.id.button3)
    Button button3;
    @BindView(R.id.activity_zhu)
    RelativeLayout activityZhu;
    public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,16}$";//驗證密碼是否有特殊符號或長度不滿6位
    private SQLiteDatabase sdb;
    private Mysqlist mys;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhu);
        ButterKnife.bind(this);
        mys = new Mysqlist(this, "zhu_c", null, 1);//使用halper建立資料庫
        sdb=mys.getWritableDatabase();
    }

    @OnClick(R.id.button3)
    public void onViewClicked() {
        //獲得賬號密碼
        String name = editText3.getText().toString().trim();
        String pass = editText4.getText().toString().trim();
        if (name == null || "".equals(name) || pass == null || "".equals(pass)) {
            Toast.makeText(this, "賬號與密碼不能為空", Toast.LENGTH_SHORT).show();
        } else {
            String number = editText3.getText().toString();
            boolean judge = isMobile(number);
            String pa = editText4.getText().toString();
            boolean judge1 = isPassword(pa);
            if (judge == true && judge1 == true) {
                Toast.makeText(this, "註冊成功", Toast.LENGTH_SHORT).show();
                sdb.execSQL("insert into user_mo(name,pass)values('"+name+"','"+pass+"')");
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);//啟動跳轉
            } else {
                Toast.makeText(this, "手機號碼不法與密碼不能有特殊符號", Toast.LENGTH_SHORT).show();
            }
        }
    }
    /**
     * 校驗密碼
     * @param password
     * @return 校驗通過返回true,否則返回false
     */
    public static boolean isPassword(String password) {
        return Pattern.matches(REGEX_PASSWORD, password);
    }
    /**
     * 驗證手機格式
     */

    public static boolean isMobile(String number) {
    /*
    移動:134、135、136、137、138、139、150、151、157(TD)、158、159、187、188
    聯通:130、131、132、152、155、156、185、186
    電信:133、153、180、189、(1349衛通)
    總結起來就是第一位必定為1,第二位必定為3或5或8,其他位置的可以為0-9
    */
        String num = "[1][358]\\d{9}";//"[1]"代表第1位為數字1,"[358]"代表第二位可以為3、5、8中的一個,"\\d{9}"代表後面是可以是0~9的數字,有9位。
        if (TextUtils.isEmpty(number)) {
            return false;
        } else {
            //matches():字串是否在給定的正則表示式匹配
            return number.matches(num);
        }
    }
}

St(工具類)

public class St {
    private int id;
    private String name;
    private String pass;

    public St(int id, String name, String pass) {
        this.id = id;
        this.name = name;
        this.pass = pass;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    @Override
    public String toString() {
        return "St{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", pass='" + pass + '\'' +
                '}';
    }
}

activity_zhu(註冊佈局)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_zhu"
    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="sizu.wxrkaoqianmomi.ZhuActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:hint="輸入手機號"
        android:gravity="center"
        android:ems="10"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="57dp"
        android:id="@+id/editText3" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:hint="輸入密碼"
        android:gravity="center"
        android:ems="10"
        android:layout_below="@+id/editText3"
        android:layout_alignLeft="@+id/editText3"
        android:layout_alignStart="@+id/editText3"
        android:layout_marginTop="44dp"
        android:id="@+id/editText4" />

    <Button
        android:text="註冊"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText4"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="88dp"
        android:id="@+id/button3" />
</RelativeLayout>

activity_main(登入佈局)

<?xml version="1.0" encoding="utf-8"?>
<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: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="sizu.wxrkaoqianmomi.MainActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:hint="輸入手機號"
        android:gravity="center"
        android:ems="10"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="33dp"
        android:id="@+id/editText" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:hint="輸入密碼"
        android:gravity="center"
        android:layout_marginTop="24dp"
        android:id="@+id/editText2"
        android:layout_below="@+id/editText"
        android:layout_alignLeft="@+id/editText"
        android:layout_alignStart="@+id/editText" />

    <Button
        android:text="登入"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText2"
        android:layout_alignLeft="@+id/editText2"
        android:layout_alignStart="@+id/editText2"
        android:layout_marginTop="85dp"
        android:id="@+id/button" />

    <Button
        android:text="註冊"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/button"
        android:layout_alignRight="@+id/editText2"
        android:layout_alignEnd="@+id/editText2"
        android:id="@+id/button2" />
</RelativeLayout>

謝謝觀看,小編祝大家生活愉快!(多多關注小編,會有非常之多精彩分享哦!)