Fragment实现一个APP的登录注册功能代码
时间: 2024-05-14 14:19:04 浏览: 11
以下是一个基本的登录注册功能的代码示例:
1. 创建两个Fragment:LoginFragment和RegisterFragment。
```java
public class LoginFragment extends Fragment {
// 声明控件
private EditText etUsername;
private EditText etPassword;
private Button btnLogin;
private TextView tvRegister;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_login, container, false);
// 初始化控件
etUsername = view.findViewById(R.id.et_username);
etPassword = view.findViewById(R.id.et_password);
btnLogin = view.findViewById(R.id.btn_login);
tvRegister = view.findViewById(R.id.tv_register);
// 设置点击事件
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 处理登录逻辑
}
});
tvRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 跳转到注册Fragment
RegisterFragment registerFragment = new RegisterFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.container, registerFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
return view;
}
}
public class RegisterFragment extends Fragment {
// 声明控件
private EditText etUsername;
private EditText etPassword;
private Button btnRegister;
private TextView tvLogin;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_register, container, false);
// 初始化控件
etUsername = view.findViewById(R.id.et_username);
etPassword = view.findViewById(R.id.et_password);
btnRegister = view.findViewById(R.id.btn_register);
tvLogin = view.findViewById(R.id.tv_login);
// 设置点击事件
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 处理注册逻辑
}
});
tvLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 跳转到登录Fragment
LoginFragment loginFragment = new LoginFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.container, loginFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
return view;
}
}
```
2. 在Activity中加载Fragment。
```java
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// 加载登录Fragment
LoginFragment loginFragment = new LoginFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.container, loginFragment);
transaction.commit();
}
}
```
3. 编写布局文件。
activity_login.xml:
```xml
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
fragment_login.xml:
```xml
<LinearLayout
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="Username" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login" />
<TextView
android:id="@+id/tv_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register" />
</LinearLayout>
```
fragment_register.xml:
```xml
<LinearLayout
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="Username" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
<Button
android:id="@+id/btn_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register" />
<TextView
android:id="@+id/tv_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login" />
</LinearLayout>
```