Вы должны будете сделать пользовательский диалог, например AlertDialog
, на котором установлен вид пользовательского контента (с setView()
). Этот пользовательский макет может быть TextView
(для представления информации) + a CheckBox
(с Do not ask me again
). В OnClickListener
, установленном для кнопки диалога, вы получите состояние этого CheckBox
, и если пользователь проверил его, чем вы установили флаг в настройках (например, значение boolean).
В следующий раз, когда пользователь использует приложение, вы проверите это логическое значение из настроек и, если оно установлено в 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>
Что вы пытались? –
Только что отредактировал главный вопрос. Я добавил, что ^^, но как я могу сделать так, чтобы я мог добавить флажок «Не спрашивать меня снова»? И это на самом деле не показывает? – Zukky