在活动的一个片段中访问的TextView [英] accessing textview within a fragment in activity

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

问题描述

我想用片段,在动作条。不幸的是,它看起来像它真的复杂。我的片段具有Textviews,我希望能够与他们沟通我的活动。在我开始使用片段我能访问它们

 私人的EditText EDITTEXT =(EditText上)findViewById(R.id.editTextName);
 

所以,我能够接受EDITTEXT值当用户点击保存。我应该怎么做这个片段路?

活动:

  @覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_add_recipe);
    。getSupportActionBar()setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    。getSupportActionBar()setDisplayOptions(0,com.actionbarsherlock.app.ActionBar.DISPLAY_SHOW_HOME | com.actionbarsherlock.app.ActionBar.DISPLAY_USE_LOGO | com.actionbarsherlock.app.ActionBar.DISPLAY_SHOW_TITLE);

    ActionBar.Tab newTab0 = getSupportActionBar()
            。新标签()
            .setText(R.string.fragment_general)
            .setTabListener(
            新MyTabListener< GeneralFragment>(这一点,一般,
                            GeneralFragment.class));


    。getSupportActionBar()addTab(newTab0);

}
公共静态类MyTabListener<吨延伸片断>器物
        TabListener {
    私人片段mFragment;
    私人最终活动mActivity;
    私人最终字符串MTAG;
    私人最终类别< T> mClass;

    / **
     * *构造函数用于创建一个新的标签,每次。 * * @参数
     *活动*主机活动,用实例片段*参数
     *标记*的标识符标记为片段*参数CLZ *的
     *片段的类,用于实例片段
     * /

    公共MyTabListener(活动活动,字符串变量,类< T> CLZ){
        mActivity =活动;
        MTAG =标签;
        mClass = CLZ;
    }

    / *下面是每个ActionBar.TabListener回调* /

    公共无效onTabSelected(TAB键,FragmentTransaction英尺){
        //检查片段已初始化
        如果(mFragment == NULL){
            //如果没有,实例化并把它添加到活动
            mFragment = Fragment.instantiate(mActivity,mClass.getName());
            ft.add(android.R.id.content,mFragment,MTAG);
        } 其他 {
            //如果它存在,只需将它为了显示它
            //ft.setCustomAnimations(android.R.animator.fade_in,R.animator.animationtest);
            ft.attach(mFragment);
        }
    }

    公共无效onTabUnselected(TAB键,FragmentTransaction英尺){
        如果(mFragment!= NULL){
            //ft.setCustomAnimations(android.R.animator.fade_in,R.animator.test);
            ft.detach(mFragment);
        }
    }

    公共无效onTabReselected(TAB键,FragmentTransaction英尺){
    }
}
 

解决方案

您应该调用getView()上的片段,然后用findViewById()的视图返回。

当然,它不会返回任何视图创建后,直到,所以你可能在某个地方调用它除了的onCreate。

I wanted to use Fragments with in the ActionBar. Unfortunately it looks like its really complicated. My Fragments have Textviews and I want to be able to communicate with them out of my activity. Before I started to use Fragments I could access them with

private EditText editText = (EditText) findViewById(R.id.editTextName);

So I was able to receive the editText value when a user clicked on save. How should I do this the Fragment-way?

Activity:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_recipe);
    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    getSupportActionBar().setDisplayOptions(0, com.actionbarsherlock.app.ActionBar.DISPLAY_SHOW_HOME | com.actionbarsherlock.app.ActionBar.DISPLAY_USE_LOGO | com.actionbarsherlock.app.ActionBar.DISPLAY_SHOW_TITLE);

    ActionBar.Tab newTab0 = getSupportActionBar()
            .newTab()
            .setText(R.string.fragment_general)
            .setTabListener(
            new MyTabListener<GeneralFragment>(this, "general",
                            GeneralFragment.class));


    getSupportActionBar().addTab(newTab0);

}
public static class MyTabListener<T extends Fragment> implements
        TabListener {
    private Fragment mFragment;
    private final Activity mActivity;
    private final String mTag;
    private final Class<T> mClass;

    /**
     * * Constructor used each time a new tab is created. * * @param
     * activity * The host Activity, used to instantiate the fragment * @param
     * tag * The identifier tag for the fragment * @param clz * The
     * fragment's Class, used to instantiate the fragment
     */

    public MyTabListener(Activity activity, String tag, Class<T> clz) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
    }

    /* The following are each of the ActionBar.TabListener callbacks */

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // Check if the fragment is already initialized
        if (mFragment == null) {
            // If not, instantiate and add it to the activity
            mFragment = Fragment.instantiate(mActivity, mClass.getName());
            ft.add(android.R.id.content, mFragment, mTag);
        } else {
            // If it exists, simply attach it in order to show it
            //ft.setCustomAnimations(android.R.animator.fade_in, R.animator.animationtest);
            ft.attach(mFragment);
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        if (mFragment != null) {
            //ft.setCustomAnimations(android.R.animator.fade_in, R.animator.test);
            ft.detach(mFragment);
        }
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }
}

解决方案

You should call getView() on the Fragment and then use findViewById() on the view returned.

Of course, it won't return anything until after the view has been created, so you may have to call it somewhere besides onCreate.

这篇关于在活动的一个片段中访问的TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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