如何在自定义对话框类中使用 getActivity.getLayoutInflater()

Posted

技术标签:

【中文标题】如何在自定义对话框类中使用 getActivity.getLayoutInflater()【英文标题】:How to use getActivity.getLayoutInflater() in custom dialog class 【发布时间】:2020-04-10 18:16:15 【问题描述】:

我为自定义对话框创建了一个类,并使用了 PrettyDialog。我想要一个对话框上的编辑文本。我使用 Inflater 但错误:getActivity -> mHost:null。我尝试创建 getTextDialogFragment() 方法,然后 getLayoutInflater 错误 mHost null。我能做些什么来解决这个问题? 文本对话框片段:

 public class TextDialogFragment extends DialogFragment 
        private EditText edtDialog;
        public interface SingleChoiceListener
            void onPositiveButtonClicked();
            void onNegativeButtonClicked();
        
        TextDialogFragment.SingleChoiceListener mListener;


        @NonNull
        public PrettyDialog onCreateTextDialog(Context context) 
            PrettyDialog prettyDialog = new PrettyDialog(context);
            LayoutInflater inflater=getActivity().getLayoutInflater();
            View view=inflater.inflate(R.layout.dialog_text,null);
            prettyDialog.setContentView(view);
            prettyDialog.setMessage("sdf");
            prettyDialog.setIcon(R.drawable.question_icon);
            prettyDialog.setCanceledOnTouchOutside(false);
            prettyDialog.addButton("EVET", R.color.pdlg_color_white, R.color.pdlg_color_green, new PrettyDialogCallback() 
                @Override
                public void onClick() 
                    mListener.onPositiveButtonClicked();
                    prettyDialog.dismiss();
                
            );
            prettyDialog.addButton("HAYIR", R.color.pdlg_color_white, R.color.pdlg_color_red, new PrettyDialogCallback() 
                @Override
                public void onClick() 
                    mListener.onNegativeButtonClicked();
                    prettyDialog.dismiss();
                
            );
            edtDialog=view.findViewById(R.id.edtDialog);
            prettyDialog.show();
            return prettyDialog;
        
        public void setListener(TextDialogFragment.SingleChoiceListener singleChoiceListener)
            mListener=singleChoiceListener;
        
    

我想添加到对话框的这个布局。对话框必须工作edittext+prettydialog:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_ android:layout_
    android:layout_margin="8dp">
    <EditText
        android:id="@+id/edtDialog"
        android:layout_
        android:layout_
        android:background="@null"
        android:focusable="true"
        android:hint="Giriş yapınız"
        android:gravity="center"
        android:imeOptions="actionDone"
        android:inputType="text"
        android:textColor="@android:color/black"
        android:textColorHint="@android:color/darker_gray"
        android:textSize="40sp">

    </EditText>
</RelativeLayout>

【问题讨论】:

onCreateTextDialog() 方法中的所有内容移至DialogFragmentonCreateDialog() 方法。您可以使用getActivity() 代替您的Context 参数。我不太确定onCreateTextDialog() 的用途是什么,但这有点表明您可能没有正确使用DialogFragment @MikeM。谢谢你的支持。我使用 BaseNavigator 所以我必须使用上下文参数。我创建 oncreatedialog 但我不能使用上下文 我不知道BaseNavigator 是什么,也不知道为什么它会要求你在那里传递你自己的Context,但就目前而言,你的TextDialogFragment 并没有真正被用作DialogFragment。如果你 show() 是从 onCreateTextDialog() 返回的 PrettyDialog,那么你就得到了一个普通的旧 Dialog。相反,如果您尝试 show() 一个 TextDialogFragment 的实例,它将是一个空的 Dialog,而不是您在该方法中设置的 PrettyDialog 【参考方案1】:

试试下面的代码

View view=LayoutInflater.from(context).inflate(R.layout.dialog_text,null);

代替

LayoutInflater inflater=getActivity().getLayoutInflater();
View view=inflater.inflate(R.layout.dialog_text,null);

希望对你有帮助!

谢谢。

【讨论】:

谢谢。但是这种方式打开布局没有对话框:/我想在 prettydialog 上打开布局 这个视图充气器是你的对话框。所以,我不明白没有对话框如何打开布局? 你能分享屏幕截图吗? 我的对话框使用此方法“onCreateTextDialog”创建,并添加了带有布局的edittext。 (查看充气机将此布局添加到 prettydialog)我用布局更新帖子

以上是关于如何在自定义对话框类中使用 getActivity.getLayoutInflater()的主要内容,如果未能解决你的问题,请参考以下文章

如何在自定义视图类中使用 soundPool? [关闭]

如何在自定义类中调用“SetDlgItemText”?

如何在自定义类中隐藏迭代器和容器实现(无增强)

如何在自定义视图类中链接 onSizeChanged()、onMeasure()、onDraw()

如何让 .xib 出现在自定义类中

在自定义适配器类中发送服务器请求后,如何更改 Listview 按钮名称?