【问题标题】:Access Actionbar TextView from another fragement从另一个片段访问操作栏 TextView
【发布时间】:2014-08-03 15:27:51
【问题描述】:

您好,我正在尝试从操作栏中访问文本视图,例如,当按下按钮时,我希望更改操作栏中的文本视图。这可能吗 ?

以下是我的带有操作栏的代码 MainActivity

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());`

片段 B 代码

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

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

【问题讨论】:

  • PS:您的代码非常非结构化。请考虑制作一些可以调用的私有方法来完成工作。 :)

标签: java android android-fragments android-actionbar


【解决方案1】:

假设您的ActionBar 中的TextView 有一个ID action_text
Fragment 中,这样做:

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

等等!你完成了! :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-27
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多