如何在 FAB 按钮单击时将数据从多个片段发送到单个活动

Posted

技术标签:

【中文标题】如何在 FAB 按钮单击时将数据从多个片段发送到单个活动【英文标题】:How to send data from Multiple Fragments to single activity on FAB button click 【发布时间】:2020-09-02 11:49:33 【问题描述】:

我的问题是点击b4(fab)只有 Fragment B 数据进入 SecondActivity.I 希望同时收集 Fragment A 和 Fragment B 数据 当单击片段 A 和可用的b4Fab 按钮时 片段 B。

也代替 Fragment A 数据 t4.setText(vres);,t5.setText(vres1);,t6.setText(vres2); 的 第二个活动得到一个值null。我不明白为什么会这样?而且 总是从 Fragment B 获取数据。我想要 Fragment A 和 当 b4(fab) 为时,在 SecondActivity 处获取的片段 B 数据 已点击。提前致谢。

片段 A

公共类 Fragment_nonveg 扩展 Fragment

    TextView t,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10;
    CheckBox c1,c2,c3,c4,c5;
    Button b1,b2,b5,b6,b7,b8,b9,b10,b11,b12;
    ImageButton b3;
    int x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,g1,g2,g3,g4,g5,g6,g7,g8,g9,g10;
    String res,res1,res2,res3,res4,re,re1,re2,re3,re4;
    FloatingActionButton b4;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 
        Bundle bundle = this.getArguments();

        return inflater.inflate(R.layout.fragment_nonveg, null);
    
@Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) 

        super.onActivityCreated(savedInstanceState);
        b4 = getActivity().findViewById(R.id.fab);
b4.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 

                Intent obj4 = new Intent(getActivity(), Bill.class);
                //obj4.putExtra("k", r1);

                int total = 0;
                String item = "", qty = "", amt = "";
                if (c1.isChecked() == true) 

                    item = item + c1.getText().toString() + "\n";
                    qty = qty + t2.getText().toString() + "\n";
                    amt = amt + t1.getText().toString() + "\n";
                    String t = t1.getText().toString();
                    total = total + Integer.parseInt(t);
                
                if (c2.isChecked() == true) 
                    item = item + c2.getText().toString() + "\n";
                    qty = qty + t4.getText().toString() + "\n";
                    amt = amt + t3.getText().toString() + "\n";
                    String t = t3.getText().toString();
                    total = total + Integer.parseInt(t);
                
                if (c3.isChecked() == true) 
                    item = item + c3.getText().toString() + "\n";
                    qty = qty + t6.getText().toString() + "\n";
                    amt = amt + t5.getText().toString() + "\n";
                    String t = t5.getText().toString();
                    total = total + Integer.parseInt(t);
                
                if (c4.isChecked() == true) 
                    item = item + c4.getText().toString() + "\n";
                    qty = qty + t8.getText().toString() + "\n";
                    amt = amt + t7.getText().toString() + "\n";
                    String t = t7.getText().toString();
                    total = total + Integer.parseInt(t);

                
                if (c5.isChecked() == true) 
                    item = item + c5.getText().toString() + "\n";
                    qty = qty + t10.getText().toString() + "\n";
                    amt = amt + t9.getText().toString() + "\n";
                    String t = t9.getText().toString();
                    total = total + Integer.parseInt(t);

                

                int T=total;
                obj4.putExtra("key", item);
                obj4.putExtra("key2", qty);
                obj4.putExtra("key3", amt);
                obj4.putExtra("key16", T);
                getActivity().startActivity(obj4);
            
       );

    

片段 B

public class Fragment_veg extends Fragment 


    FloatingActionButton b4;


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

        return inflater.inflate(R.layout.fragment_veg,null);
    

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) 
        super.onActivityCreated(savedInstanceState);
