我正在尝试创建一个“提示”对话框,通知用户在他们的手机上启用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!");
发布于 2014-08-29 23:24:55
我有我的less代码解决方案。它并不完美,因为无法使用描述,并且只能将信息作为对话框的标题传递。checkbox使用MultiChoiceItem。
在res/values/strings.xml s.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();
}
发布于 2015-07-08 11:50:23
嗨,我跟着一个tutorial,我找到了这段代码
你可以使用下面的代码:
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();
发布于 2016-06-01 02:13:22
对于这个问题,我有一个清晰而正确的方法。
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();
}
}
}
https://stackoverflow.com/questions/9550039
复制相似问题