【问题标题】:Passing a Layout and it's contents between Activities在活动之间传递布局及其内容
【发布时间】:2016-05-03 18:24:15
【问题描述】:

所以完全承认,当谈到 Android 开发时,我有点自学,所以我可能把这一切都搞错了。因此,我愿意接受建议!我实际上是在尝试将我目前每天执行的任务半自动化。

问题:如何在Activity之间传递一个LinearLayout,它的内容是完整的?

所以我有第二个活动,称为reportGeneratorActivity 在此活动中,报表预览正下方有一个线性布局。

线性布局本身在单独的 XML 文件中定义为 previewplate.xml

现在这个 Activity 起作用了,当你把文本放到上面的字段中时,它会更新下面的预览。这把我带到了我正在撞的砖墙上。目标是获取该预览板并将其添加到我在白色区域中命名为 rootActivity 的主要活动中,该区域本身是一个名为 rootWorkingLayout 的线性布局。

现在文本中的字符串都临时存储在 reportGeneratorActivity 中,此时按下按钮时我正在执行此操作:

 public void beginReport (View view) {
    //Bundle the Preview
    Bundle previewBundle = new Bundle();//Create the Bundle
    previewBundle.putString("date", dateHolder);
    previewBundle.putString("client", clientNameHolder);
    previewBundle.putString("machine", machineTypeHolder);
    previewBundle.putString("serial#", serialNumberHolder);
    previewBundle.putString("notes", notesHolder);
    // Prepare The Intent
    Intent previewPasser = new Intent(this, rootActivity.class);
    previewPasser.putExtras(previewBundle); // Add the Bundle to Intent
    //Send Preview to Root
    startActivity(previewPasser);
    //Send Preview to History
    //Send User to Decision Tree
    }

据我了解,我已将所有字符串放入 Bundle previewBundle 中,然后将 bundle 附加到 previewPasser Intent 并将 Intent 发送回 rootActivity。

在 rootActivity 的 onCreate 函数中,我放置了以下代码:

    Bundle previewReceiver = getIntent().getExtras();
    //If There is a Bundle, Process it
    if(previewReceiver != null) {
        newPreview(previewReceiver);
    }

这里的目标是获取 Intent,然后获取捆绑包,然后将其传递给我的 newPreview 函数(当前为空),该函数将从 report_generator_activity 复制完成的预览,并以相同的方式在线性布局中显示:rootWorkinglayout。

这是我碰壁的最后一步,我只能假设有一种更简单的方法,也许是一种复制布局及其内容并将其发送过来的方法?或者如果我在功能上这样做,我如何以相同的方式解包数据?

由于我是社区的新成员,请原谅我的冗长和缺少图像。

编辑#1: 为了响应 SoroushA 让我走上正确道路的出色回答,我将我的 newPreview 方法调整为:

public void newPreview (Bundle previewReceiver) {
    //Extract Strings from Bundle
    String date = previewReceiver.getString("date");
    String client = previewReceiver.getString("client");
    String machine =previewReceiver.getString("machine");
    String serialNum = previewReceiver.getString("serial#");
    //Create New Inflater
    LayoutInflater previewInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View previewLayout = previewInflater.inflate(R.layout.previewplate, null);
    //Add previewLayout to rootWorkingLayout
    rootWorkingLayout.addView(previewLayout);
}

目前,我只是想让预览版布局的灰色框出现,因为它的背景是在它自己的 XML 文件中定义的。但是,由于我自己的错误,当我清楚地完成该过程时,什么都没有发生。我不确定我错过了什么步骤。

提前致谢!

【问题讨论】:

  • 您应该发送每个容器的值。为了轻松创建自定义视图,以便在两个活动中使用相同的视图。此外,您不应该将大量数据放入捆绑包中。有些手机没有很多内存

标签: java android android-layout android-studio android-intent


【解决方案1】:

我希望我清楚地理解了你的问题。

在你的 newPreview 方法中,首先从 Bundle 中取回字符串:

public void newPreview(Bundle preview){
String dateHolder = preview.getString("date"); 
//similarly for other strings
}

然后给 LinearLayout 充气并开始设置它的元素。

LayoutInflater inflater = (LayoutInflater)context.getSystemService
  (Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.your_xml, null);
//call findviewbyid on the layout and set its children using the strings you extracted

【讨论】:

  • 谢谢 SoroushA,我想我明白这里的方向了。然而,我认为我的自我训练中的一个差距让我错过了你的答案。我可以确认捆绑包正在传递字符串,但仍未在 rootWorkingLayout 中创建布局。您介意扩大答案的后半部分吗?谢谢!
  • 任何布局都必须先膨胀后才能使用。通常,活动使用 setContentView(activity_layout) 来扩展布局。但是,如果您创建了一个新活动,并使用 setContentView 给它一个布局,然后尝试打开另一个 xml 文件并使用它,您必须先使用 LayoutInflater 手动对其进行充气。在我膨胀 your_xml 并获得视图布局后的回答中,您可以调用 layout.findViewById(R.id.some_child) 来获取其中的 TextViews 和其他视图。见here
  • 我想我明白了,但为了确保我关注我会在上面添加评论,我可以在其中添加代码。
  • 看起来不错。在将 previewLayout 的元素添加到 rootWorkingLayout 之前,不要忘记使用字符串 setText。另外,如果对您有帮助,请考虑接受答案。
  • 确实有帮助!我仍然无法让 previewLayout 出现在 rootWorkingLayout 上。我错过了一步吗?
【解决方案2】:

如何在活动之间传递一个LinearLayout,它的内容是完整的?

你没有。小部件和容器(即View 的子类)归其活动所有。

我只能假设有一个更简单的方法

只有一个活动,根据需要更改其 UI(例如,使用片段并根据需要替换它们)。这些似乎过于紧密地耦合在一起,无法成为两个独立的活动。

这里的目标是获取 Intent,然后获取捆绑包,然后将其传递给我的 newPreview 函数(当前为空),该函数将从 report_generator_activity 复制完成的预览,并以相同的方式在线性布局中显示:rootWorkinglayout。

这种方法本质上没有任何问题。你表现得好像你在实施它时遇到了问题(“我如何以相同的方式解包数据”),但我们没有足够的信息来为你提供很多建议。通常,Bundle 具有 getter 方法,用于检索您通过 setter 方法放入 Bundle 的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多