SQList数据库2
编写三个页面——首页,欢迎页,注册页。
1、首页:包含2个编辑框(用户名、密码)和两个按钮(登录、注册)
点击登录,通过查找数据库验证用户名和密码,成功则跳转欢迎页,失败则提示失败原因,不跳转。
点击注册,跳转注册页。
2、注册页:自行设计,要求有一些常用的注册信息。
点击注册按钮,如果数据库中没有相同的用户名,则注册成功,跳转首页,否则提示注册失败,不跳转
3、欢迎页:显示用户的所有信息。
代码:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:id="@+id/et01"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:id="@+id/et02"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="dian01"
android:text="登入" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="dian02"
android:text="注册" />
</LinearLayout>
MainActivity.java
package com.example.dome01;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText et01,et02;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et01 = (EditText) findViewById(R.id.et01);
et02 = (EditText) findViewById(R.id.et02);
}
public void dian01(View v){ //登入处理
//点击登入,通过查找数据库验证用户名和密码,成功则跳转欢迎页,失败则提示失败原因,不跳转
String s1 = et01.getText().toString();
String s2 = et02.getText().toString();
//查询数据库看看用户名是否存在
//存在,查询该用户的密码,看看两个密码是否一致
//一致,登入成功
//不一致,登入失败
//不存在,登入失败
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(getFilesDir()+"/my.db",null);
String sql = "select * from tUser where uuu='"+s1+"'";
Cursor c = db.rawQuery(sql,null);
if (c.moveToNext()) { //如果鼠标能够走到下一行,证明查到数据,说以用户名存在
String p = c.getString(1);
if (s2.equals(p)) {
Intent in = new Intent(this,WelcomeActivity.class);
startActivity(in);
db.close();
}else{
et01.setText("");
et02.setText("");
Toast.makeText(this,"登入失败,密码错误,请检查后重新登入",1).show();
db.close();
}
}else{
et01.setText("");
et02.setText("");
Toast.makeText(this,"登入失败,用户名不存在,请检查后重新登入",1).show();
db.close();
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dome01">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="Java2013"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Dome01">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="1328">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.dome01.WelcomeActivity"
android:label="@string/app_name">
</activity>
</application>
</manifest>
welcome.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="欢迎来到我的世界!!!"/>
</LinearLayout>
WelcomeActivity.java
package com.example.dome01;
import android.app.Activity;
import android.os.Bundle;
public class WelcomeActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
}
}
运行结果:
![]()
如上图,登入失败
如下图,登入成功

 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号