登陆页面布局设计:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/account" />
android:id="@+id/edtaccount"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:inputType="number"
android:singleLine="true" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/password" />
android:id="@+id/edtpassword"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:singleLine="true" />
android:id="@+id/btnlogin"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/login" />
注销页面布局设计:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/注销页面"
android:textSize="15sp" />
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();
}
});
}
}