简体   繁体   English

单击不同的按钮时创建类似的对话框

[英]Create similar Dialog when clicking different buttons

Background: I have 4 buttons in the activity. 背景:我在活动中有4个按钮。

What I want to achieve: Clicking on each button displays the exact same Dialog with a list of 5 items. 我要实现的目标:单击每个按钮将显示完全相同的对话框,其中包含5个项目。 Clicking on an item in the list invokes a method (which is defined in the activity) that takes 2 parameters: 单击列表中的项目会调用一个方法(在活动中定义),该方法带有两个参数:

  1. a value indicating which of the 4 buttons was initially clicked 一个值,指示最初单击了四个按钮中的哪个
  2. a value indicating which of the 5 items in the list is chosen 指示选择列表中5个项目中的哪个项目的值

That is all I want to achieve. 这就是我想要实现的。 I think it is a pretty common scenario but I can't find any solution after searching for awhile. 我认为这是一个很常见的情况,但是经过一段时间的搜索后我找不到任何解决方案。

I am trying to follow the Android developer API guide: https://developer.android.com/guide/topics/ui/dialogs.html 我正在尝试遵循Android开发人员API指南: https : //developer.android.com/guide/topics/ui/dialogs.html

If I follow the guide, I will have something like this: 如果我按照指南进行操作,将会得到以下内容:

public class MyDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(R.string.dialog_title)
           .setItems(R.array.dialog_array, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
               // something
           }
    });
    return builder.create();
}
}

The problem is that I cannot find a way to pass a value indicating which button was clicked into the onClick method. 问题是我无法找到一种方法来传递一个值,该值指示在onClick方法中单击了哪个按钮。 Of course one way is to create 4 different DialogFragment, each for each button, but I believe that should be a way cleaner than this. 当然,一种方法是分别为每个按钮创建4个不同的DialogFragment,但我认为应该比这更干净。 My first thought is to create a DialogFragment Factory and return different anonymous DialogFragment for different inputs for the factory, but the compiler complains, stating "Fragments should be static", apparently because of some life cycle management issue. 我的第一个想法是创建一个DialogFragment工厂,并为工厂的不同输入返回不同的匿名DialogFragment,但是编译器抱怨说“碎片应该是静态的”,这显然是因为生命周期管理问题。 When I am searching around, I encounter some examples using onCreateDialog that I think can solve my problem, but apparently this method is deprecated... 当我四处搜索时,我遇到了一些使用onCreateDialog示例,我认为它们可以解决我的问题,但是显然不推荐使用此方法...

I have no idea now (except to create 4 different DialogFragment). 我现在不知道(除了创建4个不同的DialogFragment)。 Any help would be very much appreciated! 任何帮助将不胜感激!

You can do it with the opposite logic 你可以用相反的逻辑来做到

I have a similar stuff, and I solved it by using a static method which creates a dialog and an interface for retrieving the result: 我有一个类似的东西,我使用一个静态方法解决了这个问题,该方法创建了一个对话框和一个用于检索结果的接口:

This is my static method 这是我的静态方法

public static void showGenericItemSelectorDialog(Activity activity, ArrayList<MyParams> items, final IReturnMyListItemToParent iReturnMyListItemToParent) {

    final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setCancelable(true);

    LayoutInflater inflater = activity.getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.dialog_searchable_list, null); //or init it howerer you want, I have a custom layout
    builder.setView(dialogView);

    final Dialog dialog = builder.create();
    /// omitted not useful code

    Button myBtn= (Button) dialogView.findViewById(R.id.myBtn);
    myBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //check which item has been clicked
            iReturnMyListItemToParent.onResult(myClickedItem);
            dialog.dismiss();
        }
    });
    dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialogInterface) {
            iReturnMyListItemToParent.onResult(null);
            dialog.dismiss();
        }
    });

    dialog.show();
}

This is my interface 这是我的界面

public interface IReturnMyListItemToParent {
    void onResult(MyItem item);
}

This is how I call it: 这就是我所说的:

RuntimeHelper.showGenericItemSelectorDialog(getActivity(), myParams, new IReturnMyListItemToParent() {
                                    @Override
                                    public void onResult(MyItem result) {
                                      //do stuff with that result item clicked
                                    }
                                });

This is an example, but this way you can do whatever you want just by calling the static method from each button. 这是一个示例,但是通过这种方式,您只需通过从每个按钮调用static方法就可以执行所需的任何操作。

If you want to keep your logic: 如果您想保持逻辑:

This way I changed your "pass button to adapter" with passing result to button. 这样,我将您的“将按钮传递给适配器”以及将结果传递给按​​钮。

If you want to pass the button to the method, just add a parameter in the constructor that points you to the clicked button :) 如果要将按钮传递给方法,只需在构造函数中添加一个参数,即可将您指向单击的按钮:)

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM