从 DialogFragment 返回值 [英] Return values from DialogFragment

查看:30
本文介绍了从 DialogFragment 返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行单击EditText 后需要显示对话框的任务.在那个对话框中,我使用 RecyclerView 显示带有 RadioButton 的内容.

现在,我想要做的是,从对话框中选择 RadioButton(RecyclerView 中的内容)后,它应该返回该内容的值,然后应该关闭对话框.

为了生成对话框,我使用了 DialogFragment.

由于我是 android 开发的新手,我完全困惑并且无法找到解决方案.

解决方案

因为你的对话框是一个 DialogFragment 你可以使用两个东西

<块引用>

  1. 如果您从 Activity 启动对话框,则可以使用接口

  • 创建接口

    公共接口 ISelectedData {void onSelectedData(String string);}

  • 在你的 Activity 中实现一个接口

    公共类 MyActivity 实现了 ISelectedData {.....@覆盖公共无效 onSelectedData(字符串 myValue){//使用返回值}}

  • 在您的对话框中,将界面附加到您的活动

    private ISelectedData mCallback;@覆盖public void onAttach(活动活动){super.onAttach(活动);尝试 {mCallback = (ISelectedData) 活动;}捕获(ClassCastException e){Log.d("MyDialog", "Activity 没有实现 ISelectedData 接口");}}

  • 将值返回给 Activity 时,只需在您的 Dialog 中调用

    mCallback.onSelectedData("myValue");

    查看 Android 开发者网站上的示例.

<块引用>

  1. 如果您从 Fragment 开始对话框,您可以使用 setTargetFragment(...)

  • 启动对话框

    MyDialog dialog = new MyDialog();dialog.setTargetFragment(this, Constants.DIALOG_REQUEST_CODE);dialog.show(fragmentManager, Constants.DIALOG);

  • 从对话框返回值

    Bundle bundle = new Bundle();bundle.putString(Constants.MY_VALUE, "MyValue");意图意图 = new Intent().putExtras(bundle);getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, 意图);解雇();

  • 获取片段中的值

    @Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (requestCode == Constants.DIALOG_REQUEST_CODE) {如果(结果代码 == Activity.RESULT_OK){如果 (data.getExtras().containsKey(Constants.MY_VALUE)) {String myValue = data.getExtras().getString(Constants.MY_VALUE);//使用返回值}}}}

I am doing task in which I need to show a dialog after clicking on EditText. In that dialog I show contents with RadioButtons using RecyclerView.

Now, what I want to do is, after selecting RadioButton (content which is in the RecyclerView) from dialog, it should return value of that content and then dialog should be dismissed.

To generate a dialog I've used a DialogFragment.

As I'm new in android development, I am totally confused and unable to find the solution.

解决方案

Because your dialog is a DialogFragment you can use two things

  1. If you are starting the dialog from a Activity, you can use an interface

  • create an interface

    public interface ISelectedData {
        void onSelectedData(String string);
    }
    

  • implement an interface in your Activity

    public class MyActivity implements ISelectedData {
    
        .....
    
        @Override 
        public void onSelectedData(String myValue) {
            // Use the returned value
        }
    }
    

  • in your Dialog, attach the interface to your Activity

    private ISelectedData mCallback;
    
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    
        try {
            mCallback = (ISelectedData) activity;
        }
        catch (ClassCastException e) {
            Log.d("MyDialog", "Activity doesn't implement the ISelectedData interface");
        }
    }
    

  • when returning the value to Activity, just call in your Dialog

    mCallback.onSelectedData("myValue");
    

    Check the example on the Android developer site.

  1. If you are starting the dialog from a Fragment, you can use setTargetFragment(...)

  • starting the Dialog

    MyDialog dialog = new MyDialog();
    dialog.setTargetFragment(this, Constants.DIALOG_REQUEST_CODE);
    dialog.show(fragmentManager, Constants.DIALOG);
    

  • returning the value from the Dialog

    Bundle bundle = new Bundle();
    bundle.putString(Constants.MY_VALUE, "MyValue");
    
    Intent intent = new Intent().putExtras(bundle);
    
    getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, intent);
    
    dismiss();
    

  • getting the value in Fragment

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        if (requestCode == Constants.DIALOG_REQUEST_CODE) {
    
            if (resultCode == Activity.RESULT_OK) {
    
                if (data.getExtras().containsKey(Constants.MY_VALUE)) {
    
                    String myValue = data.getExtras().getString(Constants.MY_VALUE);
    
                    // Use the returned value
                }
            }
        }
    }     
    

这篇关于从 DialogFragment 返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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