1.效果图
2.代码
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.EditText;
import android.widget.Toast;
public class LoginActivity extends Activity {
private EditText etUsername;
private EditText etPassword;
private CheckBox cbRememberPass;
private Button btnLogin;
protected SharedPreferences mSpSettings = null;
private static final String PREFS_NAME = "NamePwd";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_login);
etUsername = (EditText) findViewById(R.id.etUsername);
etPassword = (EditText) findViewById(R.id.etPassword);
cbRememberPass = (CheckBox) findViewById(R.id.cbRememberPass);
btnLogin = (Button) findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
if ("login".equals(etUsername.getText().toString())
&& "123456".equals(etPassword.getText().toString())) {
// 判断用户名和密码
if (cbRememberPass.isChecked()) {
// 判断复选框是否选中
mSpSettings = getSharedPreferences(PREFS_NAME,
Context.MODE_PRIVATE);
Editor edit = mSpSettings.edit();
edit.putBoolean("isKeeep", true);
edit.putString("username", etUsername.getText()
.toString());
edit.putString("password", etPassword.getText()
.toString());
edit.commit();
// 选中后记住密码
} else {
mSpSettings = getSharedPreferences(PREFS_NAME,
MODE_PRIVATE);
Editor edit = mSpSettings.edit();
edit.putBoolean("isKeeep", false);
edit.putString("username", "");
edit.putString("password", "");
edit.commit();
// 未选中没有记住密码
}
Intent intent = new Intent(LoginActivity.this,
MainActivity.class);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), "用户名或密码错误",
Toast.LENGTH_SHORT).show();
}
}
});
getData();
}
protected void onResume() {
super.onResume();
getData();
}// 在界面显示之前得到数据
private void getData() {
// TODO Auto-generated method stub
mSpSettings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
if (mSpSettings.getBoolean("isKeep", true)) {
etUsername.setText(mSpSettings.getString("username", ""));
etPassword.setText(mSpSettings.getString("password", ""));
}// 如果之前存储过,则显示在相应的文本里
else {
etUsername.setText("");
etPassword.setText("");
}// 如果之前没有显示过,则显示为空。
}
}
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
<LinearLayout 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:orientation="vertical"
android:background="@drawable/loginbg"
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=".LoginActivity" >
<include layout="@layout/login_top"/>
<include layout="@layout/login_bottom"/>"
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tvRegist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="21dp"
android:layout_marginTop="18dp"
android:text="@string/tvRegister"
android:autoLink="all"
android:textColorLink="#FF0066CC" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="24dp"
android:src="@drawable/panda" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="28dp"
android:src="@drawable/icon" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/btnbg_roundcorner"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<TextView
android:id="@+id/tvUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/tvName"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/etUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvUsername"
android:layout_below="@+id/tvUsername"
android:background="@android:drawable/edit_text"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/tvPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/etUsername"
android:layout_below="@+id/etUsername"
android:text="@string/tvPassword"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvPassword"
android:layout_below="@+id/tvPassword"
android:background="@android:drawable/edit_text"
android:ems="10"
android:inputType="textPassword" />
<CheckBox
android:id="@+id/cbRememberPass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btnLogin"
android:layout_alignBottom="@+id/btnLogin"
android:layout_alignLeft="@+id/etPassword"
android:text="记住密码"
android:textSize="20sp" />
<Button
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/etPassword"
android:layout_below="@+id/etPassword"
android:background="#FF72CAE1"
android:onClick="login"
android:text="@string/btnLogin" />
</RelativeLayout>