如何使“不要再问我”对话框popup框? Android的

我正在尝试创build一个“提示”对话框,通知用户在手机上启用GPS会降低电池寿命。 我希望它popup,但有一个checkbox,说:“不要再问我”。

我如何去在Android中创build这个?

谢谢,

Zukky。

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!"); 

Solutions Collecting From Web of "如何使“不要再问我”对话框popup框? Android的"

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

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

资源/布局中的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(); } } 

正如你所看到的,我也做了“复制和粘贴”。只更改了消息字符串。它的作品非常漂亮。

你将不得不做一个自定义对话框,例如一个AlertDialog ,你可以在其上设置一个自定义内容视图(使用setView() )。 该自定义布局可能是一个TextView (来呈现信息)+一个CheckBoxDo not ask me again )。 在为对话框的button设置的OnClickListener ,您将获得CheckBox的状态,并且如果用户检查了它,而不是在首选项中设置标志(例如,布尔值为true)。

下次用户使用应用程序时,您将从首选项中检查布尔值,如果它设置为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> 

我有我的less代码解决scheme。 这并不完美,因为不能使用描述,只有信息可以作为对话的标题传递。 MultiChoiceItem用于checkbox。

在res / values / strings.xml中:

 <string-array name="do_not_show_again_array"> <item>Do not show again.</item> </string-array> 

然后我的代码如下所示:

 DialogInterface.OnClickListener dialogClickListener = new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Do something here } }; final AlertDialog.Builder builder = new AlertDialog.Builder(activity); AlertDialog alertDialog = builder.setTitle("Title/Description") .setMultiChoiceItems(R.array.do_not_show_again_array, null, new OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { appPrefs.setLocationOnStart(!isChecked); } }) .setPositiveButton("Ja", dialogClickListener) .setNegativeButton("Nein", dialogClickListener).show(); } 

嗨,我跟着一个教程 ,我发现这个代码
你可以使用下面的代码:

  AlertDialog.Builder adb= new AlertDialog.Builder(this); LayoutInflater adbInflater = LayoutInflater.from(this); View eulaLayout = adbInflater.inflate (R.layout.activity_main, null); check = (CheckBox) eulaLayout.findViewById(R.id.skip); adb.setView(eulaLayout); adb.setTitle("Example:"); adb.setMessage(Html.fromHtml("Type your text here: ")); adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { String checkBoxResult = "NOT checked"; if (check.isChecked()) checkBoxResult = "checked"; SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putString("noshow", checkBoxResult); // Commit the edits! // sunnovalthesis(); editor.commit(); return; } }); adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { String checkBoxResult = "NOT checked"; if (check.isChecked()) checkBoxResult = "checked"; SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putString("noshow", checkBoxResult); // Commit the edits! // sunnovalthesis(); editor.commit(); return; } }); SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); String noshow = settings.getString ("noshow", "NOT checked"); if (noshow != "checked" ) adb.show(); 

我对这个问题有一个清晰和正确的方法

 package com.example.user.testing; import android.content.DialogInterface; import android.content.SharedPreferences; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.CheckBox; public class MainActivity extends AppCompatActivity { CheckBox dontShowAgain; public static final String PREFS_NAME = "MyPrefsFile1"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this); LayoutInflater adbInflater = LayoutInflater.from(MainActivity.this); View eulaLayout = adbInflater.inflate(R.layout.checkbox, null); dontShowAgain = (CheckBox) eulaLayout.findViewById(R.id.skip); adb.setView(eulaLayout); adb.setTitle("Attention"); adb.setMessage("Your message here"); adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putBoolean("skipMessage", dontShowAgain.isChecked()); editor.commit(); dialog.cancel(); } }); adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); Boolean skipMessage = settings.getBoolean("skipMessage", false); if (skipMessage.equals(false)) { adb.show(); } } 

}“