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

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,效果图所示


注:图片自己引用

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

时间: 2024-08-02 02:47:34

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

Android SQLite最简单demo实现(增删查改)

本来不太想写这篇博客的,但是看到网上的关于android数据库操作的博文都讲得很详细,对于像我这样的新手入门了解SQLite的基本操作有一定难度,所以我参考了网上的一些博客文章,并自己亲自摸索了一遍,希望写出这么一篇博文来记录SQLite的最基本操作,同时也希望能够对android的新手们有些帮助. 参考博客:http://www.20864.com/201247/274.html 这里只是一个示范性的demo,并没实现什么具体功能,只实现了对数据库的增删查改操作. 以下是实现demo的步骤:

Android SQLite 的简单实例

1.前言: 今天再一次去蹭了一下某老师的android课,这一次讲的是Android的SQLite的使用,老师当场讲解了他自己做的例子. 回来之后,我春心萌动,不得不拿着参考资料再做了一个类似的例子,其实我已经过几遍SQLite的内容了,但是认识还是不深刻.    2.SQLite继承 要想使用SQLite,就必需设计一个相应类,并且继承SQLiteOpenHelper. 基本上要操作的是onCreate函数(注意自动生成,执行语句建议还是单独写),这个函数在数据库被提及时便会执行,所以添加的内

Android笔记-4-实现登陆页面并跳转和简单的注册页面

实现登陆页面并跳转和简单的注册页面 首先我们来看看布局的xml代码 login.xml <span style="font-family:Arial;font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu

android asmack 注册 登陆 聊天 多人聊天室 文件传输

XMPP协议简介 XMPP协议(Extensible Messaging and PresenceProtocol,可扩展消息处理现场协议)是一种基于XML的协议,目的是为了解决及时通信标准而提出来的,最早是在Jabber上实现的.它继承了在XML环境中灵活的发展性.因此,基于XMPP的应用具有超强的可扩展性.并且XML很易穿过防火墙,所以用XMPP构建的应用不易受到防火墙的阻碍.利用XMPP作为通用的传输机制,不同组织内的不同应用都可以进行有效的通信. 这篇文章有基本的介绍,http://bl

简单的Android Sqlite 使用

简单的Android Sqlite 使用: package com.pdsu.db_test; import java.sql.Date; import android.app.Activity; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.widget.Toast; public class Main

【原创】android——SQLite的cmd命令的基本操作

步骤:enter为按键 1,开始—>运行—>输入cmd  (enter) 2,输入adb shell  (enter) 3, cd data/data/应用包名/databases  (enter) 4, ls (查看目录下的数据库)   (enter) 5,sqlite3  数据库名.db;   (enter) 6,SQL语句操作表,注意标点符号一定是在英文输入法下 7,示例: [原创]android--SQLite的cmd命令的基本操作,布布扣,bubuko.com

通过游戏学python 3.6 第一季 第九章 实例项目 猜数字游戏--核心代码--猜测次数--随机函数和屏蔽错误代码--优化代码及注释--简单账号密码登陆--账号的注册查询和密码的找回修改--锁定账号--锁定次数--菜单功能&#39;menufile

通过游戏学python 3.6 第一季 第九章 实例项目 猜数字游戏--核心代码--猜测次数--随机函数和屏蔽错误代码--优化代码及注释--简单账号密码登陆--账号的注册查询和密码的找回修改--锁定账号--锁定次数--菜单功能'menufile 1 #猜数字--核心代码--猜测次数--随机函数和屏蔽错误代码---优化代码及注释--简单账号密码登陆--账号的注册查询和密码的找回修改--锁定账号--锁定次数--菜单功能'menufile' 2 #!usr/bin/env python 3 #-*-c

通过游戏学python 3.6 第一季 第七章 实例项目 猜数字游戏--核心代码--猜测次数--随机函数和屏蔽错误代码--优化代码及注释--简单账号密码登陆--账号的注册查询和密码的找回修改--锁定账号

#猜数字--核心代码--猜测次数--随机函数和屏蔽错误代码---优化代码及注释--简单账号密码登陆--账号的注册查询和密码的找回修改--锁定账号 1 #猜数字--核心代码--猜测次数--随机函数和屏蔽错误代码---优化代码及注释--简单账号密码登陆--账号的注册查询和密码的找回修改--锁定账号 2 #!usr/bin/env python 3 #-*-coding:utf-8-*- 4 #QQ124111294 5 6 7 import random 8 number = random.rand

android简单登录注册服务端代码实现

长时间不写,知识都淡忘了!现在实现简单登录注册功能,供以后参考!!!! 项目下载地址: https://github.com/majunm/TestServiceDemo.git 服务器: Tomcat/7.0.40  数据库: mysql5.0 数据库创建: mysql 正确安装 配置完path 后 如下界面: mysql -u root -p    // 连接mysql数据库 create database july;//创建数据库 数据库名 july show databases; //