android-fragments 在片段中打开对话框片段

iyfjxgzm  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(246)

我在一些文本中有DialogFragment,我想在Fragment中打开它
对话片段第一

public class subscribe_dialog_frag extends DialogFragment {

    @Override
    @Nullable
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
        super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.subscribe_dialog_fragment, container, false);

    }
}

这是我的代码片段

public class Main4Fragment extends Fragment {

    private ImageButton btn_money;


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

btn_money = (ImageButton)view.findViewById(R.id.btn_momey);

        btn_money.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Fragment subscribe_dialog_frag = new subscribe_dialog_frag();
                FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
                transaction.replace(R.id.btn_momey, subscribe_dialog_frag).commit();
            }
        });

        return view;
    }
}

我只是打开了dialogFragment。
可能吗??还是我做另一种方式?

osh3o9ms

osh3o9ms1#

首先对类/变量使用命名约定...将对话框重命名为:

public class SubscribeDialog extends DialogFragment {

    @Override
    @Nullable
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
        super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.subscribe_dialog_fragment, container, false);

    }
}

在Main 4Fragment调用中,类似于以下内容:

btn_money.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
    SubscribeDialog subscribeDialog = SubscribeDialog();
    subscribeDialog.show(getChildFragmentManager(),"SubscribeDialog");
   }
});

相关问题