如何将 Context 和 AlertDialog 与片段一起使用

How to use Context and AlertDialog with fragments

所以我试图在片段中使用 EditText 实现警报对话框,代码如下: 1.prompt_dialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Type your reminder title : "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editTextDialogUserInput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

2.MainFragment.java

public class ReminderFragment extends Fragment implements View.OnClickListener {

        final Context context = this;

        public ReminderFragment() {
                    // Required empty public constructor
            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

                View layout = inflater.inflate(R.layout.main_fragment_layout,container,false);

                Button openDialog = (Button) layout.findViewById(R.id.openDialogButton);
                openDialog .setOnClickListener(this);


                return layout;
            }
        public void onClick(View view){

                LayoutInflater li = LayoutInflater.from(context);
                View promptsView = li.inflate(R.layout.prompt_dialog, null);

                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    context);

                // set prompts.xml to alertdialog builder
                alertDialogBuilder.setView(promptsView);
                // set dialog message
                alertDialogBuilder
                        .setCancelable(false)
                        .setPositiveButton("Ok",
                            new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {

                            }
                        })
                        .setNegativeButton("Cancel",
                            new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {
                                dialog.cancel();
                            }
                        });
                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();
            }
    }

但是在上下文变量 final Context context = this; 上有一个错误说

Incompatible Types:
        Required: android.content.Context
        Found: com.example.user.packgename.MainFragment

我尝试在扩展 Activity class 的另一个 class 上使用上下文并使用此代码 final Context context = this;它工作 很好,我的情况可能是因为它从 Fragment class.

延伸而来

那么我该如何解决这个问题?还有其他方法吗?

谢谢。

替换 context 的所有用法并使用 getActivity() 代替它。

Android 应用程序的所有基本四个组件都扩展了 Context,其中包括 - ActivityServiceBroadcastReceiverContentProvider .应用程序本身也扩展 Context.

要执行与 UI 相关的操作,您需要使用 ActivityContext,例如显示 AlertDialog

final Context context = this;

只要写在上面列出的任何组件的范围内,此行都是有效的。它不会在 Fragment 中工作,因为它不扩展自己的 Context,它使用父 Activity 的上下文。