安卓studio编写登录注册功能
时间: 2023-12-08 20:04:39 浏览: 74
实现登录注册功能需要用到安卓开发中的布局、控件、数据库和网络通信等知识。以下是一个简单的登录注册示例代码,供参考:
1. 布局文件activity_main.xml
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:inputType="textPassword" />
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录" />
<TextView
android:id="@+id/register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册账号"
android:gravity="center"
android:textColor="@color/colorAccent" />
</LinearLayout>
```
2. 注册页面布局文件activity_register.xml
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".RegisterActivity">
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:inputType="textPassword" />
<Button
android:id="@+id/register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册" />
</LinearLayout>
```
3. MainActivity.java
```
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText usernameEditText, passwordEditText;
private Button loginButton;
private TextView registerTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usernameEditText = findViewById(R.id.username);
passwordEditText = findViewById(R.id.password);
loginButton = findViewById(R.id.login);
registerTextView = findViewById(R.id.register);
loginButton.setOnClickListener(this);
registerTextView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.login:
String username = usernameEditText.getText().toString().trim();
String password = passwordEditText.getText().toString().trim();
if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
} else {
// 调用登录接口
// TODO
}
break;
case R.id.register:
Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
startActivity(intent);
break;
default:
break;
}
}
}
```
4. RegisterActivity.java
```
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
private EditText usernameEditText, passwordEditText;
private Button registerButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
usernameEditText = findViewById(R.id.username);
passwordEditText = findViewById(R.id.password);
registerButton = findViewById(R.id.register);
registerButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.register:
String username = usernameEditText.getText().toString().trim();
String password = passwordEditText.getText().toString().trim();
if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
} else {
// 调用注册接口
// TODO
}
break;
default:
break;
}
}
}
```
5. 网络通信部分(使用OkHttp库)
```
public class HttpUtil {
private static final String TAG = "HttpUtil";
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
public static void sendOkHttpRequest(final String address, final String json, final Callback callback) {
OkHttpClient client = new OkHttpClient();
RequestBody requestBody = RequestBody.create(JSON, json);
final Request request = new Request.Builder().url(address).post(requestBody).build();
client.newCall(request).enqueue(callback);
}
}
```
6. 数据库部分(使用LitePal库)
先在build.gradle中添加以下依赖:
```
dependencies {
implementation 'org.litepal.android:core:3.0.0'
}
```
创建User实体类:
```
public class User extends LitePalSupport {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
```
7. 登录接口示例代码
```
private void login(String username, String password) {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("username", username);
jsonObject.put("password", password);
} catch (JSONException e) {
e.printStackTrace();
}
String json = jsonObject.toString();
HttpUtil.sendOkHttpRequest("http://www.example.com/login", json, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.d(TAG, "onFailure: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "登录失败,请检查网络连接", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String responseData = response.body().string();
Log.d(TAG, "onResponse: " + responseData);
try {
JSONObject jsonObject = new JSONObject(responseData);
int code = jsonObject.getInt("code");
if (code == 1) {
// 登录成功,保存用户信息
User user = new User();
user.setUsername(username);
user.setPassword(password);
user.save();
Intent intent = new Intent(MainActivity.this, HomeActivity.class);
startActivity(intent);
finish();
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
```
8. 注册接口示例代码
```
private void register(String username, String password) {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("username", username);
jsonObject.put("password", password);
} catch (JSONException e) {
e.printStackTrace();
}
String json = jsonObject.toString();
HttpUtil.sendOkHttpRequest("http://www.example.com/register", json, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.d(TAG, "onFailure: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(RegisterActivity.this, "注册失败,请检查网络连接", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String responseData = response.body().string();
Log.d(TAG, "onResponse: " + responseData);
try {
JSONObject jsonObject = new JSONObject(responseData);
int code = jsonObject.getInt("code");
if (code == 1) {
// 注册成功,保存用户信息
User user = new User();
user.setUsername(username);
user.setPassword(password);
user.save();
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(intent);
finish();
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(RegisterActivity.this, "注册失败,请重新尝试", Toast.LENGTH_SHORT).show();
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
```
注意:以上代码仅供参考,具体实现方式可能因项目需求和开发细节而有所不同,还需根据具体情况进行调整和完善。
阅读全文