java - 通过bundle在 fragment 之间传递值

标签 java android

我是一名初学者,对 Android 技术不熟悉。实际上我试图通过不同的方法来解决已经存在的问题。我在单个 Activity 中获取了 3 个 fragment ,在第一个 fragment 中我获取了 editText,在第二个 fragment 中我获取了按钮,单击该按钮后,我尝试通过 Bundle 传递值来在第三个 fragment 中显示fragment1 数据,但我陷入了困境。如何通过这种方法完成这项任务。

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"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_weight="1"
        >
    </FrameLayout>

    <FrameLayout
        android:id="@+id/mid"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_weight="1"
        >
    </FrameLayout>

    <FrameLayout
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_weight="1"
        >
    </FrameLayout>
</LinearLayout>

主要 Activity

package com.example.com.dynamicfragmentproject;
import android.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

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

        android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        Top_fragment frg1=new Top_fragment();
        transaction.add(R.id.top,frg1);
        transaction.commit();

    }

    public void cacheData(String str) {

        android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        Mid_fragment frg2=new Mid_fragment();
        transaction.add(R.id.mid,frg2);
        transaction.commit();


        Bundle bundle = new Bundle();
        bundle.putString("editText",str);
        frg2.setArguments(bundle);
    }

    public void cacheData1(String name) {


        android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        Bottom_fragment frg3=new Bottom_fragment();
        transaction.add(R.id.bottom,frg3);
        transaction.commit();


        Bundle bundle = new Bundle();
        bundle.putString("editText",name);
        frg3.setArguments(bundle);
    }
}

fragment1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".Top_fragment">

<!-- TODO: Update blank fragment layout -->
<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/hello_blank_fragment"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:textColor="@color/colorPrimary"/>

</RelativeLayout>

Fragment1.java

package com.example.com.dynamicfragmentproject;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

public class Top_fragment extends Fragment {

    private EditText editText;
    View view;

    public Top_fragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_top_fragment, container, false);

        editText = view.findViewById(R.id.editText1);
        String str = editText.getText().toString();
        MainActivity main = (MainActivity) getActivity();

        main.cacheData(str);

        return view;
    }
}

fragment2.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".Mid_fragment">


   <Button
   android:id="@+id/button1"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_marginLeft="25dp"
   android:layout_marginRight="25dp"
   android:text="Submit"
   android:textAllCaps="false"
   android:textColor="@color/colorPrimary"
   android:layout_centerVertical="true"
   android:layout_centerHorizontal="false"
   />

</RelativeLayout>

Fragment2.java

package com.example.com.dynamicfragmentproject;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

public class Mid_fragment extends Fragment {

    private Button buttonSubmit;
    View view;

    public Mid_fragment() {
        // Required empty public constructor
    }


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

        view = inflater.inflate(R.layout.fragment_mid_fragment, container, false);

        buttonSubmit = view.findViewById(R.id.button1);

        buttonSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                MainActivity main1 = (MainActivity) getActivity();

                Bundle bundle = getArguments();
                String name = bundle.getString("editText");

                main1.cacheData1(name);
            }
        });
        return view;
    }
}

fragment3.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"
android:id="@+id/bottom"
tools:context=".Bottom_fragment">

<!-- TODO: Update blank fragment layout -->
<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/colorPrimary"
    android:padding="10dp"
     />
</FrameLayout>

Fragment3.java

package com.example.com.dynamicfragmentproject;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

public class Bottom_fragment extends Fragment {

    private TextView viewText;
    View view;

    public Bottom_fragment() {
        // Required empty public constructor
    }

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


        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_bottom_fragment, container, false);

        viewText = view.findViewById(R.id.textView1);

        Bundle bundle = getArguments();
        String name = bundle.getString("editText");
        viewText.setText(name);

        return view;
    }
}

最佳答案

在第一个 fragment 中创建第二个 fragment 的对象。

FragmentTwo fragmenttwo=new FragementTwo();
Bundle bundle = new Bundle();
bundle.putSerializable(object,"data")
fragmenttwo.setArguments(bundle);

在第二个 fragment

Bundle bundle = getArguments();
if(bundle != null){
   SampleModel model = (SampleModel) bundle.getSerializable("data");
}

关于java - 通过bundle在 fragment 之间传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50759813/

相关文章:

java - Android 中使用 BaseAdapter 的 GridView?

java - 如何将字符串转换为 GZIP Base64 字符串?

Java 正则表达式 regionEnd

android - Android用户个人资料问题

java - 广播接收器中未触发断点

java - 无法启动 SonarQube,无法启动 JVM : Permission denied (13)

java - 通过 Push、vaadin 切换附加组件

Android:将带有可播放视频的 Intent 发送到电报应用程序

java - 未打开顶部的 Activity

android - minSdkVersion 与最低 api 级别不同