Android简单登录注册功能
时间: 2023-08-13 11:03:54 浏览: 200
实现一个简单的Android登录注册功能,可以分为以下几个步骤:
1. 创建两个Activity,一个用于登录,一个用于注册。
2. 在布局文件中添加EditText控件,用于输入用户名和密码。
3. 在布局文件中添加Button控件,用于触发登录和注册事件。
4. 在Java代码中,使用SharedPreferences存储用户名和密码信息。在注册时,将新的用户名和密码存储到SharedPreferences中,在登录时,从SharedPreferences中获取保存的用户名和密码,与用户输入的信息进行比较,判断是否登录成功。
5. 在登录成功后,跳转到主界面。
下面是一个简单的代码示例:
登录界面布局:
```xml
<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/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:inputType="textPassword" />
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录" />
<TextView
android:id="@+id/tv_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="没有账号?点击注册"
android:textColor="@android:color/holo_blue_dark" />
</LinearLayout>
```
登录界面代码:
```java
public class LoginActivity extends AppCompatActivity {
private EditText etUsername;
private EditText etPassword;
private Button btnLogin;
private TextView tvRegister;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
etUsername = findViewById(R.id.et_username);
etPassword = findViewById(R.id.et_password);
btnLogin = findViewById(R.id.btn_login);
tvRegister = findViewById(R.id.tv_register);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
String savedUsername = sp.getString("username", "");
String savedPassword = sp.getString("password", "");
if (username.equals(savedUsername) && password.equals(savedPassword)) {
Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
} else {
Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
}
}
});
tvRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
}
});
}
}
```
注册界面布局:
```xml
<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/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:inputType="textPassword" />
<EditText
android:id="@+id/et_confirm_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="确认密码"
android:inputType="textPassword" />
<Button
android:id="@+id/btn_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册" />
<TextView
android:id="@+id/tv_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="已有账号?点击登录"
android:textColor="@android:color/holo_blue_dark" />
</LinearLayout>
```
注册界面代码:
```java
public class RegisterActivity extends AppCompatActivity {
private EditText etUsername;
private EditText etPassword;
private EditText etConfirmPassword;
private Button btnRegister;
private TextView tvLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
etUsername = findViewById(R.id.et_username);
etPassword = findViewById(R.id.et_password);
etConfirmPassword = findViewById(R.id.et_confirm_password);
btnRegister = findViewById(R.id.btn_register);
tvLogin = findViewById(R.id.tv_login);
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
String confirmPassword = etConfirmPassword.getText().toString();
if (password.equals(confirmPassword)) {
SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("username", username);
editor.putString("password", password);
editor.apply();
Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
finish();
} else {
Toast.makeText(RegisterActivity.this, "密码不一致", Toast.LENGTH_SHORT).show();
}
}
});
tvLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
});
}
}
```
以上就是一个简单的Android登录注册功能的实现。