数据存储--SharedPreferences--仿QQ登陆界面记住密码效果
SharedPreferences:共享数据存储
实现效果:1 用户输入用户名和密码,点击记住密码时,记住此用户名和密码,否则报空
2 用户点击登陆时,首先判断是否记住密码,是则弹出用户名和密码
布局文件
<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" >
<LinearLayout
android:id="@+id/l1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名"
android:textSize="18sp" />
<EditText
android:id="@+id/edit_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/l2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"
android:textSize="18sp" />
<EditText
android:id="@+id/edit_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
<requestFocus />
</LinearLayout>
<LinearLayout
android:id="@+id/l3"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登陆"
android:textSize="18sp" />
</LinearLayout>
<CheckBox
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"
android:textSize="18sp" />
</LinearLayout>
逻辑代码
<span style="font-family:Comic Sans MS;font-size:18px;">package com.example.week3_day3_login;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
//声明控件
private EditText name;
private EditText password;
private Button login;
private CheckBox save;
private String names;
private String passwords;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到控件id
name = (EditText) findViewById(R.id.edit_name);
password = (EditText) findViewById(R.id.edit_password);
login = (Button) findViewById(R.id.login);
save = (CheckBox) findViewById(R.id.save);
login.setOnClickListener(this);
save.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// 得到用户名和密码
names = name.getText().toString().trim();
passwords = password.getText().toString().trim();
switch (v.getId()) {
//点击登陆按钮时
case R.id.login:
if (!save.isChecked()) {
Toast.makeText(getApplicationContext(), "没有记住用户名和密码",
Toast.LENGTH_SHORT).show();
} else {
//取出数据
SharedPreferences preferences2 = getPreferences(Context.MODE_PRIVATE);
String name2 = preferences2.getString("names", "default");
String password2 = preferences2.getString("passwords",
"default");
Toast.makeText(MainActivity.this,
"用户名:" + name2 + "\n密码:" + password2,
Toast.LENGTH_SHORT).show();
}
break;
//点击记住密码按钮
case R.id.save:
if (names.equals("") || passwords.equals("")) {
Toast.makeText(getApplicationContext(), "用户名或密码不能为空",
Toast.LENGTH_SHORT).show();
} else {
//保存数据
SharedPreferences preferences = getPreferences(Context.MODE_PRIVATE);
Editor editor = preferences.edit();//获得编辑者对象
editor.putString("names", names);
editor.putString("passwords", passwords);
editor.commit();//提交
Toast.makeText(MainActivity.this, "记住了密码和用户名",
Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
}
</span>