android - 从多个选项卡中的编辑文本字段获取文本

标签 android

我正在尝试创建一个使用选项卡作为输入表单的 Android 应用程序。基本上,我希望将其设置为用户可以在一个选项卡上输入一些信息,然后提交该信息,或者转到另一个选项卡并输入更多信息,然后从两个选项卡提交信息。

我正在使用操作栏和 fragment 来显示我在一个 Activity 中的选项卡。

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

有什么办法吗?

好的,这是我目前的代码。我刚开始在这里发帖,抱歉。

我的标签是使用这个设置的:

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:

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);
}
 }

我的点击监听器:

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

我的 fragment 类如下所示:

public class DetailsFragment extends Fragment{

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

及其布局示例:

<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"/>

如果您想查看任何其他代码,我也可以发布。

最佳答案

好的,所以我想出了一个方法让它按照我想要的方式工作。我怀疑这是最好的方法,但这是我所做的。

在 TabListener 的 onTabUnselected 中,我在分离 fragment 之前添加了一个函数来检查哪个 fragment 将被分离,然后将数据保存到存储在 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();
    }
}

我仍然觉得必须有更好的方法来做到这一点,因为当我在文本框中的选项卡之间切换时,数据会保留下来。

关于android - 从多个选项卡中的编辑文本字段获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13621831/

相关文章:

javascript - 如何在 Cordova 项目上重新加载页面?

android - 关于 Activity 生命周期和资源删除/分配的问题

java - Android 无法打开现有的 SQLite 数据库文件

android - 如何在 Intent 中传递 uuid?

添加新库时启用 Android proguard 的 APK 大小会增加

android - AccountManager 无法在 Sony XZ 7.1.1 上添加帐户

java - 在android中用java创建sqlite3表

android - 如何清除所有WebView存储的信息?

android - 使用相机拍照并保存到图库

android - 最佳实践 : res folders - Xdpi vs swXXXdp