Android实战简易教程-第六十六枪(结合SharedPreferenced实现自动登录功能)

我们使用的一般应用都有记住密码、自动登录功能,这样不用用户每次都要点击登录按钮,提升用户体验,下面我们通过一个实例研究一下如何通过android的sharedpreferenced实现自动登录功能。本实例有三个界面-登录界面,跳转界面,登录成功界面。

1.登录界面:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/logo_bg"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <ImageButton
            android:id="@+id/img_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:background="@drawable/quit" />

        <TextView
            android:id="@+id/tv_zh"
            android:layout_width="wrap_content"
            android:layout_height="35dip"
            android:layout_marginLeft="12dip"
            android:layout_marginTop="10dip"
            android:gravity="bottom"
            android:text="帐号:"
            android:textColor="#000000"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/et_zh"
            android:layout_width="fill_parent"
            android:layout_height="40dip"
            android:layout_below="@id/tv_zh"
            android:layout_marginLeft="12dip"
            android:layout_marginRight="10dip" />

        <TextView
            android:id="@+id/tv_mima"
            android:layout_width="wrap_content"
            android:layout_height="35dip"
            android:layout_below="@id/et_zh"
            android:layout_marginLeft="12dip"
            android:layout_marginTop="10dip"
            android:gravity="bottom"
            android:text="密码:"
            android:textColor="#000000"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/et_mima"
            android:layout_width="fill_parent"
            android:layout_height="40dip"
            android:layout_below="@id/tv_mima"
            android:layout_marginLeft="12dip"
            android:layout_marginRight="10dip"
            android:maxLines="200"
            android:password="true"
            android:scrollHorizontally="true" />

        <CheckBox
            android:id="@+id/cb_auto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/et_mima"
            android:layout_marginLeft="12dip"
            android:text="自动登录"
            android:textColor="#000000" />

        <Button
            android:id="@+id/btn_mima"
            android:layout_width="100dip"
            android:layout_height="40dip"
            android:layout_below="@id/cb_auto"
            android:layout_marginLeft="12dip"
            android:gravity="center"
            android:text="忘记密码"
            android:textColor="#000000"
            android:textSize="18sp" />

        <Button
            android:id="@+id/btn_login"
            android:layout_width="80dip"
            android:layout_height="40dip"
            android:layout_alignRight="@+id/et_mima"
            android:layout_below="@+id/cb_auto"
            android:gravity="center"
            android:text="登录"
            android:textColor="#000000"
            android:textSize="18sp" />

    </RelativeLayout>

</LinearLayout>

2.跳转界面:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/logo_bg"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_weight="3">

        <ProgressBar
            android:id="@+id/pgBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />

        <TextView
            android:id="@+id/tv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/pgBar"
            android:layout_centerHorizontal="true"
            android:text="正在登录..."
            android:textColor="#000000"
            android:textSize="18sp" />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" >

        <Button
            android:id="@+id/btn_back"
            android:layout_width="70dip"
            android:layout_height="35dip"
            android:text="取消"
            android:textColor="#000000"
            android:textSize="12sp" />
    </LinearLayout>


</LinearLayout>

3.成功界面:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:background="@drawable/login_bg"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="登陆成功,进入用户界面"
        android:textColor="#000000"
        android:textSize="20sp" />


</LinearLayout>

下面看一下3个activity:

LoginActivity.java:

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", Context.MODE_WORLD_READABLE);
		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("AUTO_ISCHECK", false)) {
				// 设置默认是记录密码状态
				userName.setText(sp.getString("USER_NAME", ""));
				password.setText(sp.getString("PASSWORD", ""));
				
			}else if(sp.getBoolean("AUTO_ISCHECK", true)){
				Intent intent = new Intent(LoginActivity.this,
						JumpActivity.class);
				LoginActivity.this.startActivity(intent);
				LoginActivity.this.finish();
				auto_login.setChecked(true);
			}
		
		

		

		// 登录监听事件 现在默认为用户名为:yayun 密码:123
		btn_login.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				userNameValue = userName.getText().toString();
				passwordValue = password.getText().toString();
				if (userNameValue.equals("yayun") && 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("AUTO_ISCHECK", true).commit();
				} else {
					System.out.println("记住密码没有选中");
					sp.edit().putBoolean("AUTO_ISCHECK", false).commit();
				}
			}
		});
		btnQuit.setOnClickListener(new OnClickListener() {

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

	}
}

2.JumpActivity.java:

package com.liu.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;

public class JumpActivity extends Activity {
	private ProgressBar progressBar;
	private Button backButton;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 去除标题
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.jump);

		progressBar = (ProgressBar) findViewById(R.id.pgBar);
		
		backButton = (Button) findViewById(R.id.btn_back);
		
		startWelcomeAvtivity();
		backButton.setOnClickListener(new OnClickListener() {

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

	}

	private void startWelcomeAvtivity() {
		new Handler().postDelayed(new Runnable() {//延迟操作
			public void run() {
				Intent intent = new Intent(JumpActivity.this,
						WelcomeAvtivity.class);
				JumpActivity.this.startActivity(intent);
				JumpActivity.this.finish();
			}
		},2000);// 设置执行时间2秒
	}

}
3.Welcome.java:

package com.liu.activity;

import android.app.Activity;
import android.os.Bundle;

public class WelcomeAvtivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.welcome);
		
	}
}
运行实例:

这时应该记录住密码,可以自行自动登录了,我们测试一下,退出应用后再次进入,如下:

如果你想更完善一些,可以自己做一个登出的操作哦!

喜欢的朋友关注我!
源码下载

你可能感兴趣的:(android,自动登录)