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

【原创】android——SQLite实现简单的注册登陆(已经美化)

时间:2014-08-08 23:58:56      阅读:6839      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   color   java   os   io   

1,Main_activity的xmL配置

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:background="@drawable/b3"
 6     android:orientation="vertical"
 7     tools:context="${relativePackage}.${activityClass}" >
 8 
 9     <TextView
10         android:layout_width="match_parent"
11         android:layout_height="15dp"
12         android:background="#56a692"
13         android:gravity="bottom"
14         android:text="@string/main_name1"
15         android:textColor="#fff"
16         android:textSize="12sp" />
17 
18     <TextView
19         android:layout_width="match_parent"
20         android:layout_height="80dp"
21         android:gravity="center"
22         android:text="@string/main_name1"
23         android:textSize="20sp"
24         android:textStyle="italic" />
25 
26     <TextView
27         android:layout_width="match_parent"
28         android:layout_height="38dp"
29         android:background="#CD661D" />
30 
31     <TextView
32         android:layout_width="match_parent"
33         android:layout_height="30dp"
34         android:text="@string/main_name6"
35         android:textSize="15sp"
36         android:textStyle="italic" />
37 
38     <EditText
39         android:id="@+id/editText1"
40         android:layout_width="match_parent"
41         android:layout_height="wrap_content"
42         android:layout_marginTop="15dp"
43         android:ems="10"
44         android:hint="@string/main_name2" >
45 
46         <requestFocus />
47     </EditText>
48 
49     <EditText
50         android:id="@+id/editText2"
51         android:layout_width="match_parent"
52         android:layout_height="wrap_content"
53         android:layout_marginTop="34dp"
54         android:ems="10"
55         android:hint="@string/main_name3" />
56 
57     <TextView
58         android:layout_width="match_parent"
59         android:layout_height="60dp"
60         android:layout_weight="0.00" />
61 
62     <TextView
63         android:layout_width="match_parent"
64         android:layout_height="38dp"
65         android:background="#CD661D" />
66 
67     <LinearLayout
68         android:layout_width="wrap_content"
69         android:layout_height="wrap_content"
70         android:layout_weight="0.31"
71         android:orientation="horizontal" >
72 
73         <Button
74             android:id="@+id/button1"
75             android:layout_width="155dp"
76             android:layout_height="wrap_content"
77             android:text="@string/main_name4" />
78 
79         <Button
80             android:id="@+id/button2"
81             android:layout_width="164dp"
82             android:layout_height="wrap_content"
83             android:text="@string/main_name5" />
84     </LinearLayout>
85 
86     <TextView
87         android:layout_width="match_parent"
88         android:layout_height="50dp"
89         android:background="#EECFA1" />
90 
91 </LinearLayout>

2,Register_activity 的XML配置

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:background="@drawable/b1"
 6     android:orientation="vertical"
 7     tools:context="${relativePackage}.${activityClass}" >
 8 
 9     <TextView
