gpt4 book ai didi

android - 如何验证用户

转载 作者:行者123 更新时间:2023-11-29 21:10:43 26 4
gpt4 key购买 nike

我正在创建一个日志应用程序,用户可以在其中输入用户名和密码。如果用户名和密码存在于数据库中,则通过 intent 将用户带到新页面,否则将引发错误。

主页

package com.loginappdemo;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainPage extends Activity {
EditText un, pw;
Button login, register;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final DataBaseHandler db = new DataBaseHandler(this);
un = (EditText) findViewById(R.id.username);
pw = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.login);
login.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String a=un.getText().toString();
String b=pw.getText().toString();
String data=db.selectData(a, b);


}
});
register = (Button) findViewById(R.id.register);
register.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainPage.this, NewLogin.class);
startActivity(i);

}
});

}
}

数据库处理类

package com.loginappdemo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHandler extends SQLiteOpenHelper {
private static final String username = "username";
private static final String password = "password";
private static final String id = "id";
private static final String db_name = "LOGINDEMO";
private static final String table_name = "LOGINDETAILS";
private static final int db_version = 1;
private static final String create_table = " CREATE TABLE " + table_name
+ "(" + id + " INTEGER PRIMARY KEY AUTOINCREMENT, " + username
+ " VARCHAR(200)," + password + " VARCHAR(200) " + " ) ";

public DataBaseHandler(Context context) {
super(context, db_name, null, db_version);
// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {

// TODO Auto-generated method stub
db.execSQL(create_table);
}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub

}
public long insertData(String name,String password1){
SQLiteDatabase db=getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(username, name);
cv.put(password, password1);
long id=db.insert(table_name, null, cv);
return id;

}
public String selectData(String name,String password1){
SQLiteDatabase db=getWritableDatabase();
String[] col={ username, password};
String[] args={ name, password1};
StringBuffer b=new StringBuffer();
Cursor cursor=db.query(table_name, col, username + " =? AND " +password+ " =? ", args, null, null, null);
String s1= cursor.getString(1);
String s2=cursor.getString(2);
b.append(s1 + " " + s2 + "\n" );
return b.toString();

}
}

最佳答案

试试这个方法,

boolean res = dbh.GetData(s1, s2);
if(res == true) {
Intent in=new Intent(LoginActivity.this,nextActivity.class);
startActivity(in);
}
else {
Toast.makeText(MainActivity.this, "Login failed", Toast.LENGTH_SHORT).show();
}

dbh 是您的 DataBaseHandler 类对象然后

public boolean GetData(String uname, String pass)
{
SQLiteDatabase dbc = this.getReadableDatabase();
Cursor c1 = dbc.query(
TABLE_MIS2,
null,
DatabaseHandler.KEY_USERID + "=? AND " + DatabaseHandler.KEY_PWD + "=?",
new String[] { uname, pass },
null,
null,
null);
return (c1.getCount() >= 1);
}

将变量名替换为您的变量并尝试。

关于android - 如何验证用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22959464/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com