从在多个选项卡编辑文本字段获取文本 [英] Get text from edit text fields that are in multiple tabs

查看:142
本文介绍了从在多个选项卡编辑文本字段获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建使用选项卡的输入形式的android应用。基本上我希望它设置,使用户可以在一个标签上输入一些信息,然后或者提交这些信息,或转到其他选项卡并输入信息,然后提交由两个标签中的信息。

I am trying to create an android application that uses tabs for an input form. Basically I want it set up so the user can enter some information on one tab, then either submit that information, or go to another tab and enter more information and then submit the information from both tabs.

我使用的操作栏和片段从一个活动显示我的标签。

I am using the action bar and fragments to display my tabs from one activity.

如果我在一个选项卡中输入文本,然后切换到另一个,然后切换回,文字仍然存在,但我似乎可以弄清楚如何抓住从当提交按钮当前显示的选项卡中的文本点击

If I enter text in one tab then switch to the other, then switch back, the text is still there, but I can seem to figure out how to grab the text from the tab that is not currently shown when the submit button is clicked.

是否有这样做的方法吗?

Is there any way of doing this?

确定继承人我的code为止。我是新来这里发帖遗憾。

Ok heres my code so far. I'm new to posting here sorry.

我的选项卡设置使用这样的:

My tabs are set up using this:

ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

//initiate tabs, set text and set listeners
Tab tab = bar.newTab();
tab.setText("Details");
tab.setTabListener(new TabListener<DetailsFragment>(this, "Details", DetailsFragment.class));
bar.addTab(tab);

tab = bar.newTab();
tab.setText("Comments");
tab.setTabListener(new TabListener<CommentsFragment>(this, "Details", CommentsFragment.class));
bar.addTab(tab);

我TabListener:

My TabListener:

private class TabListener<T extends Fragment> implements ActionBar.TabListener{
private Fragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;

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

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, attach it in order to show it
ft.attach(mFragment);
}
 }

我的点击监听器:

My on click listener:

            public void onClick(View v) {

            titleText = (EditText)findViewById(R.id.textTitle);
            authorText = (EditText)findViewById(R.id.textAuthor);
            seriesText = (EditText)findViewById(R.id.textSeries);
            pubText = (EditText)findViewById(R.id.textPublisher);
            isbnText = (EditText)findViewById(R.id.textISBN);
            commentsText = (EditText)findViewById(R.id.textComments);

            String title = titleText.getText().toString();
            String author = authorText.getText().toString(); 
            String series = seriesText.getText().toString(); 
            String pub = pubText.getText().toString(); 
            String isbn = isbnText.getText().toString();
            String comments = commentsText.getText().toString();

我的片段类看起来是这样的:

My fragment classes look like this:

public class DetailsFragment extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    return inflater.inflate(R.layout.detailsfragment, container, false);
}

}

和它们的布局的一个示例:

and an example of their layout:

<LinearLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/detailsTab"
   android:layout_width="match_parent" 
   android:layout_height="match_parent" 
   android:orientation="vertical"
   android:gravity="center_horizontal">

<EditText android:id="@+id/textTitle" 
    android:hint="@string/textTitle" 
    android:inputType="textCapWords" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:ems="10"
    android:layout_marginBottom="15dp"
    android:layout_marginTop="25dp">
    <requestFocus />
</EditText>

<EditText android:id="@+id/textAuthor" 
    android:hint="@string/textAuthor" 
    android:inputType="textCapWords" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:ems="10"
    android:layout_margin="15dp"/>

如果有你想看,我可以张贴以及任何其他code。

If there is any other code you want to see I can post that as well.

推荐答案

好了,所以我想出了一个方法来得到它的工作如何我想它。我怀疑它的最好办法做这件事的,但继承人什么我。

Ok so I figured out a way to get it to work how i want it to. I doubt its the best way of doing it but heres what I did.

在在TabListener我增加了一个功能之前,我卸下片段检查哪个片段将要分离,然后将数据保存到存储在活动变量。在onTabUnselected

In the onTabUnselected in the TabListener I added a function right before I detach the fragment to check which fragment is about to be detached and then save the data to variables stored in the activity.

    public void onTabUnselected(Tab tab, FragmentTransaction ft) 
    {
    if(mFragment != null)
    {
            saveData(mFragment);

            //Detach the fragment
            ft.detach(mFragment);
        }
    }

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

private void saveData(Fragment frag){
    if(frag.getTag() == "Details")
    {
        titleText = (EditText)findViewById(R.id.textTitle);
        title = titleText.getText().toString();
        authorText = (EditText)findViewById(R.id.textAuthor);
        author = authorText.getText().toString();
    }
    else if(frag.getTag() == "Comments")
    {
        commentsText = (EditText)findViewById(R.id.textComments);
        comments = commentsText.getText().toString();
    }
}

我仍然觉得自己必须有,因为当我在文本框选项卡之间切换数据保留这样做的更好的方法。

I still feel like there has to be a better way of doing this since the data is retained when i switch between tabs in the text boxes.

这篇关于从在多个选项卡编辑文本字段获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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