b4 = getActivity().findViewById(R.id.fab);
b4.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                //Do what you want
                Intent obj4 = new Intent(getActivity(), Bill.class);


                int total = 0;
                String vitem = "", vqty = "", vamt = "";
                if (vc1.isChecked() == true) 

                    vitem = vitem + vc1.getText().toString() + "\n";
                    vqty = vqty + t2.getText().toString() + "\n";
                    vamt = vamt + t1.getText().toString() + "\n";
                    String t = t1.getText().toString();
                    total = total + Integer.parseInt(t);
                
                if (vc2.isChecked() == true) 
                    vitem = vitem + vc2.getText().toString() + "\n";
                    vqty = vqty + t4.getText().toString() + "\n";
                    vamt = vamt + t3.getText().toString() + "\n";
                    String t = t3.getText().toString();
                    total = total + Integer.parseInt(t);
                
                if (vc3.isChecked() == true) 
                    vitem = vitem + vc3.getText().toString() + "\n";
                    vqty = vqty + t6.getText().toString() + "\n";
                    vamt = vamt + t5.getText().toString() + "\n";
                    String t = t5.getText().toString();
                    total = total + Integer.parseInt(t);
                
                if (vc4.isChecked() == true) 
                    vitem = vitem + vc4.getText().toString() + "\n";
                    vqty = vqty + t8.getText().toString() + "\n";
                    vamt = vamt + t7.getText().toString() + "\n";
                    String t = t7.getText().toString();
                    total = total + Integer.parseInt(t);

                
                if (vc5.isChecked() == true) 
                    vitem = vitem + vc5.getText().toString() + "\n";
                    vqty = vqty + t10.getText().toString() + "\n";
                    vamt = vamt + t9.getText().toString() + "\n";
                    String t = t9.getText().toString();
                    total = total + Integer.parseInt(t);

                
                int vT;
                vT = total;
                obj4.putExtra("vkey", vitem);
                obj4.putExtra("vkey2", vqty);
                obj4.putExtra("vkey3", vamt);
                obj4.putExtra("vkey16", vT);
                startActivity(obj4);
            

        );
    


主活动

public class Menu extends AppCompatActivity 

    TextView nonveg,veg,snacks,desert,beverges;
    ViewPager viewPager;
    PageViewAdapter pageViewAdapter;
    public FloatingActionButton b4;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        nonveg=(TextView)findViewById(R.id.nonveg);
        veg=(TextView)findViewById(R.id.veg);
        snacks=(TextView)findViewById(R.id.snacks);
        desert=(TextView)findViewById(R.id.desert);
        beverges=(TextView)findViewById(R.id.beverges);
        viewPager=(ViewPager)findViewById(R.id.fragment_container);
        pageViewAdapter=new PageViewAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pageViewAdapter);

        nonveg.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                viewPager.setCurrentItem(0);
            
        );

        veg.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                viewPager.setCurrentItem(1);
            
        );

        snacks.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                viewPager.setCurrentItem(2);
            
        );

        desert.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                viewPager.setCurrentItem(3);
            
        );

        beverges.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                viewPager.setCurrentItem(4);
            
        );

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() 
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) 

            

            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onPageSelected(int position) 
                onChangeTab(position);

            

            @Override
            public void onPageScrollStateChanged(int state) 

            
        );

    

第二个活动

