在 Fragment 中更改 TextView [英] Change TextView inside Fragment

查看:112
本文介绍了在 Fragment 中更改 TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照这个人的 教程制作一个动作栏.假设我想更改其中一个片段内的 TextView.所以我在我的 StartActivity.java 上添加了这个,在 onCreate 下:

I followed this guy's tutorial on how to make an ActionBar. Let's say I want to change the TextView inside one of the fragments. So I added this on my StartActivity.java, under onCreate:

TextView textview = (TextView) findViewById(R.id.textView1);
textview.setText("HI!");

当我启动我的应用程序时,它崩溃了.有人能指出我正确的方向吗?

When I start my app, it crashes. Can somebody point me to the right direction?

我希望有人花时间看看这个家伙的教程,因为他的布局与我的基本相同.谢谢.

I hope somebody takes the time to look at the guy's tutorial because his layout is basically the same as mine. Thank you.

推荐答案

如果你想改变你的组件,我建议你在片段内部创建一个方法,如下所示:

If you want change your component, I suggest you to make a method inside the fragment like this:

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;

    public class DetailFragment extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.details, container, false);
            return view;
        }


        public void setText(String text){
            TextView textView = (TextView) getView().findViewById(R.id.detailsText);
            textView.setText(text);
        }

    }

这篇关于在 Fragment 中更改 TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