Android stdio 用代码实现,用户注册账号密码登陆。其中布局文件已经给出有两个EditText和两个Button和一个CheckBox组成。要求注册账号密码时,如果当前账号密码没有注册过则可以注册成功,否则注册失败。 登陆账号密码时会和注册的账号密码比对,如果相同则登陆成功,否则登陆失败。 CheckBox如果被赋予true,则下次打开软件时自动填充账号密码。 最终将代码以一个xml和一个Activity的格式给出
时间: 2024-02-11 08:09:40 浏览: 65
Android Studio实现一个简约记账APP,代码附详细注释
5星 · 资源好评率100%
下面是实现该功能的代码:
activity_main.xml:
```
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/activity_horizontal_margin"
tools:context=".MainActivity">
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/username"/>
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/et_username"
android:hint="@string/password"
android:inputType="textPassword"/>
<Button
android:id="@+id/btn_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/et_password"
android:layout_alignParentStart="true"
android:text="@string/register"/>
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/et_password"
android:layout_alignParentEnd="true"
android:text="@string/login"/>
<CheckBox
android:id="@+id/cb_remember"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btn_register"
android:text="@string/remember"/>
</RelativeLayout>
```
MainActivity.java:
```
import android.content.Context;
import android.content.SharedPreferences;
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;
public class MainActivity extends AppCompatActivity {
private EditText et_username;
private EditText et_password;
private Button btn_register;
private Button btn_login;
private CheckBox cb_remember;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_username = (EditText) findViewById(R.id.et_username);
et_password = (EditText) findViewById(R.id.et_password);
btn_register = (Button) findViewById(R.id.btn_register);
btn_login = (Button) findViewById(R.id.btn_login);
cb_remember = (CheckBox) findViewById(R.id.cb_remember);
sp = getSharedPreferences("user", Context.MODE_PRIVATE);
btn_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String username = et_username.getText().toString();
String password = et_password.getText().toString();
if (username.isEmpty() || password.isEmpty()) {
Toast.makeText(MainActivity.this, R.string.empty_input, Toast.LENGTH_SHORT).show();
} else {
if (sp.contains(username)) {
Toast.makeText(MainActivity.this, R.string.username_exist, Toast.LENGTH_SHORT).show();
} else {
sp.edit().putString(username, password).apply();
Toast.makeText(MainActivity.this, R.string.register_success, Toast.LENGTH_SHORT).show();
}
}
}
});
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String username = et_username.getText().toString();
String password = et_password.getText().toString();
if (username.isEmpty() || password.isEmpty()) {
Toast.makeText(MainActivity.this, R.string.empty_input, Toast.LENGTH_SHORT).show();
} else {
if (sp.contains(username)) {
if (password.equals(sp.getString(username, ""))) {
Toast.makeText(MainActivity.this, R.string.login_success, Toast.LENGTH_SHORT).show();
if (cb_remember.isChecked()) {
sp.edit().putString("last_user", username).apply();
}
} else {
Toast.makeText(MainActivity.this, R.string.password_error, Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(MainActivity.this, R.string.username_not_exist, Toast.LENGTH_SHORT).show();
}
}
}
});
String lastUser = sp.getString("last_user", "");
if (!lastUser.isEmpty()) {
et_username.setText(lastUser);
et_password.setText(sp.getString(lastUser, ""));
cb_remember.setChecked(true);
}
}
}
```
strings.xml:
```
<resources>
<string name="app_name">UserLogin</string>
<string name="username">Username</string>
<string name="password">Password</string>
<string name="register">Register</string>
<string name="login">Login</string>
<string name="remember">Remember me</string>
<string name="empty_input">Username or password cannot be empty</string>
<string name="username_exist">Username already exists</string>
<string name="register_success">Register successful</string>
<string name="login_success">Login successful</string>
<string name="password_error">Password incorrect</string>
<string name="username_not_exist">Username not exist</string>
</resources>
```
阅读全文