繁体   English   中英

单击按钮后,我的应用程序上的片段未出现

[英]Fragments on my app are not appearing after button click

我是 android 开发游戏的新手,我研究并阅读了大量关于片段的信息,但我似乎遇到了一些障碍。 在创建所有内容并检查以确保我的错误为零之后,我的应用程序没有按照我想要的方式工作。 单击按钮时,我希望片段显示在屏幕上。 我有两个按钮,所以有两个片段,但是我的按钮似乎不起作用。

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="35dp">

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

    <Button
        android:id="@+id/signin_button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:keepScreenOn="true"
        android:gravity="center"
        android:text="Sign In"
        android:textColor="#000000"
        android:textSize="18sp"
        android:textStyle="bold" />

    </RelativeLayout>
    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

    <Button
        android:id="@+id/signup_button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:keepScreenOn="true"
        android:gravity="center"
        android:text="Sign Up"
        android:textColor="#000000"
        android:textSize="18sp"
        android:textStyle="bold" />

    </RelativeLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:id="@+id/parent_layout"
        >

        <FrameLayout
        android:id="@+id/signin_fragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" />


    </LinearLayout>
    </LinearLayout>

继承人Java代码:

package com.example.projectconviencesplash;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class SigninMainActivity extends Fragment implements View.OnClickListener {

    public SigninMainActivity() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.signinfragment, container, false);

        Button signinBtn = (Button) rootView.findViewById(R.id.signin_button);
        Button signupBtn = (Button) rootView.findViewById(R.id.signup_button);

        signinBtn.setOnClickListener(this);
        signupBtn.setOnClickListener(this);


        return rootView;
    }

    @Override
    public void onClick(View view) {
        Fragment fragment = null;
        switch (view.getId()) {
            case R.id.signin_button:
                fragment = new SigninTwoActivity();
                replaceFragment(fragment);
                break;

            case R.id.signup_button:
                fragment = new SignupTwoActivity();
                replaceFragment(fragment);
                break;
        }
    }

    public void replaceFragment(Fragment someFragment) {
        assert getFragmentManager() != null;
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.signin_fragment, someFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }


}

这是片段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:layout_marginStart="10dp"
        android:layout_marginEnd="10dp">

        <Button
            android:id="@+id/dummy_button"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="0dp"
            android:layout_weight="0"
            android:background="@drawable/fb_button"
            android:drawableStart="@drawable/fb"
            android:drawablePadding="-70dp"
            android:paddingStart="70dp"
            android:text="@string/fb_Title"
            android:textColor="#FFFFFF"
            android:textSize="18sp" />

        <Button
            android:id="@+id/dummy_button"
            style="@style/Widget.AppCompat.Button.Borderless"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="0dp"
            android:layout_weight="0"
            android:background="@drawable/g_button"
            android:drawableStart="@drawable/g"
            android:drawablePadding="-80dp"
            android:paddingStart="80dp"
            android:text="@string/g_Title"
            android:textColor="#FFFFFF"
            android:textSize="18sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="70dp">

            <ImageView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:layout_marginStart="0dp"
                android:layout_marginLeft="0dp"
                android:layout_marginEnd="0dp"
                android:layout_marginRight="0dp"
                android:src="@drawable/strike" />

            <TextView
                android:layout_width="149dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:layout_marginStart="0dp"
                android:layout_marginLeft="0dp"
                android:layout_marginEnd="0dp"
                android:layout_marginRight="0dp"
                android:text="@string/email_Title"
                android:textColor="#cccccc"
                android:textSize="16sp"
                android:textStyle="" />

            <ImageView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:layout_marginStart="0dp"
                android:layout_marginLeft="0dp"
                android:layout_marginEnd="0dp"
                android:layout_marginRight="0dp"
                android:src="@drawable/strike" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="85dp"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginTop="20dp"
                android:layout_weight="1"
                android:gravity="start"
                android:text="@string/email"
                android:textColor="#000000"
                android:textSize="18sp"
                android:textStyle="" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1.5"
                android:background="@drawable/edittextbackground"
                android:inputType="text" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="85dp"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginTop="20dp"
                android:layout_weight="1"
                android:gravity="start"
                android:text="@string/password"
                android:textColor="#000000"
                android:textSize="18sp"
                android:textStyle="" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1.5"
                android:background="@drawable/edittextbackground"
                android:inputType="text"/>
        </LinearLayout>

        <Button
            android:id="@+id/dummy_button"
            style="@style/Widget.AppCompat.Button.Borderless"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="0dp"
            android:layout_weight="0"
            android:background="@drawable/rounded_corners"
            android:drawablePadding="-10dp"
            android:paddingStart="30dp"
            android:text="@string/signin"
            android:textColor="#FFFFFF"
            android:textSize="18sp" />

    </LinearLayout>

