android 登陸時記住密碼

android 登陸時記住密碼

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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.cdc.http.MainActivity$PlaceholderFragment" >

    
    <EditText 
        android:id="@+id/main_et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="用戶名"
       
        />
    
     <EditText 
        android:id="@+id/main_et2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/main_et"
        android:hint="密碼"
        />
     
     
     <CheckBox 
         android:id="@+id/main_checkbox"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_below="@id/main_et2"/>
     
     <TextView 
         android:id="@+id/main_tv"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="記住我"
         android:layout_toRightOf="@id/main_checkbox"
         android:layout_below="@id/main_et2"
         android:layout_alignBottom="@id/main_checkbox"/>
    
    <Button 
        android:id="@+id/main_bt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登錄"
        android:layout_below="@id/main_checkbox"/>
    
    
   
 
</RelativeLayout>

2.代碼
package com.cdc.http;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
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;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class MainActivity extends Activity {

	private EditText etName;
	private EditText etPassword;

	private CheckBox checkBox;

	private Button denglu;

	private String name;

	private String password;

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

		etName = (EditText) findViewById(R.id.main_et);

		etPassword = (EditText) findViewById(R.id.main_et2);

		checkBox = (CheckBox) findViewById(R.id.main_checkbox);

		denglu = (Button) findViewById(R.id.main_bt);

		SharedPreferences sharedPreferences = getSharedPreferences("mima",
				Context.MODE_PRIVATE);

		String temp_name = sharedPreferences.getString("name", "");

		String temp_password = sharedPreferences.getString("password", "");

		boolean temp_bl = sharedPreferences.getBoolean("check", false);

		etName.setText(temp_name);

		etPassword.setText(temp_password);

		checkBox.setChecked(temp_bl);

		// 監聽用戶名輸入值的變化
		etName.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				System.out.println("用戶名改變了===" + etName.getText().toString());
				
			if(checkBox.isChecked()){

				SharedPreferences sharedPreferences = getSharedPreferences(
						"mima", Context.MODE_PRIVATE);

				Editor editor = sharedPreferences.edit();

				editor.putString("name", etName.getText().toString());
				editor.commit();
			}
			}
		});

		// 監聽用戶名輸入值的變化
		etPassword.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				System.out
						.println("密碼改變了===" + etPassword.getText().toString());
				if(checkBox.isChecked()){
				SharedPreferences sharedPreferences = getSharedPreferences(
						"mima", Context.MODE_PRIVATE);

				Editor editor = sharedPreferences.edit();

				editor.putString("password", etPassword.getText().toString());
				editor.commit();
				}
			}
		});

		denglu.setOnClickListener(new OnClickListener() {

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

				name = etName.getText().toString();

				password = etPassword.getText().toString();

				System.out.println("登錄時用戶名==" + name + ",登陸時密碼==" + password);

			}
		});

		checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				// TODO Auto-generated method stub
				System.out.println("點擊了checkbox");
				if (arg1) {
					

					SharedPreferences sharedPreferences = getSharedPreferences(
							"mima", Context.MODE_PRIVATE);

					Editor editor = sharedPreferences.edit();

					name = etName.getText().toString();

					password = etPassword.getText().toString();
					editor.putString("name", name);

					editor.putString("password", password);

					editor.putBoolean("check", true);

					editor.commit();
				}else{
					SharedPreferences sharedPreferences = getSharedPreferences(
							"mima", Context.MODE_PRIVATE);

					Editor editor = sharedPreferences.edit();

					name = etName.getText().toString();

					password = etPassword.getText().toString();
					editor.putString("name", "");

					editor.putString("password", "");

					editor.putBoolean("check", false);

					editor.commit();
				}
			}
		});

	}

}


發佈了101 篇原創文章 · 獲贊 16 · 訪問量 23萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章