Android 实现登录注册功能
来源:互联网 发布:ubuntu环境变量bashrc 编辑:程序博客网 时间:2024/06/04 17:47
初次使用SQliteOpenHelper,SQliteDatabase实现登录注册功能。写下这篇博客谨记。先上图片看看效果。
首先创建布局文件activity_main.xml文件。布局很简单只有一个LinearLayout,一个TableLayout,两个TextView,两个EditText,两个Button。之所以在TableLayout之前嵌套一个LinearLayout,是希望把背景图定死。不然输入框获取焦点,软键盘弹出时背景图会被拉伸。关于软键盘弹出的遮挡组件影响美观的事情我还上网搜了有关资料。
Android彻底解决软键盘弹出遮挡组件
但是,我并没有按照这篇博文的方式重写LinearLayout。我的方法是直接设置marginTop=200dp。
以下是activity_main.xml源码:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/background" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" ><TableLayout android:layout_width="match_parent" android:layout_marginTop="200dp" android:layout_height="match_parent"> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/username" android:textSize="20dp" android:layout_marginLeft="30dp" android:textColor="#F5F5F5" android:id="@+id/textView" /> <EditText android:layout_width="200dp" android:layout_height="wrap_content" android:background="@drawable/edit_selector" android:cursorVisible="false" android:singleLine="true" android:gravity="center" android:layout_marginRight="30dp" android:id="@+id/editText" /> </TableRow> <TableRow android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:layout_marginLeft="30dp" android:textColor="#F5F5F5" android:text="@string/password" android:id="@+id/textView2" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPassword" android:background="@drawable/edit_selector" android:ems="10" android:gravity="center" android:singleLine="true" android:cursorVisible="false" android:layout_marginRight="30dp" android:id="@+id/editText3" /> </TableRow> <TableRow android:layout_marginTop="20dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" android:textSize="20dp" android:background="@drawable/button_selector" android:layout_marginLeft="30dp" android:id="@+id/button" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注册" android:textSize="20dp" android:layout_marginLeft="10dp" android:background="@drawable/button_selector" android:layout_marginRight="30dp" android:id="@+id/button2" /> </TableRow></TableLayout></LinearLayout>
写好了activity_main.xml之后呢,我还在drawable文件夹下定义了一个edit_selector和一个button_selector.xml。是为了设置输入框获取焦点是的状态改变和button点击前后的状态改变。特别提醒android的空格是#&160; 不是 。
XML文件定义好之后,我们就获取组件并为其设置监听器。
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); userName=(EditText)findViewById(R.id.editText); password=(EditText)findViewById(R.id.editText3); loadButton = (Button) findViewById(R.id.button); registerButton = (Button) findViewById(R.id.button2); loadButton.setTag(1); loadButton.setOnClickListener(this); registerButton.setTag(2); registerButton.setOnClickListener(this); } @Override public void onClick(View v) { int tag = (int) v.getTag(); switch (tag) { case 1: Intent intent1=new Intent(MainActivity.this,firstPage.class); intent1.putExtra("userName",userName.getText().toString()); intent1.putExtra("password",password.getText().toString()); if (idCorrect()){ intent1.putExtra("userName",userName.getText().toString()); intent1.putExtra("password",password.getText().toString()); startActivity(intent1); } break; case 2: Intent intent=new Intent(this,registerActivity.class); startActivity(intent); break; } }
这是登录界面的代码。当用户输入账号密码时,先进行判断该用户是否注册,即查询数据库。如果正确就跳转到另一个activity。
public boolean idCorrect(){ myOpenHelper=new MyOpenHelper(this,"mydb.db",null,1); database=myOpenHelper.getWritableDatabase(); Cursor cursor=database.rawQuery(qurySql,null); while(cursor.moveToNext()){ userName1=cursor.getString(cursor.getColumnIndex("userId")); password1=cursor.getString(cursor.getColumnIndex("password")); if (userName1.equals(userName.getText().toString())&&password1.equals(password.getText().toString())){ return true; } } Toast.makeText(this,"账号或密码错误,请重新输入",Toast.LENGTH_LONG).show(); return false; }
这是查询数据库并判断该用户是否已注册。
//打开或创建一个名为mydb.db的数据库。如果没有就创建一个myOpenHelper=new MyOpenHelper(this,"mydb.db",null,1);//在getWritableDatabase()之前数据库其实并没有实际创建。//所以真正意义上来说这句才是创建数据库。获取一个可读写的数据库database=myOpenHelper.getWritableDatabase();//查询语句Cursor cursor=database.rawQuery(qurySql,null);
MyOpenHelper是一个继承了SQLiteOpenHelper的子类。第一个参数是context,第二个参数是String DatabaseName,要打开或创建的数据库名称,第三个参数是CursorFactory factory,用来创建cursor对象,默认为null。第四个参数为int version,数据库版本。
接下来我们看下注册界面的activity。
package com.example.admin.dec1224;import android.app.Activity;import android.content.Intent;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;/** * Created by admin on 2015/12/25. */public class registerActivity extends Activity implements View.OnClickListener{ private Button register; private Button back; private String userName; private String password; private MyOpenHelper myOpenHelper; public static final String createTableSql="create table if not exists userInfo (userId varchar,password varchar)"; private int Tag=0; private SQLiteDatabase database; private EditText editUser; private EditText pwd; private EditText pwdAgain; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.register); editUser=(EditText)findViewById(R.id.reg_userText); pwd=(EditText)findViewById(R.id.reg_pwdText); pwdAgain=(EditText)findViewById(R.id.reg_pwdagText); register= (Button) findViewById(R.id.button3); register.setTag(1); register.setOnClickListener(this); back=(Button)findViewById(R.id.button4); back.setTag(2); back.setOnClickListener(this); } @Override public void onClick(View v) { int Tag=(int)v.getTag(); switch (Tag){ case 1: createDatabase(); createTable(); insertData(); Toast.makeText(registerActivity.this,"注册成功",Toast.LENGTH_LONG).show(); break; case 2: Intent intent=new Intent(this,MainActivity.class); startActivity(intent); break; default: Toast.makeText(registerActivity.this,"注册失败,请再试一次",Toast.LENGTH_LONG).show(); break; } } public void createDatabase(){ myOpenHelper=new MyOpenHelper(this,"mydb.db",null,1); database=myOpenHelper.getWritableDatabase(); } public void createTable(){ createDatabase(); database.execSQL(createTableSql); } public void insertData(){ pwdCorrect(); createDatabase(); String insertSql="insert into userInfo (userId,password) values ('"+editUser.getText().toString()+"','"+pwd.getText().toString()+"');"; database.execSQL(insertSql);// ContentValues cv=new ContentValues();// cv.put(userName,editUser.getText().toString());// cv.put(password,pwd.getText().toString());// database.insert("userInfo", null, cv); } public boolean pwdCorrect(){ if(pwd.getText().toString().equals(pwdAgain.getText().toString())){ return true; } Toast.makeText(registerActivity.this,"两次密码输入不一致,请重新输入。",Toast.LENGTH_LONG).show(); return false; }}
即插入数据。
- Android 实现登录注册功能
- Android 实现登录注册功能
- Android PHP JSON 登录注册功能实现
- Android 登录注册功能
- ios 实现注册 登录功能
- php实现登录注册功能
- 表单实现登录注册功能
- MVP实现登录注册功能
- JSP 实现登录注册功能
- 实现登录注册验证功能
- 开发实训10---Android---注册登录功能实现1
- android studio使用Bmob来实现登录注册的功能
- Android登录注册功能实现:Json+Servlet+sqlserver
- android studio使用Bmob来实现登录注册的功能
- 【Android】 使用okhttp实现注册登录功能(与服务器端)
- android 注册、登录实现
- android 注册、登录实现
- Android登录注册功能封装
- Ant-Help
- SQL Server用户自定义函数
- iOS应用崩溃(一)——Crash文件解析
- 简单的 C++ SOCKET编程 ---基于TCP/IP协议
- Ant-Ant命令行参考
- Android 实现登录注册功能
- IOS uicontroller
- Theme Section
- C++ Socket编程步骤
- Android selector标签
- 添加yum 源
- 一种高性能网络游戏服务器架构设计
- DNS相关概念
- 使用面向对象的方式模拟TOM使用ATM进行存取款的操作