从另一个片段访问Actionbar TextView

时间:2014-08-03 15:27:52

标签: java android android-fragments android-actionbar

您好我试图从操作栏访问文本视图,例如按下按钮时我想要更改操作栏中的文本视图。这可能吗 ?

以下是我的代码MainActivity with actionbar

mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
preferences = PreferenceManager.getDefaultSharedPreferences(this);
new URLAsyncCaller().execute();
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
FragmentManager fragmentManager = getSupportFragmentManager();
ActionBar actionBar = getSupportActionBar();
actionBar.setCustomView(R.layout.actionbar_layout);
LinearLayout search = (LinearLayout) actionBar.getCustomView().findViewById  (R.id.btn_chart);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("lastid", 1); // also puts the data into a shared
editor.commit();
TextView toptitle= (TextView)search.findViewById(R.id.tv_avtiontext);
toptitle.setText(GWave.getActiontext());`

Fragement B Code

GWave.setActiontext("Welcome To Z-Wave");

我尝试在主活动中创建一个方法来处理文本更改,但无法使其正常工作。任何建议将不胜感激..谢谢。

1 个答案:

答案 0 :(得分:1)

假设TextView中的ActionBar标识为action_text, 在Fragment中,执行此操作:

TextView actionText = (TextView) getActivity().findViewById(R.id.action_text);  

EtVoilà!你完成了! :)