给个android studio 登录和注册的代码
时间: 2024-01-21 15:05:14 浏览: 33
以下是一个简单的Android Studio登录和注册的代码示例:
1. 登录界面布局文件 `activity_login.xml`:
```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" />
<CheckBox
android:id="@+id/cb_remember"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码" />
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录" />
</LinearLayout>
```
2. 注册界面布局文件 `activity_register.xml`:
```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="注册" />
</LinearLayout>
```
3. 登录界面对应的Activity `LoginActivity.java`:
```java
public class LoginActivity extends AppCompatActivity {
private EditText etUsername;
private EditText etPassword;
private CheckBox cbRemember;
@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);
cbRemember = findViewById(R.id.cb_remember);
Button btnLogin = findViewById(R.id.btn_login);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 获取用户名和密码
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
// 判断用户名和密码是否正确
if (username.equals("admin") && password.equals("123456")) {
// 如果记住密码,保存用户名和密码到SharedPreferences
if (cbRemember.isChecked()) {
SharedPreferences sp = getSharedPreferences("login", Context.MODE_PRIVATE);
sp.edit().putString("username", username).putString("password", password).apply();
}
// 跳转到主界面
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
} else {
// 弹出提示框,提示用户名或密码错误
Toast.makeText(LoginActivity.this, "用户名或密码错误,请重新输入", Toast.LENGTH_SHORT).show();
}
}
});
}
}
```
4. 注册界面对应的Activity `RegisterActivity.java`:
```java
public class RegisterActivity extends AppCompatActivity {
private EditText etUsername;
private EditText etPassword;
private EditText etConfirmPassword;
@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);
Button btnRegister = findViewById(R.id.btn_register);
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)) {
// 弹出提示框,提示密码和确认密码不一致
Toast.makeText(RegisterActivity.this, "密码和确认密码不一致,请重新输入", Toast.LENGTH_SHORT).show();
return;
}
// 保存用户名和密码到SharedPreferences
SharedPreferences sp = getSharedPreferences("register", Context.MODE_PRIVATE);
sp.edit().putString("username", username).putString("password", password).apply();
// 弹出提示框,提示注册成功
Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
// 跳转到登录界面
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
});
}
}
```
以上代码演示了如何完成一个简单的登录和注册功能,并使用SharedPreferences来保存和读取用户信息。需要注意的是,这只是一个示例代码,实际的登录和注册功能需要根据具体需求进行完善和优化。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)