java - 如何制作一个 "do not ask me again"的对话框弹出框?安卓

标签 java android android-alertdialog

我正在尝试创建一个“提示”对话框,通知用户在手机上启用 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!");

最佳答案

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

它在 Android Preferences 中设置一个值并检查它是否会显示对话框。

checkbox.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>

Activity.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.

关于java - 如何制作一个 "do not ask me again"的对话框弹出框?安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9550039/

相关文章:

android - 在android中设置NavigationDrawer项目的OnClick

android - 是否可以在virtualbox中运行android studio?

java - setMultiChoiceItems 和我的数组

java - GWT >2.4 RequestFactory 不保存子对象更改

java - f :selectItems - possible? 中的两个集合

java - 使用 selenium webdriver 在第 2 列中写入数据时,数据从第 1 列中删除

java - 如何获取图像的路径?

java - Apache POI 插入图像

android - 当我尝试在 TextView 中设置文本()时出现 NullPointerException

android - 自定义警报对话框获取不需要的填充