布局文件 :login.xml
<?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_mima"
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" />
<CheckBox
android:id="@+id/cb_auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/cb_mima"
android:layout_marginLeft="12dip"
android:text="自动登录"
android:textColor="#000000" />
<Button
android:id="@+id/btn_login"
android:layout_width="80dip"
android:layout_height="40dip"
android:layout_below="@id/et_mima"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/cb_auto"
android:layout_marginRight="10dip"
android:gravity="center"
android:text="登录"
android:textColor="#000000"
android:textSize="18sp"/>
</RelativeLayout>
</LinearLayout>
布局文件:logo.xml
<?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>
布局文件:welcom.xml
<?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>
java代码:LoginActivity.java
package com.wang.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 rem_pw, auto_login;
private Button btn_login;
private ImageButton btnQuit;
private String userNameValue,passwordValue;
private SharedPreferences sp;
private final String MAK = "innoview";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去掉TitleBar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.login);
//获取shareprefrence里面的数据
sp = this.getSharedPreferences("userInfo",Context.MODE_WORLD_READABLE);
userName = (EditText) findViewById(R.id.et_zh);
passWord = (EditText) findViewById(R.id.et_mima);
rem_pw = (CheckBox) findViewById(R.id.cb_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))
{
//记住密码框状态标记为选中
rem_pw.setChecked(true);
try {
userNameValue=sp.getString("USER_NAME", "");
System.out.println("<<<<<<<<<<<<"+"加密后的用户名"+userNameValue);
String username=AESEncryptor.decrypt(MAK,userNameValue);
System.out.println("<<<<<<<<<<<<"+"解密后的用户名"+username);
userName.setText(username);
} catch (Exception e) {
Toast.makeText(LoginActivity.this,"用户名解密异常",Toast.LENGTH_SHORT).show();
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
String password= null;
try {
passwordValue=sp.getString("PASSWORD", "");
System.out.println("<<<<<<<<<<<<"+"加密后的密码"+passwordValue);
password = AESEncryptor.decrypt(MAK, passwordValue);
System.out.println("<<<<<<<<<<<<"+"解密后的密码"+password);
} catch (Exception e) {
Toast.makeText(LoginActivity.this,"密码解密异常",Toast.LENGTH_SHORT).show();
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
passWord.setText(password);
//判断自动登录框状态
if(sp.getBoolean("AUTO_ISCHECK", false))
{
//自动登录框状态标记为选中
auto_login.setChecked(true);
Intent intent = new Intent(LoginActivity.this,LogoActivity.class);
LoginActivity.this.startActivity(intent);
}
}
// 用户名:login 秘密:123456
btn_login.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
userNameValue = userName.getText().toString();
passwordValue = passWord.getText().toString();
if(userNameValue.equals("login")&&passwordValue.equals("123456"))
{
Toast.makeText(LoginActivity.this,"登陆成功", Toast.LENGTH_SHORT).show();
//如果记住密码框未选中状态
if(rem_pw.isChecked())
{
Editor editor = sp.edit();
// AESEncryptor.encrypt(userNameValue);
try {
editor.putString("USER_NAME", AESEncryptor.encrypt(MAK,userNameValue));
System.out.println("<<<<<<<<"+"加密后的用户名"+AESEncryptor.encrypt(MAK,userNameValue));
} catch (Exception e) {
Toast.makeText(LoginActivity.this,"用户名加密异常",Toast.LENGTH_SHORT).show();
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
try {
editor.putString("PASSWORD",AESEncryptor.encrypt(MAK,passwordValue));
System.out.println("<<<<<<<<"+"加密后的密码"+AESEncryptor.encrypt(MAK,passwordValue));
} catch (Exception e) {
Toast.makeText(LoginActivity.this,"密码加密异常",Toast.LENGTH_SHORT).show();
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
editor.commit();
}
Intent intent = new Intent(LoginActivity.this,LogoActivity.class);
LoginActivity.this.startActivity(intent);
//finish();
}else{
Toast.makeText(LoginActivity.this,"登录失败,用户名或密码错误", Toast.LENGTH_LONG).show();
}
}
});
//标记记住密码框状态
rem_pw.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if (rem_pw.isChecked()) {
System.out.println("记住密码框未选中状态");
sp.edit().putBoolean("ISCHECK", true).commit();
}else {
System.out.println("记住密码框未选中");
sp.edit().putBoolean("ISCHECK", false).commit();
}
}
});
//标记自动登录框状态
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) {
finish();
}
});
}
}
java代码:AESEncryptor.java(AES加密解密类,说实话小五也看的云里雾里的)
package com.wang.activity;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
/**
* Created with IntelliJ IDEA.
* User: hld
* Date: 13-12-20
* Time: ����10:47
* To change this template use File | Settings | File Templates.
*/
public class AESEncryptor {
/**
* AES����
*/
public static String encrypt(String seed, String cleartext) throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] result = encrypt(rawKey, cleartext.getBytes());
return toHex(result);
}
/**
* AES����
*/
public static String decrypt(String seed, String encrypted) throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] enc = toByte(encrypted);
byte[] result = decrypt(rawKey, enc);
return new String(result);
}
private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(seed);
kgen.init(128, sr); // 192 and 256 bits may not be available
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return raw;
}
private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(clear);
return encrypted;
}
private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}
public static String toHex(String txt) {
return toHex(txt.getBytes());
}
public static String fromHex(String hex) {
return new String(toByte(hex));
}
public static byte[] toByte(String hexString) {
int len = hexString.length()/2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++)
result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
return result;
}
public static String toHex(byte[] buf) {
if (buf == null)
return "";
StringBuffer result = new StringBuffer(2*buf.length);
for (int i = 0; i < buf.length; i++) {
appendHex(result, buf[i]);
}
return result.toString();
}
private final static String HEX = "0123456789ABCDEF";
private static void appendHex(StringBuffer sb, byte b) {
sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
}
}
java代码:LogoActivity.java
package com.wang.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
public class LogoActivity 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.logo);
progressBar = (ProgressBar) findViewById(R.id.pgBar);
backButton = (Button) findViewById(R.id.btn_back);
progressBar.setMax(3000);
Intent intent = new Intent(this, WelcomeAvtivity.class);
LogoActivity.this.startActivity(intent);
backButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
java代码:WelcomActivity.java
package com.wang.activity;
import android.app.Activity;
import android.os.Bundle;
public class WelcomeAvtivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
}
小五奉上源码