关于sharedPreference实现注册登录的问题

移动开发 码拜 7年前 (2017-05-04) 1484次浏览
先说问题,注册完登录,提示先注册。小白表示尽力了,特来讨教老人。先上两张问题图。
关于sharedPreference实现注册登录的问题关于sharedPreference实现注册登录的问题
注册页面代码

package com.wifijiaju;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Register extends Login{
EditText ruserET; 
EditText rpasswordET;
EditText upasswordET;
EditText cpasswordET;
Button registerBT;
Button changeBT;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_register);
		ruserET = (EditText) findViewById(R.id.ruserET);
		rpasswordET = (EditText) findViewById(R.id.rpasswordET);
		upasswordET = (EditText) findViewById(R.id.upasswordET);
		cpasswordET = (EditText) findViewById(R.id.cpasswordET);
		registerBT = (Button) findViewById(R.id.registerBT);
		changeBT = (Button) findViewById(R.id.changeBT);
		userrootStr = new String();

		if(userrootStr.equals(""))
		{registerBT.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				String user = ruserET.getText().toString();
				String password = rpasswordET.getText().toString();
				if(!user.equals("")&& !password.equals(""))
				{
					editor = pref.edit();
					String userroot = null;
					editor.putString(userroot, user);
					String passwordroot = null;
					editor.putString(passwordroot, password);
					editor.apply();
					Toast.makeText(Register.this, "注册成功!", Toast.LENGTH_SHORT).show();
				}

			}
		});
		}
		else
		{
			registerBT.setEnabled(false);
			changeBT.setOnClickListener(new View.OnClickListener() {

				public void onClick(View v) {
				String oldpassword = upasswordET.getText().toString();
				String newpassword = cpasswordET.getText().toString();
				if(oldpassword.equals(passwordrootStr)&&!newpassword.equals(""))
				{
					editor = pref.edit();
					editor.putString("passwordroot", newpassword);
					editor.apply();
					Toast.makeText(Register.this, "修改成功!", Toast.LENGTH_SHORT).show();
				}
				else
				{
					Toast.makeText(Register.this, "修改失败,请确认能否正确输入新旧密码!", Toast.LENGTH_SHORT).show();
				}

				}
			});
		}
		ExitApplication.getInstance().addActivity(this);
	}

	protected void dialog() { 
        AlertDialog.Builder builder = new Builder(Register.this); 
        builder.setMessage("亲,您是想?"); 
        builder.setTitle("提示"); 
        builder.setPositiveButton("直接退出", 
         new android.content.DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
          dialog.dismiss(); 
          /**
           * 完全退出应用程序
           */
          ExitApplication.getInstance().exit();
          } 
         }); 
        builder.setNegativeButton("返回上一层", 
         new android.content.DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
          dialog.dismiss(); 
          Register.this.finish(); 
          } 
         }); 
        builder.create().show(); 
       } 
       @Override
       public boolean onKeyDown(int keyCode, KeyEvent event) { 
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { 
         dialog(); 
         return false; 
        } 
        return false; 
       }
}

登录页面代码

package com.wifijiaju;
import android.app.Activity; 
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.view.KeyEvent;
import android.view.View; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.EditText; 
import android.widget.TextView;
import android.widget.Toast; 
  
public class Login extends Activity { 
  
 EditText userET; 
 EditText passwordET; 
 Button logBT; 
 TextView rfTX;
 CheckBox rememberPass; 
 SharedPreferences pref;
 SharedPreferences.Editor editor;
 String userrootStr;
 String passwordrootStr;
 String userStr; 
 String passwordStr; 
 
 
 @Override
protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_login);
	editor = getSharedPreferences("data",MODE_PRIVATE).edit();
	editor.putString("userroot", "");
	editor.putString("passwordroot", "");
	editor.apply();
	pref = getSharedPreferences("data",MODE_PRIVATE);
	userET = (EditText)findViewById(R.id.userET);
    passwordET =(EditText)findViewById(R.id.passwordET);
    rememberPass = (CheckBox)findViewById(R.id.savePasswordCB);
    logBT = (Button)findViewById(R.id.logBT);
    rfTX = (TextView)findViewById(R.id.rfTX);
    
    rfTX.setOnClickListener(new View.OnClickListener() {

		public void onClick(View v) {
			Intent intent = new Intent(Login.this,Register.class);// TODO Auto-generated method stub
			startActivity(intent);
		}
	});
    
    boolean isRemember = pref.getBoolean("remember_password", false);
    if(isRemember)
    {
    	userStr = pref.getString("user", "");
    	passwordStr = pref.getString("password", "");
    	userET.setText(userStr);
    	passwordET.setText(passwordStr);
    }
    
    logBT.setOnClickListener(new View.OnClickListener() {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			userStr = userET.getText().toString();
			passwordStr = passwordET.getText().toString();
			userrootStr = pref.getString("userroot", "");
	    	passwordrootStr = pref.getString("passwordroot", "");
	    	if(!userrootStr.equals(""))
	    	{
	    		if(userStr.equals(userrootStr)&&passwordStr.equals(passwordrootStr))
	    		{
	    			editor = pref.edit();
	    			if(rememberPass.isChecked())
	    			{
	    				editor.putBoolean("remember_password", true);
	    				editor.putString("user", userStr);
	    				editor.putString("password", passwordStr);
	    				editor.apply();
	    			}
	    			else
	    			{
	    				userET.setText("");
	    				passwordET.setText("");
	    			}
	    			Intent intent = new Intent(Login.this,MainActivity.class);
	    			startActivity(intent);
	    			finish();
	    		}
	    		else
	    		{
	    			Toast.makeText(Login.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
	    		}
	    	}
	    	else
	    	{
	    		Toast.makeText(Login.this, "请先注册", Toast.LENGTH_SHORT).show();
	    	}
		}
	});
 ExitApplication.getInstance().addActivity(this); 
 }
 protected void dialog() { 
     AlertDialog.Builder builder = new Builder(Login.this); 
     builder.setMessage("亲,确定要退出吗"); 
     builder.setTitle("提示"); 
     builder.setPositiveButton("确定", 
      new android.content.DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
       dialog.dismiss(); 
       Login.this.finish(); 
       } 
      }); 
     builder.setNegativeButton("取消", 
      new android.content.DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
       dialog.dismiss(); 
       
       } 
      }); 
     builder.create().show(); 
    } 
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { 
      dialog(); 
      return false; 
     } 
     return false; 
    }
  
}
解决方案

19

引用:

有没有大佬啊

本人至始至终没看到你注册成功之后用sp没有commit();

1

不知道想问啥问题,

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明关于sharedPreference实现注册登录的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)