最后,这是片段java:

package com.example.projectconviencesplash;

import androidx.fragment.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.FragmentManager;

public class SigninTwoActivity extends Fragment {
    @Override
    public View  onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.signinfragment, container, false);
    }
}

同样,当我点击这些按钮时,它们似乎什么也没做。 此外,如果有人觉得特别慷慨,我很想知道如何在加载此活动时将我的一个片段设为默认值,以便在第一次加载时它不仅仅是一个带有两个按钮的空白屏幕。 我感谢这些家伙的所有帮助!

当您想替换片段内的片段时,请使用getChildFragmentManager而不是getFragmentManager

我将使用更基本的代码,因为它会更容易遵循,并希望在其他项目中实现它时有所帮助。 我在下面提供的代码将有一个 MainActivity、FragmentOne、FragmentTwo、FragmentThree。 MainActivity 将在启动时加载 FragmentOne,FragmentOne 将有两个按钮,一个用于加载 FragmentTwo,另一个用于加载 FragmentThree。

activity_main.xml 这个FrameLayout 将保存片段,注意ID fragment_container。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

MainActivity 活动应该扩展 AppCompatActivity

public class MainActivity extends AppCompatActivity {

    /**
     * Called when the activity is created
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Reference to Fragment one object
        FragmentOne fragOne = new FragmentOne();

        // Reference to fragment transaction.
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

        // Fragment Transaction
        fragmentTransaction.add(R.id.fragment_container, fragOne)
                .addToBackStack(null) // Adds fragment to back stack, for backward navigation.
                .commit(); // Finishes the fragment transaction.
    }
}

fragment_one.xml 这里没什么特别的。

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FragmentOne"
    android:orientation="vertical">


    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment One"
        android:textSize="32sp"
        tools:layout_editor_absoluteX="143dp" />

    <Button
        android:id="@+id/btn_fragment_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment two"
        tools:layout_editor_absoluteY="401dp" />

    <Button
        android:id="@+id/btn_fragment_three"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment three"

        tools:layout_editor_absoluteY="401dp" />

</androidx.appcompat.widget.LinearLayoutCompat>

片段一

public class FragmentOne extends Fragment {

    /**
     * Called when the fragment loads its user interface
     */
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_one,container, false);

        // Reference the fragments
        final FragmentTwo fragTwo = new FragmentTwo();
        final FragmentThree fragThree = new FragmentThree();

        // Create FragmentTransaction, getActivity returns context of the activity in a fragment.
        final FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();

        // Reference Button view.
        Button loadFragTwoBtn = v.findViewById(R.id.btn_fragment_two);
        // Set clickListener
        loadFragTwoBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                transaction.replace(R.id.fragment_container, fragTwo)
                        .addToBackStack(null)
                        .commit();
            }
        });

        Button loadFragThreeBtn = v.findViewById(R.id.btn_fragment_three);
        loadFragThreeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                transaction.replace(R.id.fragment_container, fragThree)
                        .addToBackStack(null)
                        .commit();
            }
        });

        return v;
    }
}

fragment_two.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context=".FragmentTwo">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Fragment Two"
        android:textSize="32sp"/>
</FrameLayout>

片段二

public class FragmentTwo extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_two, container, false);

        return view;
    }
}

fragment_three 公共类 FragmentTwo 扩展 Fragment {

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_two, container, false);

    return view;
}

}


FragmentThree

公共类 FragmentThree 扩展了 Fragment {

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_three, container, false);

    return view;
}

}

Hope this is helpful, I would also strongly recommend doing the fragment code labs to help build a strong foundation. 

Additional note, 
When providing an ID for a button the ID has to be unique, in your layout, all of your buttons use the same ID. 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM