不要在 DialogFragment 上再次显示复选框

Do not show again checkbox on a DialogFragment

所以标题说明了一切。我有一个当前弹出的 dialogfragment,我想向它添加一个不显示复选框,然后显然执行该检查并且如果它被选中则不显示。我知道有一个 .setSingleChoiceItems,但我不完全确定那里会发生什么,因为它不是我要添加到某处的真正项目。但话又说回来,我可能是错的,因为我刚刚进入 Android 开发。

对话框片段java

public class WifivsDataDialog extends DialogFragment {


     @
     Override
     public Dialog onCreateDialog(Bundle savedInstanceState) {
         // Use the Builder class for convenient dialog construction
         AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
         builder.setMessage(R.string.dialog_box)
             .setPositiveButton(R.string.WiFi, new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int id) {
                     // FIRE ZE MISSILES!

                 }
             })
             .setNegativeButton(R.string.Cell_Data, new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int id) {
                     // User cancelled the dialog
                 }
             });
         // Create the AlertDialog object and return it
         return builder.create();
     }
 }

这是在我的 MainActivity.java

中调用它的代码
 WifivsDataDialog myDiag = new WifivsDataDialog();
    myDiag.show(getFragmentManager(), "dialog_layout");
    myDiag.setCancelable(false);

DialogFragmentclass

    public class WifivsDataDialog extends DialogFragment implements View.OnClickListener {
    private CheckBox checkBox;
    private Button button1;
    private Button button2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View mainView = inflater.inflate(R.layout.wifi_dialog, container);
        checkBox = (CheckBox) mainView.findViewById(R.id.checkBox);
        button1 = (Button) mainView.findViewById(R.id.button1);
        button2 = (Button) mainView.findViewById(R.id.button2);

        button1.setOnClickListener(this);
        button2.setOnClickListener(this);

        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                // Store the isChecked to Preference here
                SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putBoolean("DONT_SHOW_DIALOG", isChecked);
                editor.commit();

            }
        });
        return mainView;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button1:
                // Do wifi stuff here
                break;
            case R.id.button2:
                // Do cellular stuff here
                break;
        }
    }
}

布局xmlwifi_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:padding="20dp"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Some information to be displayed to human"
            android:id="@+id/textView" android:textSize="30dp"/>
    <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Do now show this dialog again."
            android:id="@+id/checkBox"/>
    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:gravity="center">
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="WiFi"
                android:id="@+id/button1" android:layout_gravity="center_horizontal"/>
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="CELL"
                android:id="@+id/button2"/>
    </LinearLayout>
</LinearLayout>

并且在您的 Activity 或任何显示对话框的地方,在显示对话框之前检查用户设置的首选项。如下所示。

SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
        boolean dontShowDialog = sharedPref.getBoolean("DONT_SHOW_DIALOG", false);
        if (!dontShowDialog) {
            new WifivsDataDialog().show(getFragmentManager(), "WiFi");
        }

接受的答案有效,但它看起来不像正常的 material 设计系统对话框,这正是我想要的。

这里有一个选项,使用自定义视图布局来仅包含文本和复选框(具有匹配 material 设计的填充),并允许 AlertDialog 管理按钮。

no_location_dialog.xml 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="24dp"
    android:paddingRight="12dp">

    <TextView
        android:id="@+id/no_location_text"
        style="?android:attr/textAppearance"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="14dp"
        android:paddingBottom="6dp"
        android:text="@string/main_nolocation"
        android:textSize="16sp"
        android:autoLink="all"
        android:linksClickable="true"></TextView>

    <CheckBox
        android:id="@+id/location_never_ask_again"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/main_never_ask_again" />
</LinearLayout>

以及设置 AlertDialog 的代码:

private Dialog createNoLocationDialog() {
    View view = getActivity().getLayoutInflater().inflate(R.layout.no_location_dialog, null);
    CheckBox neverShowDialog = (CheckBox) view.findViewById(R.id.location_never_ask_again);

    neverShowDialog.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
        // Save the preference
        PreferenceUtils.saveBoolean(getString(R.string.preference_key_never_show_location_dialog), isChecked);
        }
    });

    Drawable icon = getResources().getDrawable(android.R.drawable.ic_dialog_map);
    DrawableCompat.setTint(icon, getResources().getColor(R.color.theme_primary));

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
        .setTitle(R.string.main_nolocation_title)
        .setIcon(icon)
        .setCancelable(false)
        .setView(view)
        .setPositiveButton(R.string.rt_yes,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        startActivityForResult(
                                new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS),
                                REQUEST_NO_LOCATION);
                    }
                }
        )
        .setNegativeButton(R.string.rt_no,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Ok, I suppose we can just try looking from where we
                        // are.
                        mMapFragment.mController.onLocation();
                    }
                }
        );
    return builder.create();
}

看起来像这样:

这是 Github - https://github.com/OneBusAway/onebusaway-android/commit/98135b66b0be5b73187e0148bef5d308c42a9fbe.

提交中的完整代码