如何执行操作DialogFragment正面按钮点击后 [英] How to execute action after DialogFragment positive button clicked

查看:186
本文介绍了如何执行操作DialogFragment正面按钮点击后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了下面的DialogFragment从Android文档导出它:

I created the following DialogFragment deriving it from the Android documentation:

公共类PayBillDialogFragment扩展DialogFragment {

public class PayBillDialogFragment extends DialogFragment{

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){

        final Bundle b = this.getArguments();
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("Paga bollettino")
               .setPositiveButton("Paga", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // FIRE ZE MISSILES!


                   }
               })
               .setNegativeButton("Cancella", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });
        // Create the AlertDialog object and return it
        return builder.create();

    }





}

从另一片段(一个ListFragment)中,当一个排列表的被点击DialogFragment应打开且pssing所述DialogFragment的正按钮我希望能够以除去ListFragment的选定行$ P $后和也调用一个方法来执行相关联的去除一个远程动作。 我实现了ListFragment如下:

From another Fragment (a ListFragment), when a row of the list is clicked the DialogFragment should be opened and after pressing the positive button of the DialogFragment I want to be able to remove the selected row of the ListFragment and also to call a method to perform a remote action associated to the removal. I implemented the ListFragment as follows:

public static class ListFragment extends android.support.v4.app.ListFragment {



        ArrayList<String> listItems=new ArrayList<String>();


        ArrayAdapter<String> adapter;


        public static final String ARG_SECTION_NUMBER = "section_number";

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            final View rootView = inflater.inflate(R.layout.list_fragment_view,
                    container, false);


            ListView lv = (ListView)rootView.findViewById(android.R.id.list);

            }});
            adapter=new ArrayAdapter<String>(this.getActivity(),
                    android.R.layout.simple_list_item_1,
                    listItems);
                setListAdapter(adapter);
            return rootView;
        }



        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {



            //opening the dialogfragment


        }


    }




    }

我不知道是怎么来后DialogFragment的正面按钮的点击处理动作。你能帮助我吗?

What I don't know is how to handle the action after the click of the positive button of the DialogFragment. Can you help me?

编辑:澄清,这是工作流程:单击列表 - >显示DialogFragment - 点击DialogFragment后>从列表中删除元素

to clarify, this is the workflow: click on the list -> show the DialogFragment -> after click on DialogFragment remove the element from the list.

推荐答案

这是我如何处理片段和对话片段之间的通信

This is how I handle communication between fragment and dialog fragment

示例片段:

public class MainFragment extends Fragment {

    private static final int REQ_CODE = 1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.main_fragment, container, false);
        Button b = (Button) v.findViewById(R.id.button);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                MyDialog dialog = new MyDialog();
                dialog.setTargetFragment(MainFragment.this, REQ_CODE);
                dialog.show(getFragmentManager(), "dialog");
            }
        });
        return v;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Toast.makeText(getActivity(), "Result: " + resultCode,
                Toast.LENGTH_SHORT).show();
        super.onActivityResult(requestCode, resultCode, data);
    }

}

例DialogFragment:

Example DialogFragment:

public class MyDialog extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("My dialog message")
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        notifyToTarget(Activity.RESULT_OK);
                    }
                })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                notifyToTarget(Activity.RESULT_CANCELED);
                            }
                        });
        return builder.create();
    }

    private void notifyToTarget(int code) {
        Fragment targetFragment = getTargetFragment();
        if (targetFragment != null) {
            targetFragment.onActivityResult(getTargetRequestCode(), code, null);
        }
    }

}

这是改变的方向,当我的工作也是唯一的方法。

This is the only method I got working also when changing orientation.

这篇关于如何执行操作DialogFragment正面按钮点击后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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