您的位置:首页 > 其它

通用电商项目关于登陆注册自动的实现

2015-03-27 16:14 447 查看
还是使用sharedpreference来实现。

案例在android 2.5g/自动登陆注册/sharedpreferences

loginActivity 与 welcomeActivity例子。

LoginActivity

package com.liu.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

public class LoginActivity extends Activity {

	private EditText userName, password;
	private CheckBox auto_login;
	private Button btn_login, btn_forget_pass;
	private ImageButton btnQuit;
	private String userNameValue, passwordValue;
	private SharedPreferences sp;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// 去除标题
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.login);

		// 获得实例对象
		sp = this.getSharedPreferences("userInfo",MODE_PRIVATE);
		userName = (EditText) findViewById(R.id.et_zh);
		password = (EditText) findViewById(R.id.et_mima);
		btn_forget_pass = (Button) findViewById(R.id.btn_mima);
		auto_login = (CheckBox) findViewById(R.id.cb_auto);
		btn_login = (Button) findViewById(R.id.btn_login);
		btnQuit = (ImageButton) findViewById(R.id.img_btn);

		// 判断记住密码多选框的状态
		if (sp.getBoolean("ISCHECK", false)) {
			// 设置默认是记录密码状态
			userName.setText(sp.getString("USER_NAME", ""));
			password.setText(sp.getString("PASSWORD", ""));
			// 判断自动登陆多选框状态
			if (sp.getBoolean("AUTO_ISCHECK", false)) {
				// 跳转界面
				Intent intent = new Intent(LoginActivity.this,
						JumpActivity.class);
				LoginActivity.this.startActivity(intent);
				LoginActivity.this.finish();
			}
		}

		// 登录监听事件 现在默认为用户名为:liu 密码:123
		btn_login.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				userNameValue = userName.getText().toString();
				passwordValue = password.getText().toString();
				if (userNameValue.equals("liu") && passwordValue.equals("123")) {
					Toast.makeText(LoginActivity.this, "登录成功",
							Toast.LENGTH_SHORT).show();
					// 登录成功和记住密码框为选中状态才保存用户信息
					if (auto_login.isChecked()) {
						// 记住用户名、密码、
						Editor editor = sp.edit();
						editor.putString("USER_NAME", userNameValue);
						editor.putString("PASSWORD", passwordValue);
						editor.commit();
					}
					// 跳转界面
					Intent intent = new Intent(LoginActivity.this,
							JumpActivity.class);
					LoginActivity.this.startActivity(intent);
					LoginActivity.this.finish();

				} else {
					Toast.makeText(LoginActivity.this, "用户名或密码错误,请重新登录",
							Toast.LENGTH_LONG).show();
				}

			}
		});

		// 监听记住密码多选框按钮事件
		auto_login.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (auto_login.isChecked()) {
					System.out.println("记住密码已选中");
					sp.edit().putBoolean("ISCHECK", true).commit();
				} else {
					System.out.println("记住密码没有选中");
					sp.edit().putBoolean("ISCHECK", false).commit();
				}
			}
		});
		btnQuit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				LoginActivity.this.finish();
			}
		});

	}
}


WelcomeActivity.java
package com.liu.activity;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;

public class WelcomeAvtivity extends Activity {
	private SharedPreferences sp;
	private String str;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.welcome);
		sp = this.getSharedPreferences("userInfo", MODE_PRIVATE);
		initView();
	}

	private void initView() {
		// TODO Auto-generated method stub
		Toast.makeText(getApplicationContext(),
						sp.getString("USER_NAME", "") , Toast.LENGTH_SHORT).show();
		str = sp.getString("USER_NAME", "");
		if(str.isEmpty()){
			Toast.makeText(getApplicationContext(), "no", Toast.LENGTH_SHORT).show();
		}else{
			Toast.makeText(getApplicationContext(), "yes", Toast.LENGTH_SHORT).show();
		}
	}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