安卓登录注册页面及功能代码
时间: 2024-03-02 08:51:33 浏览: 25
这里为你提供一个简单的安卓登录注册页面及功能代码示例,仅供参考:
1. 创建两个布局文件 login_layout.xml 和 register_layout.xml,分别对应登录和注册页面。
login_layout.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/username_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名" />
<EditText
android:id="@+id/password_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="密码" />
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录" />
<TextView
android:id="@+id/register_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="还没有账号?点击注册" />
</LinearLayout>
```
register_layout.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/username_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名" />
<EditText
android:id="@+id/password_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="密码" />
<Button
android:id="@+id/register_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册" />
<TextView
android:id="@+id/login_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="已有账号?点击登录" />
</LinearLayout>
```
2. 在相应的 Activity 中设置布局和监听器,实现登录和注册功能。
LoginActivity.java:
```
public class LoginActivity extends AppCompatActivity {
private EditText usernameEditText;
private EditText passwordEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_layout);
usernameEditText = findViewById(R.id.username_edittext);
passwordEditText = findViewById(R.id.password_edittext);
Button loginButton = findViewById(R.id.login_button);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();
if (login(username, password)) {
// 登录成功,跳转到主页面
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
} else {
// 登录失败,弹出提示框
Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
}
}
});
TextView registerTextView = findViewById(R.id.register_textview);
registerTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 跳转到注册页面
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
}
});
}
// 用户登录函数
public boolean login(String username, String password) {
// 查询数据库中是否存在该用户
Cursor cursor = database.query("users", null, "username=? and password=?", new String[]{username, password}, null, null, null);
if (cursor.moveToFirst()) {
// 登录成功,保存用户信息并返回true
currentUser = new User(cursor.getInt(cursor.getColumnIndex("id")), cursor.getString(cursor.getColumnIndex("username")), cursor.getString(cursor.getColumnIndex("password")));
return true;
}
// 登录失败,返回false
return false;
}
}
```
RegisterActivity.java:
```
public class RegisterActivity extends AppCompatActivity {
private EditText usernameEditText;
private EditText passwordEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register_layout);
usernameEditText = findViewById(R.id.username_edittext);
passwordEditText = findViewById(R.id.password_edittext);
Button registerButton = findViewById(R.id.register_button);
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();
if (register(username, password)) {
// 注册成功,跳转到登录页面
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(intent);
finish();
} else {
// 注册失败,弹出提示框
Toast.makeText(RegisterActivity.this, "用户名已存在", Toast.LENGTH_SHORT).show();
}
}
});
TextView loginTextView = findViewById(R.id.login_textview);
loginTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 跳转到登录页面
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(intent);
}
});
}
// 用户注册函数
public boolean register(String username, String password) {
// 首先查询数据库中是否已经存在该用户
Cursor cursor = database.query("users", null, "username=?", new String[]{username}, null, null, null);
if (cursor.getCount() > 0) {
// 用户已经存在,返回false
return false;
}
// 将新用户信息写入数据库
ContentValues values = new ContentValues();
values.put("username", username);
values.put("password", password);
database.insert("users", null, values);
// 注册成功,返回true
return true;
}
}
```
以上代码示例中,使用 SQLite 数据库存储用户信息。在 LoginActivity 中设置登录按钮的点击监听器,获取用户名和密码并调用 login() 函数进行登录操作。如果登录成功,则跳转到 MainActivity,否则弹出提示框。在 TextView 上设置点击监听器,跳转到 RegisterActivity。在 RegisterActivity 中设置注册按钮的点击监听器,获取用户名和密码并调用 register() 函数进行注册操作。如果注册成功,则跳转到 LoginActivity,否则弹出提示框。在 TextView 上设置点击监听器,跳转到 LoginActivity。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)