Android 在片段中使用带有自定义布局的 AlertDialog

Android using AlertDialog with custom layout in fragment

晚上好,

我正在尝试在片段中使用警告对话框(TabNavigation 的原因)。我必须使用 "privacy" 布局。

但是 eclipse 在 "AlertDialog.Builder" 处给了我一个错误:(构造函数 AlertDialog.Builder(AboutActivity2) 未定义)

并且在inflate之后的“.from”处:(LayoutInflater类型中的方法from(Context)不适用于参数(AboutActivity2))

感谢您的帮助, 问候

    View rootView = inflater.inflate(R.layout.activity_about2, container, false);  
    rootView.findViewById(R.id.privacybutton).setOnClickListener(this);
    return rootView;
}

final OnClickListener mGlobal_OnClickListener = new OnClickListener() {
    public void onClick(View v) {
        switch(v.getId()) {
        case R.id.privacybutton:
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
            LayoutInflater factory = LayoutInflater.from(getActivity());
            final View view = factory.inflate(R.layout.privacy, null);
            alertDialog.setView(view);
            alertDialog.setNegativeButton("Schließen", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                dialog.dismiss();
                        }
                    });
            alertDialog.show();
            break;
    }
}

AlertDialog.Builder 接收上下文作为参数。而且不是片段。

http://developer.android.com/reference/android/app/AlertDialog.Builder.html#AlertDialog.Builder(android.content.Context)

改为使用 getActivity() :

    AlertDialog.Builder alertDialog3 = new AlertDialog.Builder(getActivity());
    LayoutInflater factory3 = LayoutInflater.from(getActivity());

您还需要将侦听器添加到您的按钮。你可以这样做:

View rootView = inflater.inflate(R.layout.activity_about2, container, false);  
rootView.findViewByID(R.id.privacybutton).setOnClickListener(this);
return rootView;

最终代码

    View rootView = inflater.inflate(R.layout.activity_about2, container, false);
    rootView.findViewById(R.id.privacybutton).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(v.getContext());
            LayoutInflater factory = LayoutInflater.from(v.getContext());
            final View view = factory.inflate(R.layout.privacy, null);
            alertDialog.setView(view);
            alertDialog.setNegativeButton("Schließen", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            alertDialog.show();
        }
    });
    return rootView;

您可以使用普通对话框来显示警报。 Alert dialog也派生自dialog,你只需要为视图写一个自定义的XML并将视图设置为dialog即可。

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp" />

        <TextView
            android:id="@+id/text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#FFF" 
            android:layout_toRightOf="@+id/image"/>/>

         <Button
            android:id="@+id/dialogButtonOK"
            android:layout_width="100px"
            android:layout_height="wrap_content"
            android:text=" Ok "
            android:layout_marginTop="5dp"
            android:layout_marginRight="5dp"
            android:layout_below="@+id/image"
            />

    </RelativeLayout>





                // custom dialog
                final Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.custom);
                dialog.setTitle("Title...");

                // set the custom dialog components - text, image and button
                TextView text = (TextView) dialog.findViewById(R.id.text);
                text.setText("Android custom dialog example!");
                ImageView image = (ImageView) dialog.findViewById(R.id.image);
                image.setImageResource(R.drawable.ic_launcher);

                Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
                // if button is clicked, close the custom dialog
                dialogButton.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

                dialog.show();

在我看来,问题是关于上下文的,它与片段有关。 尝试获取应用程序的上下文而不是 class (AboutActivity2.this)

的上下文
AlertDialog.Builder alertDialog3 = new AlertDialog.Builder(getActivity());

 AlertDialog.Builder alertDialog3 = new AlertDialog.Builder(v.getContext());

请试一试,看看是否有效。

your_dialouge_fragment dppr = new your_dialouge_fragment();
dppr.show(getActivity().getSupportFragmentManager(),"mmtag");

这对我有用。

最好选择 DialogFragment 而不是 AlertDialog 或 Dialog,因为 DialogFragment 易于实现,并且它有自己的生命周期方法,可用于处理其他事件或数据处理。 看到这个 Google link -

https://developer.android.com/guide/fragments/dialogs