android studio写一个登陆注册界面连接数据以及具体代码
时间: 2024-05-10 22:19:14 浏览: 24
这里给出一个简单的Android Studio登陆注册界面的代码示例,包含连接数据库的实现:
1. 在 app/build.gradle 文件中添加以下依赖:
```
dependencies {
implementation 'com.android.volley:volley:1.2.0'
implementation 'com.google.code.gson:gson:2.8.6'
}
```
2. 在 activity_login.xml 中设计登陆界面的布局:
```
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<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:text="注册账号"/>
</LinearLayout>
```
3. 在 LoginActivity.java 中实现登陆逻辑和数据库连接:
```
public class LoginActivity extends AppCompatActivity {
private EditText etUsername, 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) {
login();
}
});
// 点击注册账号
tvRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
}
});
}
// 登陆方法
private void login() {
String username = etUsername.getText().toString().trim();
String password = etPassword.getText().toString().trim();
// 校验用户名和密码是否为空
if (TextUtils.isEmpty(username)) {
Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
return;
}
// 发起登陆请求
String url = "http://your.api.url/login";
Map<String, String> params = new HashMap<>();
params.put("username", username);
params.put("password", password);
JsonObjectRequest request = new JsonObjectRequest(
Request.Method.POST,
url,
new JSONObject(params),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
int code = response.getInt("code");
String message = response.getString("message");
if (code == 200) {
// 登陆成功
// TODO: 保存用户信息并跳转到主界面
} else {
// 登陆失败
Toast.makeText(LoginActivity.this, message, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Toast.makeText(LoginActivity.this, "网络错误,请稍后重试", Toast.LENGTH_SHORT).show();
}
});
Volley.newRequestQueue(this).add(request);
}
}
```
4. 如果需要实现注册功能,可以在 RegisterActivity.java 中添加注册逻辑,与登陆逻辑类似。具体代码示例略。