public class Bill extends AppCompatActivity 

    TextView t,t1,t2,t3,t16,t17,t4,t5,t6;
    Button b,b1;
    static   String res,res1,res2,/*res15,*/vres,vres1,vres2/*vres15*/;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bill);
        t = findViewById(R.id.textView);
        t1 = findViewById(R.id.textView5);
        t2 = findViewById(R.id.textView10);
        t3 = findViewById(R.id.textView15);
        t4 = findViewById(R.id.textView6);
        t5 = findViewById(R.id.textView11);
        t6 = findViewById(R.id.textView16);
        t16 = findViewById(R.id.textView20);
        t17 = findViewById(R.id.textView21);
        b = findViewById(R.id.button22);
        b1 = findViewById(R.id.button23);
        Intent obj2 = getIntent();
        res = obj2.getStringExtra("key");
        res1 = obj2.getStringExtra("key2");
        res2 = obj2.getStringExtra("key3");
        int res15 = getIntent().getIntExtra("key16",0);
        vres = obj2.getStringExtra("vkey");
        vres1 = obj2.getStringExtra("vkey2");
        vres2 = obj2.getStringExtra("vkey3");
        int vres15 = getIntent().getIntExtra("vkey16",0);
        //vres15 = obj2.getStringExtra("vkey16");
        t1.setText(res);
        t2.setText(res1);
        t3.setText(res2);
        t4.setText(vres);
        t5.setText(vres1);
        t6.setText(vres2);
        t16.setText("Total Price:");
        int total2 = res15+vres15;
        t17.setText(String.valueOf(total2));


【问题讨论】:

【参考方案1】:

为了在片段和活动之间传递数据,您应该声明接口,然后在片段中创建该接口的变量,并在按钮单击时调用接口的功能之一,同时在您的活动中实现这些方法,这样您就可以通过通过接口函数和这些函数的参数从片段到您的活动的任何数据

 interface Test
    void onSthHappend(String str);

 
class fragment extends Fragment
private Test interface;
 @Override
    public void onAttach(@NonNull Context context) 
        super.onAttach(context);
        interface= (Test ) context;
    
   fab.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
interface.onSthHappend("hi");
            
        );

clas activity Extends Activity implements Test
@override onSthHappend(String str)
//do your stuff


【讨论】:

通过这样做,它会获取多个片段数据并将其传递给单个活动吗?因为我已经在 *** 上经历了许多这种类型的解决方案,但它们是针对单个片段的。 我通过在以下参考答案中对所有其他片段处理相同的编码Fragment A 找到了此问题所需的内容。请参阅此 [***.com/questions/61868402/…【参考方案2】:

在这个问题中,需要的是sharedpreferences

第1步:- 在不同Fragment中的Activity片段容器中实现如下:-

public class Fragment_class_name extends Fragment 
public static int totalp;
    public static String itemp, qtyp, amtp;
    SharedPreferences sp;
    private static Fragment_nonveg instance;
@Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 
        Bundle bundle = this.getArguments();
        instance = this;
        return inflater.inflate(R.layout.fragment_nonveg, null);
    
public void onActivityCreated(@Nullable Bundle savedInstanceState) 

            super.onActivityCreated(savedInstanceState);
     //do your code

     public static Fragment_nonveg getInstance() 
            return instance;
        
        public void yourfuncname()//any calculative function you want to calculate or //pass data
sp=this.getActivity().getSharedPreferences("calis", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor=sp.edit();
editor.putString("key1",value1);
            editor.putString("key2",value2);
editor.commit();
        
    

第 2 步:- 然后在您的 Activity 中执行以下操作:-

public class Activity_class_name extends AppCompatActivity 

   
    ViewPager viewPager;
    PageViewAdapter pageViewAdapter;
    public FloatingActionButton b4;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        b4 = findViewById(R.id.fab);
b4.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                Intent obj4 = new Intent(Menu.this, Bill.class);
                Fragment_class_name.getInstance().yourfuncname();
                startActivity(obj4);
            
        );

第 3 步:- 对片段容器的所有片段执行此操作

【讨论】:

以上是关于如何在 FAB 按钮单击时将数据从多个片段发送到单个活动的主要内容,如果未能解决你的问题,请参考以下文章

在通知单击时将项目添加到片段内的 recyclerview

FloatingActionButton 子菜单

如何在按钮单击时将用户发送到 Facebook 页面(Facebook Messenger)

如何在单击按钮时将按钮颜色保存到共享首选项?

AngularJS:将布尔值绑定到单选按钮,以便在取消选中事件时将模型更新为 false

Aurelia在检查更改时将值绑定到单选按钮