android账号填写代码,Android制作登录页面并且记住账号密码功能的实现代码
时间: 2024-02-09 16:11:37 浏览: 26
以下是一个简单的 Android 登录页面的示例,并包含记住账号密码功能的实现代码:
1. activity_login.xml
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/username_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username" />
<EditText
android:id="@+id/password_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
<CheckBox
android:id="@+id/remember_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remember Me" />
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Log In" />
</LinearLayout>
```
2. LoginActivity.java
```
public class LoginActivity extends AppCompatActivity {
private EditText mUsernameEditText;
private EditText mPasswordEditText;
private CheckBox mRememberCheckbox;
private SharedPreferences mSharedPreferences;
private SharedPreferences.Editor mEditor;
private static final String PREFS_NAME = "MyPrefsFile";
private static final String KEY_USERNAME = "username";
private static final String KEY_PASSWORD = "password";
private static final String KEY_REMEMBER = "remember";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mUsernameEditText = findViewById(R.id.username_edittext);
mPasswordEditText = findViewById(R.id.password_edittext);
mRememberCheckbox = findViewById(R.id.remember_checkbox);
mSharedPreferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
mEditor = mSharedPreferences.edit();
boolean rememberMe = mSharedPreferences.getBoolean(KEY_REMEMBER, false);
mRememberCheckbox.setChecked(rememberMe);
if (rememberMe) {
String username = mSharedPreferences.getString(KEY_USERNAME, "");
String password = mSharedPreferences.getString(KEY_PASSWORD, "");
mUsernameEditText.setText(username);
mPasswordEditText.setText(password);
}
Button loginButton = findViewById(R.id.login_button);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = mUsernameEditText.getText().toString().trim();
String password = mPasswordEditText.getText().toString().trim();
if (username.isEmpty() || password.isEmpty()) {
Toast.makeText(LoginActivity.this, "Please enter username and password", Toast.LENGTH_SHORT).show();
} else {
if (mRememberCheckbox.isChecked()) {
mEditor.putString(KEY_USERNAME, username);
mEditor.putString(KEY_PASSWORD, password);
mEditor.putBoolean(KEY_REMEMBER, true);
mEditor.apply();
} else {
mEditor.clear();
mEditor.apply();
}
// Do login logic here
}
}
});
}
}
```
在上面的代码中,我们使用了 Android 的 SharedPreferences 来存储记住的账号密码信息。如果用户勾选了“记住我”的复选框,则将账号密码信息存储到 SharedPreferences 中,并在下次打开应用时自动填充。如果用户没有勾选“记住我”的复选框,则清除 SharedPreferences 中的账号密码信息。