Android如何根据第一个标签输入更新第二个标签?

时间:2013-03-01 06:34:01

标签: android android-fragments

我使用Fragment构建了2个标签。第一个标签包含EditTextbutton。如果用户在EditText中输入内容并点击button,则会转到第二个标签。 onButtonClick我已设置为显示第二个标签但我的值未更新。虽然我已经在session中成功编写了第一个标签值。但是不知道如何更新第二个标签?如何重建第二个标签?

在此处添加了我的代码:

在FragmentA中我onButtonClick我调用了这个函数:

private void goToFragmentB(String 1stTabValue)
{
    ViewPager mViewPager = (ViewPager) getActivity().findViewById(R.id.pager);
       // My 2nd tab has a TextView where I need to set the 1stTabValue. What should be the code
       // to set 1stTabValue in 2nd tab
    mViewPager.setCurrentItem(1);
}

3 个答案:

答案 0 :(得分:1)

编辑:

选项卡不会刷新,因为只会首次调用Intent ..在添加选项卡时,您将设置意图添加此标记addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

示例:

 Intent i = new Intent().setClass(this, YourClass.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

您可以使用Shared Preferences,将输入值存储在已存储的偏好设置中并进入第二个标签

答案 1 :(得分:0)

您可以将值存储在包含片段的活动中。我希望,你的下一个片段将使用相同的活动。但是此方法不会持久存储您的数据,如果活动已关闭,则会清除以前的所有数据。

如果您需要数据持久,那么您必须使用共享首选项。数据将存储在shared_pref文件夹下的应用程序文件夹中。它存储为键值对。

希望这有帮助。

答案 2 :(得分:0)

在添加标签时尝试此操作

  private void addTab(int drawableId,String name, Class<?> c)
{

    TabHost.TabSpec spec = tabHost.newTabSpec(name);

    View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);

    ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
    TextView text = (TextView) tabIndicator.findViewById(R.id.title);
    icon.setImageResource(drawableId);
    text.setText(name);
    tabHost.setTag(name);
    tabHost.addTab(spec
            .setIndicator(tabIndicator)
            .setContent(new Intent(this, c)
                    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
}

每次点击标签时,此Intent.FLAG_ACTIVITY_CLEAR_TOP都会刷新标签

并且为了存储价值,使用共享偏好或数据库,这完全取决于您的选择