首页 > 解决方案 > 用按钮链接片段

问题描述

有人可以帮帮我吗?我只想在一个活动中添加 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>

标签: android

解决方案


使用这样的代码,点击按钮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();
 }

推荐阅读