【问题标题】:Update textView in Fragment from Activity从 Activity 更新 Fragment 中的 textView
【发布时间】:2018-08-01 20:36:25
【问题描述】:

我在 Fragment 中有 3 个 TextView..

在 MainActivity 我有每 10 秒生成 3 个字符串的方法。

现在我想从活动中更新 TextViews,但我不知道如何。

有人可以给我建议吗?

【问题讨论】:

    标签: android


    【解决方案1】:

    可以在您的活动中使用接口来实现,并在生成字符串时将该参数传递给您的片段:

    public class MainActivity extends AppCompatActivity {
    private OnGenerateStringListener onGenerateStringListener;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        onGenerateStringListener = (OnGenerateStringListener) this;
    
        onStartGenerateStringFragment();
    
        try {
            generateString();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    
    }
    
    private void generateString() throws InterruptedException {
    
        // After string a is generated
        String a = "test one";
        onGenerateStringListener.onGeneratedString(a);
    
        // Sleep 10 secs
        Thread.sleep(10000);
    
        String b = "test Two";
        onGenerateStringListener.onGeneratedString(b);
    
        // Sleep 10 secs
        Thread.sleep(10000);
    
        String c = "test Three";
        onGenerateStringListener.onGeneratedString(c);
    
    }
    
    private void onStartGenerateStringFragment() {
     //Method to launch your fragment
    }    
    
    interface OnGenerateStringListener {
        void onGeneratedString(String string);
    }
    
    }
    

    这是你的Fragment 课程:

    public class FragmentTextView extends Fragment implements MainActivity.OnGenerateStringListener{
    
        private View view;
        private TextView textViewOne;
        private TextView textViewTwo;
        private TextView textViewThree;
    
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            if (view != null) {
                ViewGroup group = (ViewGroup) view.getParent();
                if (group != null) {
                    group.removeView(view);
                }
            } else {
                view = inflater.inflate(R.layout.fragment_layout, container, false);
                textViewOne = view.findViewById(R.id.textViewOne);
                textViewTwo = view.findViewById(R.id.textViewTwo);
                textViewThree = view.findViewById(R.id.textViewThree);
            }
            return view;
        }
    
    
        @Override
        public void onGeneratedString(String string) {
            switch (string) {
                // Use a switch case to determine which text view gets what parameters
                // for the sake of the example, I just passed a dummy text view input
                case "test one":
                    textViewOne.setText(string);
                    break;
                case "test Two":
                    textViewTwo.setText(string);
                    break;
                case "test Three":
                    textViewThree.setText(string);
                    break;
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      为 xml 文件中的 textviews 设置 id:android:id="@+id/myId 然后这样做:

      TextView textView = (TextView) findViewById(R.id.myId);
      textView.setText("updated text");
      

      【讨论】:

      • 为什么不让片段处理该功能?
      • 是的,我只是想他在问具体如何更改文本视图中的文本
      • 如果我在片段视图的活动中尝试 findviewbyid,我会得到 null
      • 我相信现在投射到 (TextView) 是多余的
      • 是的,我写它是因为我不确定他们有什么版本的 Android Studio
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多