10         android:layout_width="match_parent"
11         android:layout_height="15dp"
12         android:background="#56a692"
13         android:gravity="bottom"
14         android:text="@string/register_name"
15         android:textColor="#fff"
16         android:textSize="12sp" />
17 
18     <TextView
19         android:layout_width="match_parent"
20         android:layout_height="wrap_content"
21         android:gravity="center"
22         android:text="@string/register_name6"
23         android:textColor="#B22222"
24         android:textSize="20sp"
25         android:textStyle="italic" />
26 
27     <TextView
28         android:layout_width="wrap_content"
29         android:layout_height="100dp"
30         android:gravity="bottom" />
31 
32     <TextView
33         android:layout_width="match_parent"
34         android:layout_height="wrap_content"
35         android:background="#B2DFEE"
36         android:gravity="bottom"
37         android:text="@string/register_name7"
38         android:textStyle="italic" />
39 
40     <EditText
41         android:id="@+id/etid"
42         android:layout_width="match_parent"
43         android:layout_height="wrap_content"
44         android:ems="10"
45         android:hint="@string/register_name2" >
46 
47         <requestFocus />
48     </EditText>
49 
50     <TextView
51         android:layout_width="match_parent"
52         android:layout_height="wrap_content"
53         android:gravity="center"
54         android:textColor="#B22222"
55         android:textSize="20sp"
56         android:textStyle="italic" />
57 
58     <EditText
59         android:id="@+id/etname"
60         android:layout_width="match_parent"
61         android:layout_height="wrap_content"
62         android:ems="10"
63         android:hint="@string/register_name3" />
64 
65     <TextView
66         android:layout_width="match_parent"
67         android:layout_height="wrap_content"
68         android:background="#B2DFEE"
69         android:gravity="center"
70         android:textColor="#B22222"
71         android:textSize="20sp"
72         android:textStyle="italic" />
73 
74     <LinearLayout
75         android:layout_width="wrap_content"
76         android:layout_height="0dp"
77         android:layout_weight="0.84"
78         android:orientation="horizontal" >
79 
80         <Button
81             android:id="@+id/btn_qu"
82             android:layout_width="155dp"
83             android:layout_height="wrap_content"
84             android:text="@string/register_name4" />
85 
86         <Button
87             android:id="@+id/btn_sure"
88             android:layout_width="163dp"
89             android:layout_height="wrap_content"
90             android:text="@string/register_name5" />
91     </LinearLayout>
92 
93     <TextView
94         android:layout_width="match_parent"
95         android:layout_height="50dp"
96         android:background="#EECFA1" />
97 
98 </LinearLayout>

 

3,SQLite的配置创建数据库和表

例如:新建类sqliteopenhelper

package com.example.dbtest;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
//新建类Sqliteopenhelper 继承于SQLiteOpenHelper
public class Sqliteopenhelper extends SQLiteOpenHelper {

                   //数据库名:
    private static final String DBNAME="test.db";
                 //表名
    private static final String TABLENAME="student";
    private static final String GOODSNAME="goods";
                 //版本号:具体我也不知道是什么,照着写就行了
    private static final int TESTVERSION=1;
                
    public Sqliteopenhelper(Context context) {
        super(context, DBNAME, null, TESTVERSION);
        // TODO Auto-generated constructor stub
    }

                  //初始化,创建表
    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
   String sql1="create table"+" "+TABLENAME+"(id varchar,name varchar)";
String sql2="create table"+" "+GOODSNAME+"(LF varchar,name varchar,miaoshu varchar)";
          db.execSQL(sql1);
          db.execSQL(sql2);
          
    }
                 //失败后删除,重新创建
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
           if(newVersion>oldVersion)
           {
               String sql1="drop table if exists"+TABLENAME;
               String sql2="drop table if exists"+GOODSNAME;
               db.execSQL(sql1);
               db.execSQL(sql2);
               this.onCreate(db);
           }
    }

}

4,Main_activity.java 调用并且实现验证数据库内信息

  1 package com.example.dbtest;
  2 
  3 import android.app.Activity;
  4 import android.app.AlertDialog;
  5 import android.app.AlertDialog.Builder;
  6 import android.content.DialogInterface;
  7 import android.content.Intent;
  8 import android.database.Cursor;
  9 import android.database.sqlite.SQLiteDatabase;
 10 import android.database.sqlite.SQLiteException;
 11 import android.database.sqlite.SQLiteOpenHelper;
 12 import android.os.Bundle;
 13 import android.view.View;
 14 import android.view.View.OnClickListener;
 15 import android.widget.Button;
 16 import android.widget.EditText;
 17 import android.widget.Toast;
 18 
 19 public class MainActivity extends Activity {
 20 
 21     private EditText et_id, et_name;
 22     private Button btn_test, btn_local;
 23                   //1,SQLite的声明
 24     SQLiteOpenHelper helper;
 25     private String _id;
 26     private String _name;
 27 
 28     @Override
 29     protected void onCreate(Bundle savedInstanceState) {
 30         super.onCreate(savedInstanceState);
 31         setContentView(R.layout.activity_main);
 32         // 2,数据库的创建,及调用
 33         helper = new Sqliteopenhelper(this);
 34         helper.getWritableDatabase();
 35 
 36         et_id = (EditText) findViewById(R.id.editText1);
 37         et_name = (EditText) findViewById(R.id.editText2);
 38         btn_test = (Button) findViewById(R.id.button1);
 39         btn_local = (Button) findViewById(R.id.button2);
 40 
 41         btn_test.setOnClickListener(new testListener());
 42         btn_local.setOnClickListener(new localListener());
 43 
 44     }
 45 
 46     class testListener implements OnClickListener {
 47 
 48         @Override
 49         public void onClick(View v) {
 50             // TODO Auto-generated method stub
 51     Intent intent = new Intent(MainActivity.this, Register.class);
 52             startActivity(intent);
 53         }
 54 
 55     }
 56          //登陆按钮
 57 
 58     class localListener implements OnClickListener {
 59 
 60         @Override
 61         public void onClick(View v) {
 62             // TODO Auto-generated method stub
 63             _id = et_id.getText().toString();
 64             _name = et_name.getText().toString();
 65             if (_name.equals("") || _id.equals("")) {         Toast.makeText(getApplicationContext(), "请输入账号密     码!",Toast.LENGTH_SHORT).show();
 66             } else {
 67                 sureuser(_id, _name);
 68             }
 69         }
 70 
 71     }
 72 
 73     private void sureuser(String userid, String username) {
 74                  //3,数据库的操作,查询
 75     SQLiteDatabase sdb = helper.getReadableDatabase();
 76         try {
 77     String sql = "select * from student where id=? and name=?";
 78             // 实现遍历id和name
 79     Cursor cursor = sdb.rawQuery(sql, new String[] { _id, _name });
 80             if (cursor.getCount() > 0) {
 81     Intent intent = new Intent(MainActivity.this, User.class);
 82         Bundle bundle = new Bundle();
 83             bundle.putString("name", _name);
 84                 intent.putExtras(bundle);
 85                 startActivity(intent);
 86             } else {
 87         Toast.makeText(getApplicationContext(), "登录失败",
 88                         Toast.LENGTH_SHORT).show();
 89             }
 90             cursor.close();
 91             sdb.close();
 92         } catch (SQLiteException e) {
 93     oast.makeText(getApplicationContext(), "亲,请注册!",
 94                     Toast.LENGTH_SHORT).show();
 95         }
 96     }
 97 
 98 
 99         
100     
101 }


5,Register_activity.java

package com.example.dbtest;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Register extends Activity {

    private EditText etid,etname;
    private Button btn_qu,btn_sure;
    
    SQLiteOpenHelper helper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
         helper=new Sqliteopenhelper(this);
         helper.getWritableDatabase();
         etid=(EditText)findViewById(R.id.etid);
         etname=(EditText)findViewById(R.id.etname);
         btn_qu=(Button)findViewById(R.id.btn_qu);
         btn_sure=(Button)findViewById(R.id.btn_sure);
         btn_sure.setOnClickListener(new sureListener());
         btn_qu.setOnClickListener(new quListener());
         
    }
    
    class sureListener implements OnClickListener{

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            try{
        SQLiteDatabase sdb=helper.getWritableDatabase();
            ContentValues values=new ContentValues();
            values.put("id",etid.getText().toString());
            values.put("name",etname.getText().toString());
            sdb.insert("student",null, values);
Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_SHORT).show();
            Intent intent=new Intent(Register.this,User.class);
            Bundle bundle=new Bundle();
            bundle.putString("name",etname.getText().toString());
            intent.putExtras(bundle);
            startActivity(intent);;
            }
            catch(SQLiteException e)
            {Toast.makeText(getApplicationContext(), "注册失败", Toast.LENGTH_SHORT).show();}
        }
    }
    
    class quListener implements OnClickListener{

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
    Intent intent=new Intent(Register.this,MainActivity.class);
            startActivity(intent);
        }
        
    }
    
}

6,效果图所示

bubuko.com,布布扣bubuko.com,布布扣
注:图片自己引用

 

【原创】android——SQLite实现简单的注册登陆(已经美化),布布扣,bubuko.com

【原创】android——SQLite实现简单的注册登陆(已经美化)

标签:android   style   blog   http   color   java   os   io   

原文地址:http://www.cnblogs.com/LABELNET/p/3900300.html

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