1. 程式人生 > >第一次登陸 記住密碼第二次進入直接進入展示頁面

第一次登陸 記住密碼第二次進入直接進入展示頁面

//佈局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <EditText
        android:id="@+id/edit_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="90dp"
        android:hint="請輸入賬號"
        android:ems="10" >
        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/edit_pass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edit_name"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="47dp"
        android:hint="請輸入密碼"
        android:ems="10" />

    <CheckBox
        android:id="@+id/check_remember"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/edit_pass"
        android:layout_below="@+id/edit_pass"
        android:layout_marginTop="29dp"
        android:text="記住密碼" />

    <CheckBox
        android:id="@+id/check_voluntarily"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/check_remember"
        android:layout_alignBottom="@+id/check_remember"
        android:layout_alignRight="@+id/edit_pass"
        android:text="自動登入" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/check_remember"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:text="登入" />

</RelativeLayout>

//main主介面操作

package com.example.day09_sharedpreferences_demo;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;

public class MainActivity extends Activity {
	
	private SharedPreferences sharedPreferences;
	private Editor editor;
	private EditText edit_name,edit_pass;
	private CheckBox check_remember,check_voluntarily;
	private Button button;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //1.獲取資源ID
        findByID();
        //2.得到sharedPreferences
        	sharedPreferences = getSharedPreferences("User", MODE_PRIVATE);
        	//得到editor
        	editor = sharedPreferences.edit();
        	
        	//7.取出記住密碼的狀態值進行判斷
        	boolean r_ischeck = sharedPreferences.getBoolean("r_ischeck", false);
        	//8.判斷狀態值 如果為true就記住密碼,如果為false就不記住
        	if(r_ischeck){
        		String name = sharedPreferences.getString("name", null);
        		String pass= sharedPreferences.getString("pass", null);
        		
        		edit_name.setText(name);
        		edit_pass.setText(pass);
        		check_remember.setChecked(true);
        	}
        	
        	
        	//取出自動登入的狀態值
        	boolean v_ischeck = sharedPreferences.getBoolean("v_ischeck", false);
        	if(v_ischeck){
        		Intent intent = new Intent(MainActivity.this,LoginActivity.class);
			startActivity(intent);
			finish();
        	}
        	
        	//勾選自動登入同時記住密碼
        	check_voluntarily.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					// TODO Auto-generated method stub
					if(isChecked){
						//設定記住密碼為勾選
						check_remember.setChecked(true);
						}else{
							//清空
							editor.clear();
							editor.commit();
						}
				}
			});
        	
        	
        	//3.點選按鈕將值存入sharedPreferences
        	button.setOnClickListener(new OnClickListener() {
				 
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					//4.判斷記住密碼是否有沒有勾選
					if(check_remember.isChecked()){
						//5.獲取輸入框的值
						String name = edit_name.getText().toString();
						String pass = edit_pass.getText().toString();
						//6.將值存入sharedPreferences
						editor.putString("name", name);
						editor.putString("pass", pass);
						//存入一個勾選了的狀態值
						editor.putBoolean("r_ischeck", true);
						//提交
						editor.commit();
					}else{
						//清空
						editor.clear();
						editor.commit();
					}
					
					//自動登入
					if(check_voluntarily.isChecked()){
						editor.putBoolean("v_ischeck", true);
						editor.commit();
					}
					
					Intent intent = new Intent(MainActivity.this,LoginActivity.class);
					startActivity(intent);
					finish();
				}
			});
        	
        	
    }
    
    
    public void findByID(){
    		edit_name = (EditText) findViewById(R.id.edit_name);
    		edit_pass = (EditText) findViewById(R.id.edit_pass);
    		check_remember = (CheckBox) findViewById(R.id.check_remember);
    		check_voluntarily = (CheckBox) findViewById(R.id.check_voluntarily);
    		button = (Button) findViewById(R.id.button1);
    }
    
}

//登入成功進入 頁面佈局 (點選登出)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登入成功" />

    <Button
        android:id="@+id/l_but"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="182dp"
        android:text="登出" />

</RelativeLayout>

//登出程式碼操作

package com.example.day09_sharedpreferences_demo;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class LoginActivity extends Activity {

	private SharedPreferences sharedPreferences;
	private Editor editor;
	private Button button;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		//獲取資源ID
		button = (Button) findViewById(R.id.l_but);
		sharedPreferences = getSharedPreferences("User", MODE_PRIVATE);
		editor = sharedPreferences.edit();
		
		//點選登出返回
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//清空
				editor.clear();
				editor.commit();
				
				Intent intent = new Intent(LoginActivity.this,MainActivity.class);
				startActivity(intent);
				finish();
			}
		});
		
	}
}