**寄存器表将任何String视为有效**
登录类
有一个寄存器表,并且有字段FirstName,LastName和Password我检查edittext值到寄存器表....但它对任何字符串都返回true .....
package com.example.app;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
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 login extends Activity{
EditText e1,e2;
Button b1;
DBHandler handler=new DBHandler(this);
//SQLiteDatabase db=handler.getWritableDatabase();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
e1=(EditText)findViewById(R.id.e_l_fnm);
e2=(EditText)findViewById(R.id.e_l_lnm);
b1=(Button)findViewById(R.id.b_login);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String fname=e1.getText().toString();
String pwd=e2.getText().toString();
if(fname.equals("") || pwd.equals(""))
{
Toast.makeText(getApplicationContext(), "fields are required", Toast.LENGTH_SHORT).show();
}
else
{
handler.login(fname, pwd);
Toast.makeText(getApplicationContext(), "login successfull", Toast.LENGTH_SHORT).show();
// Intent i=new Intent(login.this,home.class);
// startActivity(i);
}
}
});
}
}
登录方法(DBHandler类)
public boolean login(String fname,String pwd)
{
SQLiteDatabase db=this.getWritableDatabase();
Cursor c=db.rawQuery("SELECT * FROM " + TABLE_REGISTER + " WHERE FirstName=? AND Password=?", new String[]{fname,pwd});
if(c!=null)
{
if(c.getCount()<0)
{
return false;
}
}
return true;
}
答案 0 :(得分:2)
如果条件为handler.login
,则使用if-else
验证true
方法调用结果,然后启动下一步,否则显示“无效登录”的警告消息:
if(handler.login(fname, pwd)){
// use login successful
}else{
// login failed
}