功能:實現賬登錄、檢查賬號密碼、記住密碼、清除記錄功能。
設備:電腦一臺、Android Studio軟件一個。
技術:xml佈局、SharedPreferences的使用。
開始:
1.看效果:
2.文件目錄:
3.activity_main.xml 佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:src="@drawable/head" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@color/colorWhite"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用戶名:"
android:textColor="@color/colorBlack"
android:textSize="12sp" />
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@color/colorWhite"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 碼:"
android:textColor="@color/colorBlack"
android:textSize="12sp" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
</LinearLayout>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/bt_memory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="remember"
android:paddingRight="100dp"
android:text="記住密碼"
android:textSize="10sp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clcInfo"
android:text="清除記錄"
android:textSize="10sp" />
</RadioGroup>
<Button
android:id="@+id/bt_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@color/colorAccent"
android:text="登錄"
android:textColor="@color/colorWhite" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="賬號註冊"
android:textSize="10sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="110dp"
android:text="忘記密碼?"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
4.MainActivity文件
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText etName;
private EditText etPassword;
private Button login;
private boolean isRemember;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
Map<String, String> userInfo = MyService.getUserInfo(this);
if (userInfo != null) {
String name = userInfo.get("name");
String pwd = userInfo.get("pwd");
etName.setText(name);
etPassword.setText(pwd);
}
}
private void initView() {
etName = (EditText) findViewById(R.id.et_name);
etPassword = (EditText) findViewById(R.id.et_password);
login = (Button) findViewById(R.id.bt_login);
login.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String name = etName.getText().toString().trim();
String pwd = etPassword.getText().toString().trim();
if (TextUtils.isEmpty(name)) {
Toast.makeText(this, "請輸入用戶名", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(pwd)) {
Toast.makeText(this, "請輸入密碼", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(this, "登錄成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this,SecondActivity.class);
startActivity(intent);
if (isRemember) {
boolean isSaveSuccess = MyService.saveUserInfo(this, name, pwd);
if (isSaveSuccess) {
Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "保存失敗", Toast.LENGTH_SHORT).show();
}
}
}
//記住密碼
public void remember(View view) {
isRemember = true;
}
//清除記錄
public void clcInfo(View view) {
boolean isClean = MyService.clearUserInfo(this);
if (isClean) {
Toast.makeText(this, "清除成功", Toast.LENGTH_SHORT).show();
etName.setText("");
etPassword.setText("");
} else {
Toast.makeText(this, "清除失敗", Toast.LENGTH_SHORT).show();
}
}
}
5.MyService文件(保存賬號密碼)
import android.content.Context;
import android.content.SharedPreferences;
import java.util.HashMap;
import java.util.Map;
public class MyService {
//保存QQ賬號和密碼到data.xml文件
public static boolean saveUserInfo(Context context, String name, String password) {
SharedPreferences info = context.getSharedPreferences("data", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = info.edit(); //獲取編輯器
editor.putString("userName", name); //存入用戶名
editor.putString("userPwd", password);
editor.commit(); //提交修改
return true;
}
//從data.xml文件中取出賬號與密碼
public static Map<String, String> getUserInfo(Context context) {
SharedPreferences info = context.getSharedPreferences("data", Context.MODE_PRIVATE);
String name = info.getString("userName", null);
String pwd = info.getString("userPwd", null);
Map<String, String> userMap = new HashMap<String, String>();
userMap.put("name", name);
userMap.put("pwd", pwd);
return userMap;
}
//清除記錄
public static boolean clearUserInfo(Context context) {
SharedPreferences info = context.getSharedPreferences("data", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = info.edit(); //獲取編輯器
editor.clear();
editor.commit(); //提交修改
return true;
}
}
6.跳轉的頁面根據自己的需求寫!
附: