需求:
当用户点击记住用户名时,我们在下次启动Activity时就可以自动添加用户名。
思路:
这里当用户点击记住用户名时,我们需要把用户输入的用户名写入文件,然后在下次启动Activity时
直接从内存中读取用户名添加到用户名的输入框。
代码如下:
xml文件:
<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"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:text="用户名" />
<EditText
android:id="@+id/userEt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView1"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/textView1"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="30dp"
android:text="密 码"
android:textSize="20sp" />
<EditText
android:id="@+id/passEt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/userEt"
android:ems="10"
android:inputType="numberPassword" />
<CheckBox
android:id="@+id/chkSaveUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="15dp"
android:checked="true"
android:text="保存用户名" />
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/chkSaveUser"
android:layout_below="@+id/chkSaveUser"
android:layout_marginTop="24dp"
android:text="登录"
android:onClick="loginClick"/>
<Button
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/login"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
android:text="注册" />
</RelativeLayout>
主程序如下:
package org.mobiletrain.sharepreferenced_demo02;
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.EditText;
import android.widget.Toast;
//存储用户登录的用户名
public class MainActivity extends Activity {
private EditText userEt,passEt;
private CheckBox chkSave;
SharedPreferences pref;
Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userEt = (EditText)findViewById(R.id.userEt);
passEt = (EditText)findViewById(R.id.passEt);
chkSave = (CheckBox)findViewById(R.id.chkSaveUser);
pref = getSharedPreferences("name_pref", MODE_PRIVATE);
editor = pref.edit();
String name = pref.getString("name", null);
if (name == null) {
chkSave.setChecked(false);
}else{
userEt.setText(name);
chkSave.setChecked(true);
}
}
public void loginClick(View v){
String name = userEt.getText().toString();
String pass = passEt.getText().toString();
if ("admin".equals(name)&&"123456".equals(pass)) {
if (chkSave.isChecked()) {
editor.putString("name", name);
editor.commit();
}else{
editor.remove("name");
editor.commit();
}
Toast.makeText(MainActivity.this, "登录成功", 0).show();
}else{
Toast.makeText(MainActivity.this, "登录失败", 0).show();
}
}
}