如何从一个片段转移到另一个片段?

时间:2018-06-08 05:26:50

标签: android fragment transfer

我想将一些像“editText1Double”和“sum”这样的数据传输到另一个将使用这些数据的片段(SecondFragment)。我该怎么做?

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.first_fragment, container, false);
    editText1 = (EditText) v.findViewById(R.id.editText1);
    editText2 = (EditText) v.findViewById(R.id.editText2);
    editText3 = (EditText) v.findViewById(R.id.editText3);
    textView = (TextView) v.findViewById(R.id.textView);


    calculateButton = (Button) v.findViewById(R.id.calculateButton);
    calculateButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            editText1Double = Double.parseDouble(editText1.getText().toString());
            editText2Double = Double.parseDouble(editText2.getText().toString());
            editText3Double = Double.parseDouble(editText3.getText().toString());

            sum = editText1Double + editText2Double + editText3Double;
        }
    });

3 个答案:

答案 0 :(得分:0)

使用

传递数据
      try
        {
            myProcess.StartInfo.FileName = exe;
            myProcess.StartInfo.UseShellExecute = true;
            myProcess.StartInfo.CreateNoWindow = true;
            myProcess.StartInfo.ErrorDialog = false;
            myProcess.StartInfo.WorkingDirectory = 
            Path.GetDirectoryName(exe);
            myProcess.Start();  
            myProcess.WaitForExit();
            myProcess.Close();
            return true;

        }
        catch (Exception e)
        {
            return false;
        }

使用

在另一个片段中获取数据
fragment = new HomeFragment();
                Bundle bundle = new Bundle();
            bundle.putDouble("double", 1.1);
                fragment.setArguments(bundle);
                getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment, "HomeFragment").addToBackStack(null).commit();

希望它适合你。

答案 1 :(得分:0)

从fragment_A发送数据

Bundle bundle = getArguments();
double d = bundle.getDouble("double")

在fragment_B上接收数据

FragmentManager fM = getActivity().getSupportFragmentManager();
FragmentTransaction fT = fM.beginTransaction();
Fragment_B fragment = new Fragment_B();//receiving fragment
Bundle bundle = new Bundle();
bundle.putDouble("sum_key", sum);
fragment.setArguments(bundle);
fT.add(R.id.container, fragment);
fT.addToBackStack(null);
fT.commit();

答案 2 :(得分:0)

1)使用Bundle:

FragmentManager fM = getActivity().getSupportFragmentManager();
FragmentTransaction fT = fM.beginTransaction();
Fragment_B fragment = new Fragment_B();//receiving fragment
Bundle bundle = new Bundle();
bundle.putDouble("key", sum);
fragment.setArguments(bundle);
fT.add(R.id.container, fragment);
fT.addToBackStack(null);
fT.commit();

并收到

Bundle bundle = getArguments();
Double sum = bundle.getDouble("key");

2)使用Construcor:

FragmentManager fM = getActivity().getSupportFragmentManager();
FragmentTransaction fT = fM.beginTransaction();
Fragment_B fragment = new Fragment_B(sum);//receiving fragment
fT.add(R.id.container, fragment);
fT.addToBackStack(null);
fT.commit();

并收到

public class Fragment_B extends Fragment{
private double sum;
 public Fragment_B (double sum) {
        this.sum= sum;
    }
 public Fragment_B () {

 }}
相关问题