我有一个带有多个滑动标签片段的活动。每个片段都有一些复选框,编辑文本和切换字段。我想要的是浏览所有选项卡,编辑字段的状态,然后在最后一个选项卡上整理所有元素中的信息,并将这些细节保存到数据库中。
我的问题是我不知道如何保存这些元素值并将它们传递到我的上一个标签以便存储。
我是Android应用程序的新手,所以任何提示或答案都会非常感激。
以下是目前代码的显示方式:
活动:
public class IconTextTabsActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {
R.drawable.ic_tab_favourite,
R.drawable.ic_tab_call,
R.drawable.ic_tab_contacts,
R.drawable.ic_vector_test
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_icon_text_tabs);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
Bundle b = getIntent().getExtras();
String id = b.getString("id");
Toast.makeText(this, id, Toast.LENGTH_LONG).show();
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
tabLayout.getTabAt(3).setIcon(tabIcons[2]);
tabLayout.getTabAt(4).setIcon(tabIcons[3]);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new OneFragment(), "ONE");
adapter.addFrag(new TwoFragment(), "TWO");
adapter.addFrag(new ThreeFragment(), "THREE");
adapter.addFrag(new FourFragment(), "FOUR");
adapter.addFrag(new FiveFragment(), "FIVE");
viewPager.setAdapter(adapter);
}
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
标签片段1
public class OneFragment extends Fragment {
private EditText mBreedName;
public OneFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_one, container, false);
mBreedName = (EditText) v.findViewById(R.id.addName);
return v;
}
标签片段2
public class TwoFragment extends Fragment {
public TwoFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_two, container, false);
return v;
}
假设Tab Fragment 2是我的最后一个标签,我怎么能在这里检索&#34; mBreedName&#34;的文本值。存储在Tab Fragment 1中?
谢谢!
答案 0 :(得分:1)
您可以将值存储在活动中,并使用回调来更新和查询它们。
试试这个:
关于您的活动:
public class IconTextTabsActivity extends AppCompatActivity implements ActivityCallback{
private String mBreedTextName = "";
...
@Override
public void onEditTextChange(String text) {
this.mBreedTextName = text;
}
@Override
public String getEditTextName() {
return mBreedTextName;
}
}
ActivityCallback接口:
public interface ActivityCallback {
void onEditTextChange(String text);
String getEditTextName();
}
然后在你的片段上:
public class OneFragment extends Fragment {
...
private ActivityCallback callback;
@Override
public void onAttach(Context context) {
super.onAttach(context);
callback = (ActivityCallback) context;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_one, container, false);
mBreedName = (EditText) v.findViewById(R.id.addName);
Button b = v.findViewById(R.id.accept_button);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
callback.onEditTextChange(mBreedName.getText().toString());
}
});
return v;
}
}
第二个片段也一样:
public class TwoFragment extends Fragment {
...
private ActivityCallback callback;
private String text;
@Override
public void onAttach(Context context) {
super.onAttach(context);
callback = (ActivityCallback) context;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_two, container, false);
text = callback.getEditTextName();
return v;
}
}