码迷,mamicode.com
首页 > 移动开发 > 详细

android简单登陆和注册功能实现+SQLite数据库学习

时间:2016-04-16 21:13:44      阅读:1252      评论:0      收藏:0      [点我收藏+]

标签:

最近初学android,做了实验室老师给的基本任务,就是简单的登陆和注册,并能通过SQLite实现登陆,SQlLite是嵌入在安卓设备中的

好了下面是主要代码:

数据库的建立:

这里我只是建立了一个用简单的存储用户名和密码的表单

MyDBHelper.java

public class MyDBHelper extends SQLiteOpenHelper {
    
    public static final String CREATE_USERDATA="create table userData(" +
            "id integer primary key autoincrement"
            +"name,"
            +"password text)";

    private Context mContext;

    public MyDBHelper(Context context, String name, SQLiteDatabase.CursorFactory cursorFactory,int version){
        super(context,name,cursorFactory,version);
        mContext=context;
    }

    public  void onCreate(SQLiteDatabase db){
        db.execSQL(CREATE_USERDATA);
    }

    public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
        //onCreate(db);
    }

}

注册:

Logon.java

public class Main2Activity extends AppCompatActivity {

    private MyDBHelper dbHelper;

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

        dbHelper = new MyDBHelper(this,"UserStore.db",null,1);
    }


    public void logon(View view){
        EditText editText3=(EditText)findViewById(R.id.editText3);
        EditText editText4=(EditText)findViewById(R.id.editText4);
        String name =editText3.getText().toString();
        String password=editText4.getText().toString();
        if (register(name,password)){
            Toast.makeText(this,"插入数据表成功",Toast.LENGTH_SHORT).show();
        }

    }

    public boolean register(String username,String password){
        SQLiteDatabase db= dbHelper.getWritableDatabase();

        //下面注释掉的是其他数据库插入的方法
        /*String sql = "insert into userData(name,password) value(?,?)";
        Object obj[]={username,password};
        db.execSQL(sql,obj);*/
        /*ContentValues values=new ContentValues();
        values.put("name",username);
        values.put("password",password);
        db.insert("userData",null,values);*/

       
        //这是数据库插入的简便语法
        db.execSQL("insert into userData (name,password) values (?,?)");
        return true;
    }
}

登陆:

Login.java

public class MainActivity extends AppCompatActivity {
    private MyDBHelper dbHelper;
    private EditText username;
    private EditText userpassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        
        dbHelper = new MyDBHelper(this,"UserStore.db",null,1);
    }
    
     //点击注册按钮进入注册页面
    public void logonClicked(View view){
        Intent intent = new Intent(MainActivity.this,Main2Activity.class);
        startActivity(intent);
    }


    //点击登录按钮
    public void loginClicked(View view) {
        username=(EditText)findViewById(R.id.editText2);
        userpassword=(EditText)findViewById(R.id.editText);
        String userName=username.getText().toString();
        String passWord=userpassword.getText().toString();
        if (login(userName,passWord)) {
            Toast.makeText(MainActivity.this, "登陆成功(ZY,111)", Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(MainActivity.this, "登陆失败", Toast.LENGTH_SHORT).show();
        }
    }

    //验证登录
    public boolean login(String username,String password) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String sql = "select * from userData where name=? and password=?";
        Cursor cursor = db.rawQuery(sql, new String[] {username, password});
        if (cursor.moveToFirst()) {
            cursor.close();
            return true;
        }
        return false;
    }

好了,就酱。

android简单登陆和注册功能实现+SQLite数据库学习

标签:

原文地址:http://www.cnblogs.com/harrymusic/p/5399069.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!