如何制作“不再问我”对话框弹出框?人造人

2022-09-02 11:15:32

我正在尝试创建一个“提示”对话框,通知用户在手机上启用GPS将缩短电池寿命。我希望它弹出,但有一个复选框,上面写着:“不要再问我”。

如何在 Android 中创建它?

谢谢

祖基。

AlertDialog.Builder prompt = new AlertDialog.Builder(this); 
prompt.setCancelable(false); 
prompt.setTitle("Warning"); 
prompt.setMessage ("HINT: Otherwise, it will use network to find" + 
                   "your location. It's inaccurate but saves on " + 
                   "battery! Switch GPS on for better accuracy " + 
                   "but remember it uses more battery!");

答案 1

编辑:当心!代码重复。由于我不再为Android开发,因此我无法重构下面的代码。

它在Android首选项中设置一个值,并检查它是否将显示对话框。

复选框.xml资源/布局中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:padding="10dp" >
    <CheckBox
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/skip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ok please do not show again." >
    </CheckBox>
</LinearLayout>

活动.java

public class MyActivity extends Activity {
    public static final String PREFS_NAME = "MyPrefsFile1";
    public CheckBox dontShowAgain;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    protected void onResume() {
        AlertDialog.Builder adb = new AlertDialog.Builder(this);
        LayoutInflater adbInflater = LayoutInflater.from(this);
        View eulaLayout = adbInflater.inflate(R.layout.checkbox, null);
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        String skipMessage = settings.getString("skipMessage", "NOT checked");

        dontShowAgain = (CheckBox) eulaLayout.findViewById(R.id.skip);
        adb.setView(eulaLayout);
        adb.setTitle("Attention");
        adb.setMessage(Html.fromHtml("Zukky, how can I see this then?"));

        adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                String checkBoxResult = "NOT checked";

                if (dontShowAgain.isChecked()) {
                    checkBoxResult = "checked";
                }

                SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                SharedPreferences.Editor editor = settings.edit();

                editor.putString("skipMessage", checkBoxResult);
                editor.commit();

                // Do what you want to do on "OK" action

                return;
            }
        });

        adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                String checkBoxResult = "NOT checked";

                if (dontShowAgain.isChecked()) {
                    checkBoxResult = "checked";
                }

                SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                SharedPreferences.Editor editor = settings.edit();

                editor.putString("skipMessage", checkBoxResult);                    
                editor.commit();

                // Do what you want to do on "CANCEL" action

                return;
            }
        });

        if (!skipMessage.equals("checked")) {
            adb.show();
        }

        super.onResume();
    }
}

As you can see, I did "copy and paste" too. Changed only the message strings. It works beautifully.


答案 2

您必须创建一个自定义对话框,例如,在其上设置自定义内容视图(使用 setView())。该自定义布局可以是(用于显示信息)+ a(带 )。在对话框按钮的集合中,您可以获得该按钮的状态,如果用户检查了它,那么您在首选项中设置了一个标志(例如,布尔值为true)。AlertDialogTextViewCheckBoxDo not ask me againOnClickListenerCheckBox

下次用户使用该应用程序时,您将从首选项中检查该布尔值,如果将其设置为true,则不会显示对话框,否则用户未选中该对话框,因此您再次向他显示对话框。CheckBox

编辑示例应用程序:

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class DoNotShowDialog extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button action = new Button(this);
        action.setText("Start the dialog if the user didn't checked the "
                + "checkbox or if is the first run of the app.");
        setContentView(action);
        action.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                SharedPreferences prefs = PreferenceManager
                        .getDefaultSharedPreferences(DoNotShowDialog.this);
                boolean dialog_status = prefs
                        .getBoolean("dialog_status", false);//get the status of the dialog from preferences, if false you ,ust show the dialog
                if (!dialog_status) {
                    View content = getLayoutInflater().inflate(
                            R.layout.dialog_content, null); // inflate the content of the dialog
                    final CheckBox userCheck = (CheckBox) content //the checkbox from that view
                            .findViewById(R.id.check_box1);
                    //build the dialog
                    new AlertDialog.Builder(DoNotShowDialog.this) 
                            .setTitle("Warning")
                            .setView(content)
                            .setPositiveButton("Ok",
                                    new DialogInterface.OnClickListener() {

                                        public void onClick(
                                                DialogInterface dialog,
                                                int which) {
                                            //find our if the user checked the checkbox and put true in the preferences so we don't show the dialog again 
                                            SharedPreferences prefs = PreferenceManager
                                                    .getDefaultSharedPreferences(DoNotShowDialog.this);
                                            SharedPreferences.Editor editor = prefs
                                                    .edit();
                                            editor.putBoolean("dialog_status",
                                                    userCheck.isChecked());
                                            editor.commit();
                                            dialog.dismiss(); //end the dialog.
                                        }
                                    })
                            .setNegativeButton("Cancel",
                                    new DialogInterface.OnClickListener() {

                                        public void onClick(
                                                DialogInterface dialog,
                                                int which) {
                                            //find our if the user checked the checkbox and put true in the preferences so we don't show the dialog again
                                            SharedPreferences prefs = PreferenceManager
                                                    .getDefaultSharedPreferences(DoNotShowDialog.this);
                                            SharedPreferences.Editor editor = prefs
                                                    .edit();
                                            editor.putBoolean("dialog_status",
                                                    userCheck.isChecked());
                                            editor.commit();
                                            dialog.dismiss();

                                        }
                                    }).show();
                } else {
                    //the preferences value is true so the user did checked the checkbox, so no dialog
                    Toast.makeText(
                            DoNotShowDialog.this,
                            "The user checked the checkbox so we don't show the dialog any more!",
                            Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

对话框内容的布局():R.layout.dialog_content

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enabling GPS on your phone will decrease battery life!" />

    <CheckBox
        android:id="@+id/check_box1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Do not ask me again!" />

</LinearLayout>

推荐