android - 用按钮链接片段
问题描述
有人可以帮帮我吗?我只想在一个活动中添加 2 个按钮,每个按钮都转到一个不同的片段,例如: 按钮 1 转到 Login_Fragment
按钮 2 转到 SignUp_Fragment 这些片段有自己的 xml 文件,下面的代码是 FirstPage,它的 xml 是 first_page 所以下面的代码将用片段 xml 文件替换 first_page xml 我该如何防止呢?
package com.example.logindesign;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
public class First_Page extends AppCompatActivity {
Button createAccount, login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_page);
createAccount = findViewById(R.id.createAccount);
createAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fm = getSupportFragmentManager();
SignUp_Fragment f2 = new SignUp_Fragment();
fm.beginTransaction().replace(R.id.mParent, f2).commit();
}
});
login = findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fm = getSupportFragmentManager();
Login_Fragment f2 = new Login_Fragment();
fm.beginTransaction().add(R.id.mParent, f2).commit();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/Blue"
android:id="@+id/mParent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/stay_connected_with_friends"
android:textSize="28sp"
android:textColor="@color/white"
android:fontFamily="@font/nunito_bold"
android:layout_marginTop="230dp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="200dp"
>
</TextView>
<Button
android:id="@+id/createAccount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@color/Blue"
android:fontFamily="@font/nunito_bold"
android:text="@string/sign_up"
android:textColor="@color/white"
android:textSize="25sp">
</Button>
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="200dp"
android:layout_marginBottom="172dp"
android:background="@color/Blue"
android:fontFamily="@font/nunito_bold"
android:text="@string/login"
android:textColor="@color/white"
android:textSize="25sp">
</Button>
<ImageView
android:id="@+id/imageView"
android:layout_width="175dp"
android:layout_height="141dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="114dp"
android:layout_marginBottom="136dp"
android:contentDescription="@string/logo"
app:srcCompat="@drawable/logo" />
</RelativeLayout>
解决方案
使用这样的代码,点击按钮A,
Fragment A = new FragmentA();
点击按钮 B,
Fragment B = new FragmentB();
并为两个加载片段创建一个函数,
public void loadFragment(Fragment f)
{
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.main_contenier,f).commit();
}
推荐阅读
- python - 如何从表格中的范围计算非均匀值百分比?
- ruby - 猴子补丁方法只在我自己的代码中(通过自动使用改进?)
- android - 如何在不支持Androidx的旧项目中更新opentok最新版本?有可能吗?
- xamarin - 警告:尝试显示不在窗口层次结构中的视图?
- reactjs - CKEditor5 - 使用反应在编辑器的光标位置附加内容
- ftp - sftp批处理文件无法执行
- php - 通过多种语言访问 Session-Var
- javascript - 使用 JavaScript/jQuery 在当前图像之上显示下一个图像
- c# - 如何将智能卡与我的 C# 应用程序集成?
- hyperledger-fabric - Hyperledger Fabric:无法使用 Node SDK 调用