如何接收通过在DialogFragment用户进行的操作? [英] How to receive the actions made by the user in a DialogFragment?

查看:192
本文介绍了如何接收通过在DialogFragment用户进行的操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示联系人列表一个ListView。当点击联系人,DialogFragment创建它显示就是否取消或确认的通知。根据他们是否单击取消或证实我想改变ListView项的颜色。我怎样才能按钮点击对话框的状态。我的code的对话框:

  @覆盖
    公共对话框onCreateDialog(捆绑savedInstanceState){
        //使用生成器类简单的对话框建设
        第一串= this.getArguments()的getString(第一)。
        。字符串最​​后= this.getArguments()的getString(最后);
        字符串手机= this.getArguments()的getString(手机)。        AlertDialog.Builder建设者=新AlertDialog.Builder(getActivity());        LayoutInflater吹气= getActivity()getLayoutInflater()。        查看查看= inflater.inflate(R.layout.add_contact_dialog,NULL);
        TextView的电视=(TextView中)view.findViewById(R.id.new_first);
        tv.setText(第一);        TextView中TV2 =(TextView中)view.findViewById(R.id.new_phone);
        tv2.setText(电话);        TextView的TV3 =(TextView中)view.findViewById(R.id.new_last);
        tv3.setText(最后);        builder.setView(视图);
        ?//builder.setTitle(\"Are你肯定)。setMessage(你愿​​意加入+姓名+作为联系人)?;        builder.setPositiveButton(确认,新DialogInterface.OnClickListener(){
                   公共无效的onClick(DialogInterface对话,诠释的id){
                       //添加联系人                   }
               })
               .setNegativeButton(拒绝,新DialogInterface.OnClickListener(){
                   公共无效的onClick(DialogInterface对话,诠释的id){
                       //用户取消了对话框
                   }
               });        //创建AlertDialog对象,并将其返回
        返回builder.create();
    }

现在我想setPositiveButton或setNegativeButton返回,我可以在我的主要活动有比较或让我在对话框的范围ArrayAdapter这样我可以更改它的值。有什么办法来关联 DialogFragment.show()并单击的对话框按钮?


解决方案

  

有什么办法来关联DialogFragment.show()和对话框
  按钮被点击?


使用一个接口来传递信息的活动从而将它传递给适配器/片段有做改变的可能性:

 公共接口OnSelectionMade {     INT OK = 1000;
     INT CANCEL = 2000;
     doChange(INT结果);
}

使活动实现此接口并使用 doChange()回调做在适配器的变化,无论是通过直接访问适配器(如果你使用一个简单的的ListView ),或者将它传递给片段持有的ListView 做的修改:

 公共类YourActivity扩展活动实现OnSelectionMade {     @覆盖
     公共无效doChange(INT结果){
         //结果参数将是要么确定或取消常数
         //你现在知道被点击哪个按钮所以你可以做的改变
         ...
     }}

现在你需要接线了 DialogFragment OnSelectionMade 这样传递的事件:

 私人OnSelectionMade mListener;@覆盖
公共无效onAttach(活动活动){
    super.onAttach(活动);
    mListener =(OnSelectionMade)活性; //你想在这里实现投检查是安全的
}//然后在onCreateDialog()方法
builder.setPositiveButton(确认,新DialogInterface.OnClickListener(){
                   公共无效的onClick(DialogInterface对话,诠释的id){
                       mListener.doChange(OnSelectionMade.OK); //做一个空mListener检查?
                   }
               })
               .setNegativeButton(拒绝,新DialogInterface.OnClickListener(){
                   公共无效的onClick(DialogInterface对话,诠释的id){
                       mListener.doChange(OnSelectionMade.CANCEL);
                   }
               });

您也许可以传递给一个参考 DialogFragment 直接传递的结果,但不是一个很好的解决方案,长期的。

I have a ListView that displays a list of Contacts. When a contact is clicked, a DialogFragment is created which displays a notice on whether or not to Cancel or Confirm. Based on whether they click Cancel or Confirm I want to change the color of the ListView item. How can I get the status of the Button click for the dialog. My code for the dialog:

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

Now I want setPositiveButton or setNegativeButton to return a value that I can compare with in my Main Activity or get my ArrayAdapter in the scope of the dialog so that I can make changes to it. Is there any way to relate the DialogFragment.show() and the Dialog button that was clicked?

解决方案

Is there any way to relate the DialogFragment.show() and the Dialog button that was clicked?

Use an interface to pass that information to the activity which in turn will pass it to the adapter/fragment that has the possibility to do the change:

public interface OnSelectionMade {

     int OK = 1000;
     int CANCEL = 2000;
     doChange(int result);
}

Make the activity implement this interface and use the doChange() callback to do the change in the adapter, either by accessing the adapter directly(if you use a simple ListView) or pass it to the fragment holding the ListView to do the change:

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
         ...
     }

}

Now you need to wire up the DialogFragment to pass the event through OnSelectionMade like this:

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

You could probably pass a reference to the DialogFragment directly to pass the results, but that is not a very good solution long term.

这篇关于如何接收通过在DialogFragment用户进行的操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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