注册完之后,老是登录不上去

图片说明
图片说明
图片说明

 package com.example.sharedpreferences;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
    EditText numbuter,password;
    CheckBox checkBox;
    SharePreferences a;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        numbuter=(EditText)findViewById(R.id.numbuter);
        password=(EditText)findViewById(R.id.password);
        checkBox=(CheckBox)findViewById(R.id.checkbox);
        a=new SharePreferences();
        numbuter.addTextChangedListener(new TextWatcher(){
            @Override
            public void onTextChanged(CharSequence s,int start,int before,int count){
                String number=numbuter.getText().toString();
                String pwd=a.duqu(MainActivity.this,number);
                boolean b=a.duqucheck(MainActivity.this,number);
                if(pwd!=null&&b!=false){
                    password.setText(pwd);
                }
                else
                    password.setText("");
            }
            @Override
            public void afterTextChanged(Editable s)
            {
            }
            @Override
            public void beforeTextChanged(CharSequence s,int start,int count,int after)
            {

            }
        }
        );
    }
    public void button(View view)
    {
        String numb=numbuter.getText().toString();
        String pass=password.getText().toString();
        String pwd=a.duqu(MainActivity.this,numb);
        if(numb.equals("")||pass.equals("")){
            Toast.makeText(MainActivity.this, "密码或账号不能为空!", Toast.LENGTH_SHORT).show();
        }
        else if(!pwd.equals("pass"))
        {
            Toast.makeText(MainActivity.this,"账号或密码错误!",Toast.LENGTH_LONG).show();
            Intent intent=new Intent(this,MainActivity.class);
            startActivity(intent);
        }
        else if(pwd.equals("pass"))
        {
            Toast.makeText(MainActivity.this,"登录成功!",Toast.LENGTH_LONG).show();
        }
    }
    public void button2(View view)
    {
        String numb=numbuter.getText().toString();
        String pass=password.getText().toString();
        if(pass.equals("")||numb.equals("")){
            Toast.makeText(MainActivity.this,"账号或密码不能为空",Toast.LENGTH_LONG).show();
        }
        else if(!pass.equals("")&&!numb.equals("")&&checkBox.isChecked())
        {
            a.baocun(MainActivity.this,numb,pass);
            Toast.makeText(MainActivity.this,"注册成功",Toast.LENGTH_LONG).show();
            numbuter.setText("");
            password.setText("");
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
 package com.example.sharedpreferences;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SharePreferences {
    public boolean baocun(Context context,String numbuter,String password)
    {
        SharedPreferences sp=context.getSharedPreferences("date",Context.MODE_PRIVATE);
        Editor editor=sp.edit();
        editor.putString("username",numbuter);
        editor.putString("pwd",password);
        editor.commit();
        return true;
    }
    public String duqu(Context context,String numbuter)
    {
        SharedPreferences sp=context.getSharedPreferences("date",Context.MODE_PRIVATE);
        String password=sp.getString("pwd","");
        if(password!=null)
        {
            return password;
        }
        else
        {
            return null;
        }
    }
    public void baocuncheck(Context context,String number,boolean bool)
    {
        SharedPreferences sp=context.getSharedPreferences("date",Context.MODE_APPEND);
        SharedPreferences.Editor editor=sp.edit();
        editor.putBoolean(number,bool);
        editor.commit();
    }
    public boolean duqucheck(Context context,String number)
    {
        SharedPreferences sp=context.getSharedPreferences("date",Context.MODE_APPEND);
        boolean b=sp.getBoolean(number,false);
        return b;
    }

}

4个回答

把你登陆方法中的这段代码:
} else if (!pwd.equals("pass")) {

改为:

} else if (!pwd.equals(pass)) {

你需要的是password文本框中输入的字符串,而不是“pass”这个字符串。

图片说明

同理,最后一个else if中也这样修改

你注册的时候,手机绑定没有?

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