当前位置: 首页 > 知识库问答 >
问题:

android中片段/对话框之间的通信

上官树
2023-03-14

我有一个包含两个片段的活动:一个用于在网格视图中显示产品,另一个用于显示用户添加到订单中的产品(ListFragment)。当用户在网格视图中单击一个产品时,我需要的是显示一个对话框(DialogFragment),我在其中询问所需产品的数量。然后,当用户在对话框中单击Accept时,我希望产品出现在ListFragment中。

一方面,我必须将产品对象传递给对话框,以便将其名称显示为对话框的标题(例如)。所以我所做的就是这样传递:

public static class ProductDialog extends DialogFragment {

        static ProductDialog newInstance(ProductVO product) {
            ProductDialog f = new ProductDialog();

            Bundle args = new Bundle();
            args.putSerializable("product", product);
            f.setArguments(args);

            return f;
        }

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            ProductVO product = (ProductVO) getArguments().getSerializable("product");

            return new AlertDialog.Builder(getActivity())
                    .setIcon(R.drawable.ic_dialog_add)
                    .setTitle(R.string.add_product)

                    ...

                    .setPositiveButton(R.string.accept,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {

                            }
                        }
                    )
                    .setNegativeButton(R.string.cancel,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                            }
                        }
                    )
                    .create();
        }
    }

我想没关系,如果我错了,请纠正我。但是,在肯定按钮的onClick事件中,我必须检索对话框中引入的数量,并将其传递给另一个片段(ListFragment),此时它应该立即显示在列表中。

我怎么能那样做?

提前感谢

共有1个答案

韩朝斑
2023-03-14

推荐的方法是使用接口从DialogFraank到Active进行通信,然后从Active到Frach。

在您的活动中:

public class Main extends FragmentActivity implements OnQuantitySelectedListener {

    public interface OnQuantitySelectedListener {
        void onFinishEditDialog(String inputText);
    }


    @Override
    public void onFinishEditDialog(String inputText) {
        Toast.makeText(this, "Quantity: " + inputText, Toast.LENGTH_SHORT).show();
    }
}

然后是DialogFraank内部类

public static class ProductDialog extends DialogFragment {

    static ProductDialog newInstance(ProductVO product) {
        ProductDialog f = new ProductDialog();

        Bundle args = new Bundle();
        args.putSerializable("product", product);
        f.setArguments(args);

        return f;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        ProductVO product = (ProductVO) getArguments().getSerializable("product");

        LayoutInflater factory = LayoutInflater.from(getActivity());
        final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
        mEditText = (EditText) textEntryView.findViewById(R.id.txt_your_name);

        return new AlertDialog.Builder(getActivity())
                .setIcon(R.drawable.ic_dialog_add)
                .setTitle(R.string.add_product)
                .setView(textEntryView)
                .setPositiveButton(R.string.accept,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                             OnQuantitySelectedListener listener = (OnQuantitySelectedListener) getActivity();
                             listener.onFinishEditDialog(mEditText.getText().toString());
                        }
                    }
                )
                .setNegativeButton(R.string.cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                        }
                    }
                )
                .create();
    }
}

R.layout的XML。alert\u dialog\u text\u条目来自API演示。它不适合从用户获取数量的用例,但它说明了如何使用自定义布局从用户获取值。

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView 
        android:id="@+id/username_view"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:text="@string/alert_dialog_username"
        android:gravity="left"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/username_edit"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:scrollHorizontally="true"
        android:autoText="false"
        android:capitalize="none"
        android:gravity="fill_horizontal"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/password_view"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:text="@string/alert_dialog_password"
        android:gravity="left"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/password_edit"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:scrollHorizontally="true"
        android:autoText="false"
        android:capitalize="none"
        android:gravity="fill_horizontal"
        android:password="true"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>
 类似资料:
  • 我有一个片段和一个对话框片段,我想在它们之间进行通信:将数据从片段发送到对话框片段,并将数据从对话框片段发送到片段 我阅读了有关和的信息,但我不明白如何正确使用它们向/从发送/接收数据。 任何帮助?

  • 我遇到了一个如何在对话框片段中更新片段的问题。 当我单击过滤器菜单按钮时,会显示一个新的对话框片段,其中包括一个无线电组。 我想在单击ok按钮时更新包含位置列表的片段。 它是PlaceActive的代码,其中包含PlaceFraank: 公共类PlaceActive扩展AppCompatActive{ } 以下是PlaceFragment类的代码: 公共类PlaceFragment扩展了片段{ }

  • Hy,我是android开发的新手,我正在尝试在我的自定义对话框片段(扩展了DialogFragment并包含四个按钮)和view Pager中的片段之间建立一个通信。 我的视图分页器适配器扩展了FragmentStatePagerAdapter并包含7个选项卡,但对于每个选项卡,我创建了相同的片段,该片段具有一个recyclerView,但用于列表的数据不同。这个想法是,如果我单击对话框片段中的

  • 问题内容: 需要专家意见,我应该如何构造这个问题。我有一个自定义方法 process_filter ,它驻留在一个片段中,因为它需要访问私有和该片段。 在处理过程中,此片段将访问一个,并且在其中我需要使用back process_filter 方法 基本上,这里是结构: MyFragment.java MyAdapter.java 问题答案: 创建一个从适配器到片段的接口。 在适配器中创建接口,并

  • 我正试图做到这一点:http://android-er.blogspot.com/2012/06/communication-between-fragments-in.html只不过我用的是一台碎纸机 我有一个有两个片段的活动 FragmentA有一个编辑文本和一个按钮,FragmentB有一个文本视图 现在我想要的是,每当我在编辑文本中输入一些内容并单击按钮时,我的文本视图中就会出现一些内容。

  • 我有一个片段,它添加了一个选项到选项菜单。当单击此选项时,将打开一个对话框片段。对话框将原始片段设置为其目标片段。如果在对话片段打开时没有发生方向变化,则目标片段与预期的一样,但是在方向变化之后,目标片段被设置为对话片段本身,而不是先前设置的片段。结果,当试图将目标片段强制转换为被设置为目标片段的片段时,我得到了一个classCastException。我需要在对话框中获取目标片段,因为它实现了一