帮我写Android studio 登录界面代码,可以记住密码的那种
时间: 2024-05-03 14:22:59 浏览: 132
以下是一个简单的Android Studio登录界面代码,可以记住密码:
1. 创建一个新的Android Studio项目,然后在布局文件中添加以下代码:
```xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<EditText
android:id="@+id/usernameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username" />
<EditText
android:id="@+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="Password" />
<CheckBox
android:id="@+id/rememberMeCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remember me" />
<Button
android:id="@+id/loginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login" />
</LinearLayout>
```
2. 在Java文件中添加以下代码:
```java
public class LoginActivity extends AppCompatActivity {
private EditText usernameEditText, passwordEditText;
private CheckBox rememberMeCheckBox;
private Button loginButton;
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
private static final String SHARED_PREFS = "sharedPrefs";
private static final String USERNAME = "username";
private static final String PASSWORD = "password";
private static final String REMEMBER_ME = "rememberMe";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
usernameEditText = findViewById(R.id.usernameEditText);
passwordEditText = findViewById(R.id.passwordEditText);
rememberMeCheckBox = findViewById(R.id.rememberMeCheckBox);
loginButton = findViewById(R.id.loginButton);
sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
editor = sharedPreferences.edit();
// 如果已经保存了用户名和密码,则自动填写
if (sharedPreferences.getBoolean(REMEMBER_ME, false)) {
usernameEditText.setText(sharedPreferences.getString(USERNAME, ""));
passwordEditText.setText(sharedPreferences.getString(PASSWORD, ""));
rememberMeCheckBox.setChecked(true);
}
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = usernameEditText.getText().toString().trim();
String password = passwordEditText.getText().toString().trim();
// 检查用户名和密码是否为空
if (TextUtils.isEmpty(username)) {
Toast.makeText(LoginActivity.this, "Please enter username", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(LoginActivity.this, "Please enter password", Toast.LENGTH_SHORT).show();
return;
}
// 如果勾选了“记住我”,则保存用户名和密码
if (rememberMeCheckBox.isChecked()) {
editor.putString(USERNAME, username);
editor.putString(PASSWORD, password);
editor.putBoolean(REMEMBER_ME, true);
editor.apply();
} else {
editor.clear();
editor.apply();
}
// 登录逻辑
// ...
}
});
}
}
```
这个简单的登录界面具备记住密码的功能,如果用户勾选了“记住我”复选框,则会保存用户名和密码,并在下次打开应用时自动填写。如果用户未勾选,则会清除保存的用户名和密码。您可以根据自己的需求进行修改和优化。
阅读全文