【问题标题】:How to receive the actions made by the user in a DialogFragment?如何接收用户在 DialogFragment 中所做的操作?
【发布时间】:2014-06-04 07:08:54
【问题描述】:

我有一个显示联系人列表的 ListView。单击联系人时,会创建一个 DialogFragment,其中显示有关是否取消或确认的通知。根据他们是单击取消还是确认,我想更改 ListView 项的颜色。如何获取对话框的按钮单击状态。我的对话框代码:

@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        String first = this.getArguments().getString("first");
        String last = this.getArguments().getString("last");
        String phone = this.getArguments().getString("phone");

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();

        View view = inflater.inflate(R.layout.add_contact_dialog, null);
        TextView tv = (TextView) view.findViewById(R.id.new_first);
        tv.setText(first);

        TextView tv2 = (TextView) view.findViewById(R.id.new_phone);
        tv2.setText(phone);

        TextView tv3 = (TextView) view.findViewById(R.id.new_last);
        tv3.setText(last);

        builder.setView(view);
        //builder.setTitle("Are you sure?").setMessage("Would you like to add " + name + " as a contact?");

        builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // Add the contact

                   }
               })
               .setNegativeButton("Deny", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });

        // Create the AlertDialog object and return it
        return builder.create();
    }

现在我希望 setPositiveButton 或 setNegativeButton 返回一个值,我可以在我的主活动中进行比较,或者让我的 ArrayAdapter 在对话框的范围内,以便我可以对其进行更改。有什么方法可以将DialogFragment.show() 和被点击的对话框按钮联系起来?

【问题讨论】:

    标签: android android-fragments android-dialogfragment


    【解决方案1】:

    有什么方法可以关联 DialogFragment.show() 和 Dialog 被点击的按钮?

    使用接口将该信息传递给活动,该活动又将其传递给有可能进行更改的适配器/片段:

    public interface OnSelectionMade {
    
         int OK = 1000;
         int CANCEL = 2000;
         doChange(int result);
    }
    

    使活动实现此接口并使用doChange() 回调在适配器中进行更改,方法是直接访问适配器(如果您使用简单的ListView)或将其传递给持有@987654324 的片段@进行更改:

    public class YourActivity extends Activity implements OnSelectionMade {
    
         @Override
         public void doChange(int result) {
             // the result parameter will be either the OK or CANCEL constants
             // you know now what button was clicked so you can do the change
             ...
         }
    
    }
    

    现在您需要连接DialogFragment 以通过OnSelectionMade 传递事件,如下所示:

    private OnSelectionMade mListener;
    
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mListener = (OnSelectionMade) activity; // you'd want to implement a cast check here to be safe
    }
    
    //then in the onCreateDialog() method
    builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           mListener.doChange(OnSelectionMade.OK); // do a null mListener check?
                       }
                   })
                   .setNegativeButton("Deny", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           mListener.doChange(OnSelectionMade.CANCEL);
                       }
                   });
    

    您可能可以直接传递对 DialogFragment 的引用来传递结果,但从长远来看,这不是一个很好的解决方案。

    【讨论】:

      【解决方案2】:

      确实有。在 ActivityFragment 中实例化 DialogFragment,首先声明:

      public static final int MY_DIALOGFRAGMENT = 123;   /* Any random int values will do. */
      public static final int CONTACT_CONFIRM = 124;
      public static final int CONTACT_DENY = 125;
      

      像这样实例化你的DialogFragment

      FragmentManager fm = getSupportFragmentManager();
      MyDialogFragment myDialogFragment = new MyDialogFragment();
      myDialogFragment.setTargetFragment(this, MY_DIALOGFRAGMENT);
      myDialogFragment.show(fm, "my_dialog_fragment");
      

      DialogFragmentonCreateDialog() 方法中,

      builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface dialog, int id) {
                         // Add the contact
                         getTargetFragment().onActivityResult(getTargetRequestCode(), 
                             MyActivity.CONTACT_CONFIRM, getActivity().getIntent());
                         dismiss();
                     }
                 })
                 .setNegativeButton("Deny", new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface dialog, int id) {
                         // User cancelled the dialog
                         getTargetFragment().onActivityResult(getTargetRequestCode(), 
                             MyActivity.CONTACT_DENY, getActivity().getIntent());
                         dismiss();
                     }
                 });
      

      最后,将以下方法添加到您的ActivityFragment

      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
              switch(requestCode) {
                  case MY_DIALOGFRAGMENT:
      
                      if (resultCode == CONTACT_CONFIRM) {
      
                          // Change color of listview item to green
      
                      } else if (resultCode == CONTACT_DENY){
      
                          // Change color of listview item to red
      
                      }
      
                      break;
              }
      }
      

      这应该可以。试试看。

      【讨论】:

      • 是 DialogFragment 或 MainActivity 中的 onActivityResult?因为它似乎在没有范围访问 ListView 的 Dialog 类中
      • onActivityResult() 位于ActivityFragment 中,您将从那里调用DialogFragment。您当然可以从那里访问ListView。三个public static int 字段也应该放在添加onActivityResult() 的同一个类中。试试看,如果你得到你想要的,请告诉我。
      猜你喜欢
      • 2019-03-29
      • 2013-06-23
      • 2018-08-23
      • 1970-01-01
      • 2022-07-20
      • 2012-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多