用Android studio java语言实现一个简洁的登陆注册界面的类似功能,登录的信息用文本记录,登录页面填写手机号、密码,左下角是一个注册按钮,右下角是登录按钮,点击登录后显示已成功登录即可;点击注册按钮则跳转到注册页面,注册页面可填写姓名、手机号、密码,底下为注册按钮,填写完资料后点击注册即可完成注册,可进行登录操作。
时间: 2023-06-14 20:04:17 浏览: 84
好的,下面是一个简单的登陆注册界面实现的示例代码(仅供参考):
MainActivity.java
```
public class MainActivity extends AppCompatActivity {
private EditText mPhoneEditText;
private EditText mPasswordEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPhoneEditText = findViewById(R.id.phone_edit_text);
mPasswordEditText = findViewById(R.id.password_edit_text);
Button loginButton = findViewById(R.id.login_button);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String phone = mPhoneEditText.getText().toString();
String password = mPasswordEditText.getText().toString();
// 判断手机号和密码是否为空
if (TextUtils.isEmpty(phone) || TextUtils.isEmpty(password)) {
Toast.makeText(MainActivity.this, "手机号或密码不能为空", Toast.LENGTH_SHORT).show();
return;
}
// 判断手机号和密码是否匹配
if (checkLoginInfo(phone, password)) {
Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "手机号或密码错误", Toast.LENGTH_SHORT).show();
}
}
});
Button registerButton = findViewById(R.id.register_button);
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
startActivity(intent);
}
});
}
// 检查手机号和密码是否匹配
private boolean checkLoginInfo(String phone, String password) {
String info = readFile("login.txt");
if (TextUtils.isEmpty(info)) {
return false;
}
String[] infos = info.split(",");
if (infos.length != 2) {
return false;
}
String savedPhone = infos[0];
String savedPassword = infos[1];
return phone.equals(savedPhone) && password.equals(savedPassword);
}
// 读取文件
private String readFile(String fileName) {
try {
FileInputStream fis = openFileInput(fileName);
byte[] buffer = new byte[1024];
int len = -1;
StringBuilder sb = new StringBuilder();
while ((len = fis.read(buffer)) != -1) {
sb.append(new String(buffer, 0, len));
}
fis.close();
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
}
```
activity_main.xml
```
<?xml version="1.0" encoding="utf-8"?>
<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/phone_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="手机号"/>
<EditText
android:id="@+id/password_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:inputType="textPassword"/>
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"/>
<Button
android:id="@+id/register_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册"
android:layout_marginTop="16dp"/>
</LinearLayout>
```
RegisterActivity.java
```
public class RegisterActivity extends AppCompatActivity {
private EditText mNameEditText;
private EditText mPhoneEditText;
private EditText mPasswordEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mNameEditText = findViewById(R.id.name_edit_text);
mPhoneEditText = findViewById(R.id.phone_edit_text);
mPasswordEditText = findViewById(R.id.password_edit_text);
Button registerButton = findViewById(R.id.register_button);
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = mNameEditText.getText().toString();
String phone = mPhoneEditText.getText().toString();
String password = mPasswordEditText.getText().toString();
// 判断姓名、手机号和密码是否为空
if (TextUtils.isEmpty(name) || TextUtils.isEmpty(phone) || TextUtils.isEmpty(password)) {
Toast.makeText(RegisterActivity.this, "姓名、手机号或密码不能为空", Toast.LENGTH_SHORT).show();
return;
}
// 保存注册信息
if (saveRegisterInfo(name, phone, password)) {
Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
finish();
} else {
Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
}
}
});
}
// 保存注册信息
private boolean saveRegisterInfo(String name, String phone, String password) {
try {
FileOutputStream fos = openFileOutput("login.txt", MODE_PRIVATE);
String info = phone + "," + password;
fos.write(info.getBytes());
fos.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
```
activity_register.xml
```
<?xml version="1.0" encoding="utf-8"?>
<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/name_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="姓名"/>
<EditText
android:id="@+id/phone_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="手机号"/>
<EditText
android:id="@+id/password_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:inputType="textPassword"/>
<Button
android:id="@+id/register_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册"
android:layout_marginTop="16dp"/>
</LinearLayout>
```
这个示例代码只是实现了最基本的功能,还有很多需要优化和改进的地方,比如密码加密、手机号格式验证、登录状态保持等等。