如何在android中通过点击按钮从另一个片段中替换和打开一个片段?

问题描述 投票:0回答:1

我无法在第二个片段中删除一个当前片段,同时试图从第一个片段中替换它,并试图通过点击按钮从第一个片段中打开第二个片段。

以下是我的代码

FirstFragment.java

package com.example.testanderase;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.design.widget.TextInputEditText;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.List;
import java.util.Objects;

public class FirstFragment extends Fragment implements View.OnClickListener{

    TextInputEditText inputEditText;
    Button enterButton;
    String getMessage;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.first_fragment,container,false);
        inputEditText = view.findViewById(R.id.enter_edit_txt_input);
        enterButton = view.findViewById(R.id.first_fragment_btn);
        enterButton.setOnClickListener(this);

        return view;
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void onClick(View v) {
        getMessage = inputEditText.getText().toString();

        SecondFragment secondFragment = new SecondFragment();
        Bundle b = new Bundle();
        b.putString("data",getMessage);
        secondFragment.setArguments(b);

        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.second_fragment_layout,secondFragment);
        //transaction.add(R.id.second_fragment_layout,secondFragment);
        transaction.commit();

****下面我试图修改代码***为了从第一个片段打开第二个片段。 assert getFragmentManager() != null; FragmentManager fm = getFragmentManager(); fm.beginTransaction().replace(R.id.second_fragment_layout,secondFragment).commit(); /fm.beginTransaction().replace(SecondFragment.newInstance()); }。 }

first_fragment.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:gravity="center">

    <LinearLayout
        android:id="@+id/first_fragment_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center">

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginEnd="20dp">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/enter_edit_txt_input"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="@string/input"
            android:textAlignment="center"
            android:gravity="center_horizontal" />

    </android.support.design.widget.TextInputLayout>

    <Button
        android:id="@+id/first_fragment_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="@string/enter"
        android:background="@color/black"
        android:textColor="@color/white"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        />
    </LinearLayout>
</LinearLayout>

SecondFragment.java

package com.example.testanderase;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class SecondFragment extends Fragment implements View.OnClickListener{

    TextView textView;
    Button enterButton;
    String firstFragmentValue;
    Toast t;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.second_fragment,container,false);
        textView = view.findViewById(R.id.second_fragment_txt_view);
        enterButton = view.findViewById(R.id.second_fragment_btn);
        enterButton.setOnClickListener(this);

        //assert getArguments() != null;
        //firstFragmentValue = getArguments().getString("data");
        //textView.setText(firstFragmentValue);

        Bundle bundle = getArguments();
        if(bundle != null)
        {
            String name = bundle.getString("data");
            textView.setText(name);
            Toast.makeText(getContext(),"passed data "+name,Toast.LENGTH_SHORT).show();
        }

        //textView.setText(firstFragmentValue);

        return view;
    }

    @Override
    public void onClick(View v) {
        //
        ToastButton();
        /*assert getFragmentManager() != null;
        FirstFragment firstFragment = new FirstFragment();
        FragmentManager fragmentTransaction = getFragmentManager();
        fragmentTransaction.beginTransaction().replace(R.id.first_fragment_layout, firstFragment).commit();*/
    }

    public void ToastButton()
    {
        if(t!=null)
        {
            t.cancel();
        }
        else
        {
            Toast.makeText(getContext(),"Clicked",Toast.LENGTH_SHORT).show();
        }
    }
}

second_fragment.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:gravity="center">


    <TextView
        android:id="@+id/second_fragment_txt_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginEnd="20dp"
        android:text="@string/change_text"
        android:textAlignment="center"
        android:gravity="center_horizontal"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        />

    <Button
        android:id="@+id/second_fragment_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="@string/back_button_text"
        android:background="@color/black"
        android:textColor="@color/white"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        />
</LinearLayout>

SectionPageAdapter.java

package com.example.testanderase;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class SectionPageAdapter extends FragmentPagerAdapter {

    private final List<Fragment> fragmentList = new ArrayList<>();
    private final List<String> fragmentListTitle = new ArrayList<>();

    public void addFragment(Fragment fragment, String title)
    {
        fragmentList.add(fragment);
        fragmentListTitle.add(title);
    }

    public SectionPageAdapter(FragmentManager fm)
    {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        return fragmentList.get(i);
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return fragmentListTitle.get(position);
    }

    @Override
    public int getCount() {
        return fragmentList.size();
    }
}

MainActivity.java

package com.example.testanderase;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity{

    private SectionPageAdapter mSectionPageAdapter;
    private ViewPager mainViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

            mSectionPageAdapter = new SectionPageAdapter(getSupportFragmentManager());

            mainViewPager = findViewById(R.id.container);
            setMainViewPager(mainViewPager);

            TabLayout tabLayout = findViewById(R.id.tabs);
            tabLayout.setupWithViewPager(mainViewPager);
    }

    public void setMainViewPager(ViewPager viewPager)
    {
        SectionPageAdapter adapter = new SectionPageAdapter(getSupportFragmentManager());
        adapter.addFragment(new FirstFragment(),"FIRST");
        adapter.addFragment(new SecondFragment(),"SECOND");
        viewPager.setAdapter(adapter);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity"
    android:id="@+id/main_layout"
    android:layout_margin="20dp">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


</LinearLayout>
android android-studio android-fragments android-button
1个回答
1
投票

R.id.second_fragment_layout在哪里?

无论如何,将你的second_fragment.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:gravity="center">
<LinearLayout
    android:id="@+id/second_fragment_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

<TextView
    android:id="@+id/second_fragment_txt_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="20dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginEnd="20dp"
    android:text="hello"
    android:textAlignment="center"
    android:gravity="center_horizontal"

android:textAppearance="@style/TextAppearance.AppCompat.Large"
    />

<Button
    android:id="@+id/second_fragment_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:text="back"
    android:background="#000"
    android:textColor="#fff"

android:textAppearance="@style/TextAppearance.AppCompat.Large"
    />
</LinearLayout>
</LinearLayout>

并通过点击按钮从第一个片段中打开第二个片段。

先把mainViewPager做成静态的

static ViewPager mainViewPager

把这个放在第一个片段上点击

int page =1;mainViewPager.setCurrentItem(page)。

返回到第一个片段

把这个放在第二个片段上点击

int page =0;mainViewPager.setCurrentItem(page)。

ts为我工作。

© www.soinside.com 2019 - 2024. All rights reserved.