简体   繁体   English

使用Android标签无法将信息从一个片段传递到另一个片段

[英]Passing info from one fragment to another using Android tabs not working

I have created a basic android app which uses TabHost and contains two tabs. 我创建了一个使用TabHost并包含两个标签的基本android应用。 The first tab has a fragment to input 2 values, and the 2nd tab has a fragment containing: a button to see the solution, a TextView to display the solution, and a button to reset all values in both fragments. 第一个选项卡包含一个片段,用于输入2个值,第二个选项卡包含一个片段,其中包含:用于查看解决方案的按钮,用于显示解决方案的TextView和用于重置两个片段中所有值的按钮。 All appears to work smoothly the 1st time I run through the app. 第一次运行该应用程序时,所有功能似乎都能正常运行。 The problem begins after viewing the solution the 1st time. 问题是在第一次查看解决方案后开始的。 If I try to enter two new numbers, and then click to see the solution, I see the last value of the TextView (either "" if having pressed reset, or the previous answer when not having pressed reset. Any suggestions as to what I may need to change would be greatly appreciated. Please let me know if any additional code is needed. Thanks in advance. 如果我尝试输入两个新数字,然后单击以查看解决方案,我将看到TextView的最后一个值(如果按下了reset,则为“”;如果按下了reset,则为上一个答案。关于我的任何建议可能需要更改将不胜感激。如果需要任何其他代码,请告诉我。

Note: All fragments import android.support.v4.app.Fragment. 注意:所有片段都会导入android.support.v4.app.Fragment。

InputFragment.java InputFragment.java

public class InputFragment extends Fragment {

public static String answerString = "";
public static EditText e1;
public static EditText e2;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_input,container, false);
    EditText et1 = (EditText)view.findViewById(R.id.editText1);
    EditText et2 = (EditText)view.findViewById(R.id.editText2);
    setEt(et1, et2);
    return view;
}   

public void setEt(EditText a, EditText b) {
    e1 = a;
    e2 = b;
}   
}

SolutionFragment.java: SolutionFragment.java:

public class SolutionFragment extends Fragment {

public String answer;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_solution,container,false);
    updateAnswer();
    Button b1 = (Button)view.findViewById(R.id.button1);//get answer button
    b1.setOnClickListener(new OnClickListener() {           
        @Override
        public void onClick(View v) {
            setText(answer);
        }
    });

    Button b2 = (Button)view.findViewById(R.id.button2);//reset button
    b2.setOnClickListener(new OnClickListener() {           
        @Override
        public void onClick(View v) {
            setText("");
            InputFragment.e1.setText("");
            InputFragment.e2.setText("");
            answer = "";
        }
    });
    return view;
}

public void updateAnswer() {
    String s1 = InputFragment.e1.getText().toString();
    String s2 = InputFragment.e2.getText().toString();
    double num1 = Double.parseDouble(s1);
    double num2 = Double.parseDouble(s2);
    double ans = num1 + num2;
    String ansS = String.valueOf(ans);
    answer = ansS;
}

public void setText(String s) {
    TextView tv = (TextView)getView().findViewById(R.id.textView2);
    tv.setText(s);
}
}

Based on the link that was provided by Shayan Pourvatan ( How to pass values between Fragments ), it looks like the simple approach is to be able to: 根据Shayan Pourvatan提供的链接( 如何在Fragments之间传递值 ),看起来简单的方法是能够:

1) Create a "bundle" with the information you want to pass. 1)使用您要传递的信息创建一个“捆绑包”。 2) From some location that has access to the instance of your fragment, call setArguments() with a bundle. 2)从可以访问您的片段实例的某个位置,使用捆绑包调用setArguments()。 3) Read in those arguments during onCreateView() in your fragment by calling getArguments(); 3)通过调用getArguments()在片段的onCreateView()期间读取这些参数;

Really, I'm only focusing on step 5 and the part listed after that in the linked article. 确实,我只关注第5步以及链接文章中列出的那一部分。 So because you'll be flipping between tabs, I think this approach should work well for you. 因此,因为您将在各个选项卡之间切换,所以我认为这种方法应该适合您。 Set the arguments before you switch tabs, and then when you switch to your destination tab call getArguments(). 在切换选项卡之前设置参数,然后在切换到目标选项卡时调用getArguments()。

Let me know if that helps! 让我知道是否有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:将数据从一个片段传递到另一个 - Android: passing data from one fragment To another 使用Android Studio将对象或数组列表从一个片段传递到另一个片段 - Passing object or Array List from one Fragment to another Fragment using Android Studio 将数据从一个片段传递到另一个片段时无法正常工作 - Passing data from one fragment to another is not working when passing data Android将片段从一个替换到另一个不起作用 - Android replace fragment from one to another is not working 从一个片段传递到另一个 - Passing from one fragment to another Android:使用BroadcastReceiver将数据从一个片段传递到另一个片段 - Android: passing data from one fragment to another with BroadcastReceiver 使用 Bundle 将 textview 从一个片段更新到另一个片段不起作用 - Using Bundle to update textview from one fragment to another fragment not working 使用对象从一个片段到另一个片段,Android - Using object from one fragment to another fragment, android 将图像从一个片段传递到另一个片段并在该片段中显示图像 - Passing image from one fragment to a another fragment and display image in that fragment 将整数数组从一个片段传递到另一个片段 - Passing an integer array from one fragment to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM