2012-09-20 66 views
0

我想要使用SharedPreferences保存登录凭据的代码。我能够或相信凭证正在成功保存,但在尝试通过比较编辑中的凭证和保存的凭证进行登录时。我一直收到“错误的密码”错误。不知道我在忽略什么。登录的代码如下。SharedPreferences登录错误

登录:

public class AccessApp extends Activity implements OnClickListener { 
private SharedPreferences sp; 
String user,pass; 
Button lBttn,cBttn; 
EditText uname,pword; 
Intent i; 

int flag=0; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    lBttn=(Button)findViewById(R.id.login_button); 
    cBttn=(Button)findViewById(R.id.cancel_button); 
    uname=(EditText)findViewById(R.id.username); 
    pword=(EditText)findViewById(R.id.password); 

    lBttn.setOnClickListener(this); 
    cBttn.setOnClickListener(this); 
} 
public void onClick(View arg0) { 

    sp=this.getSharedPreferences("Register", MODE_WORLD_READABLE); 
    user=sp.getString("USERNAME", ""); 
    pass=sp.getString("PASSWORD",""); 


    if(lBttn==arg0){ 
      if((uname.getText().toString().compareTo(user)==0)&& 
       (pword.getText().toString().compareTo(pass)==0)) 

      { 
      Toast.makeText(this, "You are Logged In", 20000).show(); 

       Intent intent; 
       intent=new Intent(this,details.class); 
       startActivity(intent); 
       flag=1; 
      } 

     else 
      { 
      Toast.makeText(this, "Wrong Username or Password",20000).show(); 
      flag=0; 
      }  
     } 

注册:

 public class SharedPrefLoginActivity extends Activity implements OnClickListener { 

private SharedPreferences sp; 

Intent i; 
Button regBttn,rtnBttn; 
EditText rName,rPwd; 
String user,pass,cpass,chk; 
String stat="a"; 


/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 


    super.onCreate(savedInstanceState); 
    setContentView(R.layout.register); 


    rName=(EditText)findViewById(R.id.reg_uname); 
    rPwd=(EditText)findViewById(R.id.reg_pswd); 
    regBttn=(Button)findViewById(R.id.reg_button); 
    rtnBttn=(Button)findViewById(R.id.rtn_button); 

    regBttn.setOnClickListener(this); 
    rtnBttn.setOnClickListener(this); 
    sp=this.getSharedPreferences("AccessApp", MODE_WORLD_READABLE); 
    chk=sp.getString("USERNAME", ""); 
    if(chk.length()!=0){ 
     sp=getSharedPreferences("AccessApp",MODE_WORLD_WRITEABLE); 

     i=new Intent(this,AccessApp.class); 
     startActivity(i);  
    } 

} 

public void onClick(View arg0) { 
    user=rName.getText().toString(); 
    pass=rPwd.getText().toString(); 
    if(arg0==regBttn){ 

     if((user.length()!=0)) 
     { 
      if((pass.length()!=0)) 
      { 

     sp=getSharedPreferences("AccessApp",MODE_WORLD_WRITEABLE); 
     Editor myEditor=sp.edit(); 
     myEditor.putString("USERNAME", user); 
     myEditor.putString("PASSWORD", pass); 
     myEditor.commit(); 
     Toast.makeText(this, "Registration is successfull",10000).show(); 
     i=new Intent(this,AccessApp.class); 
     startActivity(i); 
     } 
     else 
     { 
      Toast.makeText(this, "Please Enter password", 10000).show(); 
     } 
     } 
     else{ 
      Toast.makeText(this,"Please Enter Username",10000).show(); 
     } 
     } 
+2

请张贴的代码,部分地方已保存sharedPrefs – Shrikant

+0

如果你在这里放置一个断点,如果((uname.getText()。toString()。compareTo(user)== 0)&& (pword.getText()。toString()。compareTo(pass)== 0)),调试器告诉你关于user,pass和uname,pword的值的信息? – Simon

+0

我已经添加了我用来插入数据的类。 – sean

回答

0

问题导致的问题是低于

登录:

sp=this.getSharedPreferences("Register", MODE_WORLD_READABLE); /* change "Register" to "AccessApp" */ 

登记:

sp=getSharedPreferences("AccessApp",MODE_WORLD_WRITEABLE); 
1

使用==操作符你不能比较Button对象。相反,尝试使用:

if(arg0.getId()==R.id.login_button){ 
+0

该选项允许登录工作,但输入任何字符串。没有用这个选项做比较。 – sean

+0

你的意思是不做比较?我认为比较已经完成,但它不会返回您的预期。 –

+0

当我输入与在注册活动中输入相同的凭据时,我收到错误的密码消息。当我点击editText字段中没有数据的登录按钮时,我登录并进入下一个活动。我想我可能会输入一个空的字符串与我的注册活动,但不知道如何验证。 – sean