Android自動登錄功能的實現

登陸頁面佈局設計:


 <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/account" />

        <EditText
            android:id="@+id/edtaccount"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:singleLine="true" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/password" />

        <EditText
            android:id="@+id/edtpassword"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:singleLine="true" />
    </LinearLayout>

    <Button
        android:id="@+id/btnlogin"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="@string/login" />


註銷頁面佈局設計:



    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="@string/註銷頁面"
        android:textSize="15sp" />

    <Button
        android:id="@+id/btncancel"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="@string/cancel" />


LoginActivity.java:

package com.xiaoyan.autologin;

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.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {

	// 定義組件
	private EditText edtAccount;
	private EditText edtPassword;
	private Button btnLogin;

	// 用於記錄帳號和密碼
	private String strAccount = "";
	private String strPassword = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_main);

		// 設置標題
		setTitle("Login");

		// 獲取sharedpreferences對象
		SharedPreferences share = getSharedPreferences("Login",
				Context.MODE_PRIVATE);
		strAccount = share.getString("Account", "");
		strPassword = share.getString("Password", "");

		// 判斷是否是之前有登錄過
		if (share == null) {
			init();
		} else {
			// 判斷是否剛註銷
			if (share.getBoolean("LoginBool", false)) {
				// 跳轉到註銷頁面並銷燬當前activity
				Intent intent = new Intent(LoginActivity.this,
						CancelActivity.class);
				startActivity(intent);
				finish();
			} else {

				init();
			}
		}

	}

	private void init() {

		// 初始化組件
		edtAccount = (EditText) findViewById(R.id.edtaccount);
		edtPassword = (EditText) findViewById(R.id.edtpassword);
		btnLogin = (Button) findViewById(R.id.btnlogin);

		edtAccount.setText(strAccount);
		edtPassword.setText(strPassword);
		
		// 監聽按鈕
		btnLogin.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// 判斷帳號和密碼是輸入是否爲空
				if (edtAccount.getText().toString().equals("")
						|| edtPassword.getText().toString().equals("")) {
					Toast.makeText(LoginActivity.this, "帳號或密碼不能爲空",
							Toast.LENGTH_SHORT).show();
				} else {
					// 創建SharedPreferences對象用於儲存帳號和密碼,並將其私有化
					SharedPreferences share = getSharedPreferences("Login",
							Context.MODE_PRIVATE);
					// 獲取編輯器來存儲數據到sharedpreferences中
					Editor editor = share.edit();
					editor.putString("Account", edtAccount.getText().toString());
					editor.putString("Password", edtPassword.getText()
							.toString());
					editor.putBoolean("LoginBool", true);
					// 將數據提交到sharedpreferences中
					editor.commit();

					// 跳轉到註銷頁面並銷燬當前activity
					Intent intent = new Intent(LoginActivity.this,
							CancelActivity.class);
					startActivity(intent);
					finish();
				}

			}
		});
	}

}

CancelActivity.java:

package com.xiaoyan.autologin;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class CancelActivity extends Activity {

	// 定義組件
	private Button btnCancel;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cancel_activity);

		// 設置標題
		setTitle("Cancel");
		// 初始化頁面
		init();

	}

	private void init() {
		// 初始化組件
		btnCancel = (Button) findViewById(R.id.btncancel);

		// 監聽註銷按鈕
		btnCancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub

				// 註銷帳號並銷燬當前頁面
				SharedPreferences share = getSharedPreferences("Login",
						Context.MODE_PRIVATE);
				share.edit().putBoolean("LoginBool", false).commit();
				
				Intent intent = new Intent(CancelActivity.this,
						LoginActivity.class);
				startActivity(intent);
				finish();
			}
		});
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章