在Android的碎片/对话框之间的通信 [英] Communication between fragments/dialogs in android

查看:184
本文介绍了在Android的碎片/对话框之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个片段的活性:一个用于表示产品在一个gridview和另一个显示用户添加到顺序(ListFragment)的产品。当用户单击网格视图一个产品,我需要的是显示一个对话框(DialogFragment),其中我问产品的数量通缉。然后,当用户点击接受的对话框中,我希望产品出现在ListFragment。

I have an activity with two fragments: one for showing products in a gridview and the other to show the products that the user adds to the order (ListFragment). When the user clicks a product in the grid view, what I need is to display a dialog (DialogFragment) in which I ask the quantity of product wanted. Then, when the user click accept in the dialog, I want the product to appear in the ListFragment.

在一方面,我有对象产品传递给对话框,以显示它的名称作为对话框的标题(例如)。所以我所做的就是通过这种方式:

On one hand, I have to pass the object product to the dialog in order to show it's name as the dialog's title (for example). So what I did was to pass it this way:

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),然后将其应显示在列表中即刻

I think that it is okey, correct me if I'm wrong. But then, in the onClick event of the positive button I have to retrieve the quantity introduced in the dialog, and then pass it to the other fragment (the ListFragment), and then it should be displayed in the list instantly.

我怎么能这样做呢?

在此先感谢

推荐答案

href="http://android-developers.blogspot.com/2012/05/using-dialogfragments.html">推荐方法的是从DialogFragment到活动使用接口进行通信,然后从活动到片段

The recommended approach is to communicate from the DialogFragment to the Activity using an Interface, and then from the Activity to the Fragment.

在你的活动:

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

然后DialogFragment内部类

Then the DialogFragment inner class

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

中的XML的R.layout.alert_dialog_text_entry是从<一个href="http://developer.android.com/resources/samples/ApiDemos/res/layout/alert_dialog_text_entry.html">API演示的。它不适合你的使用情况下,从用户使用量,但它说明了使用自定义布局,以获取用户的值。

The XML for R.layout.alert_dialog_text_entry is from the API Demos. It doesn't fit your use case of getting a quantity from the user, but it illustrates using a custom layout to get a value from the user.

<?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>

这篇关于在Android的碎片/对话框之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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