YXTS122 2017-06-04 14:54 采纳率: 100%
浏览 1369
已采纳

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

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

 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条回答 默认 最新

  • legendCoder 2017-06-04 15:53
    关注

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

    改为:

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试