当前位置: 首页 > 知识库问答 >
问题:

将复选框输入从一个片段传递到另一个片段

郦楷
2023-03-14

我在一个片段中有一个复选框列表(我们称之为FragmentA),我有一个包含两个片段(FragmentB和C)的活动。如果选中一个复选框,我想在片段C中显示一条消息,说明选中了特定的复选框。我需要帮助,因为我不知道如何将(isChecked)从片段A传递到片段C。

FragmentA.java

 CB16.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int finalScore = Values.getInstance().updateScore(CB16.getId());
            if (CB16.isChecked()) {
                score = score + finalScore;
            } else if (!CB16.isChecked()) {
                score = score - finalScore;
                tv.setText(" " + score);
            }
        }
    });

    CB17.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int finalScore = Values.getInstance().updateScore(CB17.getId());
            if (CB17.isChecked()) {
                score = score + finalScore;
            } else if (!CB17.isChecked()) {
                score = score - finalScore;
                tv.setText(" " + score);
            }
        }
    });


    CB18.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int finalScore = Values.getInstance().updateScore(CB18.getId());
            if (CB18.isChecked()) {
                score = score + finalScore;
            } else if (!CB18.isChecked()) {
                score = score - finalScore;
                tv.setText(" " + score);
            }
        }
    });


    CB19.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int finalScore = Values.getInstance().updateScore(CB19.getId());
            if (CB19.isChecked()) {
                score = score + finalScore;
            } else if (!CB19.isChecked()) {
                score = score - finalScore;
                tv.setText(" " + score);
            }
        }
    });


    CB20.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int finalScore = Values.getInstance().updateScore(CB20.getId());
            if (CB20.isChecked()) {
                score = score + finalScore;
            } else if (!CB20.isChecked()) {
                score = score - finalScore;
                tv.setText(" " + score);
            }
        }
    });

    CB21.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int finalScore = Values.getInstance().updateScore(CB21.getId());
            if (CB21.isChecked()) {
                score = score + finalScore;
            } else if (!CB21.isChecked()) {
                score = score - finalScore;
                tv.setText(" " + score);
            }
        }
    });


    CB22.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int finalScore = Values.getInstance().updateScore(CB22.getId());
            if (CB22.isChecked()) {
                score = score + finalScore;
            } else if (!CB22.isChecked()) {
                score = score - finalScore;
                tv.setText(" " + score);
            }
        }
    });





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


            ((MainActivityMale)getActivity()).setViewPager(6);            }
    });






    return view;

}

分数不影响碎片C。

包含片段 B 和 C 的活动

 FragmentB fragmentb = new FragmentB();
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.constraintLayout1,fragmentB,fragmentB.getTag())
            .commit();

    FragmentC fragmentc = new FragmentC();
    FragmentManager fragmentManagerrslt = getSupportFragmentManager();
    fragmentManagerC.beginTransaction()
            .replace(R.id.constraintLayout2,fragmentC,fragmentC.getTag())
            .commit();

片段C 活动

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_fragment_c, container, false);
    Log.d(TAG, "onCreateView: started");


    return view;
}

共有1个答案

孙泳
2023-03-14

有两种方法可以在活动和片段之间共享信息。一种是在每个开关上手动传递它们作为参数。第二种(听起来和你可能想要的最相似)是将复选框数据存储在共享首选项中。

由于您提到您的片段处于不同的活动中,因此您首先必须将参数(复选框值)传递给下一个活动,然后下一个活动可以将它们传递给片段。

向新活动传递参数的示例

将布尔checkboxValue以及该复选框的唯一ID放入新的Intent

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
Bundle b = new Bundle();
b.putBoolean("key", checkboxValue); //Your id
intent.putExtras(b); //Put your id to your next Intent
startActivity(intent);
finish();

然后在新活动中获取 ID:

Bundle b = getIntent().getExtras();
boolean checkboxValue = false; // or other values
if(b != null)
    value = b.getBoolean("key", false);

将参数传递给片段的示例

创建片段时,只需向其添加参数,如下所示:

Fragment f = new FragmentC();
Bundle b = new Bundle();
b.putBoolean("uniqueId", checkboxValue);
f.setArguments(b);

然后在您的FragmentTransaction中使用该片段

稍后在onCreateonCreateViewmethod中完成从片段中提取:

Bundle args = getArguments();
int checkboxValue = args.getBoolean("uniqueId", false);

共享首选项是应用的全局数据存储。您可以在应用程序的任何部分中输入和提取数据。放置数据的示例如下所示

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences()
SharedPreferences.Editor edit = prefs.edit()
edit.putBoolean("checkbox1", checkboxValue)
edit.apply()

当你想从应用程序的其他部分获取数据时,你可以这样做:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences()
boolean checkboxValue = prefs.get("checkbox1", false)

这是在您的应用程序中存储全局设置的好方法!在此处了解更多信息

 类似资料: