布局文件:
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"/>
<EditText
android:id="@+id/user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入你的用户名!"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"/>
<EditText
android:id="@+id/psw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入你的密码!"/>
</LinearLayout>
<CheckBox
android:id="@+id/remember"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="记住密码"/>
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doClick"
android:text="登录"/>
</LinearLayout>
package com.duolyn.administrator.logintest
import android.app.Activity
import android.content.SharedPreferences
import android.content.SharedPreferences.Editor
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.Toast
import static com.duolyn.administrator.logintest.R.id.login
import static com.duolyn.administrator.logintest.R.id.psw
import static com.duolyn.administrator.logintest.R.id.user
public class MainActivity extends AppCompatActivity {
private SharedPreferences sp
private EditText user_et, psw_et
private Button btn
private CheckBox checkBox
private Editor editor
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
sp = getSharedPreferences("test", Activity.MODE_PRIVATE)
editor = sp.edit()
user_et = (EditText) findViewById(R.id.user)
psw_et = (EditText) findViewById(R.id.psw)
checkBox = (CheckBox) findViewById(R.id.remember)
btn = (Button) findViewById(R.id.login)
String user = sp.getString("user", null)
String psw = sp.getString("psw", null)
if (user == null && psw == null) {
checkBox.setChecked(false)
user_et.setText(user)
psw_et.setText(psw)
}
else {
checkBox.setChecked(true)
user_et.setText(user)
psw_et.setText(psw)
}
}
public void doClick(View v) {
if (checkBox.isChecked()) {
String username = user_et.getText().toString()
String password = psw_et.getText().toString()
editor.putString("user", username)
editor.putString("psw", password)
editor.commit()
Toast.makeText(getApplicationContext(), "登录成功!", Toast.LENGTH_SHORT).show()
} else {
editor.remove("user")
editor.remove("psw")
editor.commit()
Toast.makeText(getApplicationContext(), "未记住密码!", Toast.LENGTH_SHORT).show()
}
}
}