我正在尝试创建一个包含消息和复选框的对话框。我使用以下代码
private void displayWarning() {
SharedPreferences prefs;
final String PREFS_NAME = "UserData";
final String PREF_SHOW_WARNING_KEY = "show_warning";
prefs = this.getActivity().getSharedPreferences(PREFS_NAME, 0);
final String[] items = {"do not show again"};
final boolean[] itemsChecked = {false};
boolean displayWarnings = prefs.getBoolean(PREF_SHOW_WARNING_KEY, true);
if (displayWarnings) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setMessage("this is a warning")
.setCancelable(false)
.setMultiChoiceItems(items, itemsChecked, new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
itemsChecked[which] = false;
} else {
itemsChecked[which] = true;
}
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do things
}
});
AlertDialog alert = builder.create();
alert.show();
}
if (itemsChecked[0]) {
displayWarnings = false;
}
else {
displayWarnings = true;
}
prefs.edit().putBoolean(PREF_SHOW_WARNING_KEY, displayWarnings).commit();
}
运行此代码时,复选框和文本"不再显示" doens没有显示出来。当我删除消息时("这是一个警告"),我确实得到了复选框。此外,当我将消息更改为标题时,我会收到复选框,但原始消息太长而无法在标题中使用...
希望这样做。最重要的是,我想要一个包含消息和复选框的对话框供用户检查,这样对话框就不会再显示了。
答案 0 :(得分:1)
警报对话框适合只显示一种类型的消息。
所以,据我所知,如果你想要查看短信而不是多重发票,你必须使用自定义布局。
StackOverflow上有很多关于同一个项目的类似问题,每个人都有相同的建议:使用“复杂”布局的自定义布局。
观看以下链接:
答案 1 :(得分:0)
看看这个例子。我有一个带复选框的警告对话框。它工作正常:
public class StartActivity extends AppCompatActivity {
SharedPreferences sharedpreferences;
String email_pref;
String passw_pref;
Boolean remember;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
sharedpreferences = getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
email_pref = sharedpreferences.getString("EMAIL_SAVED", "");
passw_pref = sharedpreferences.getString("PASSW_SAVED", "");
remember = sharedpreferences.getBoolean("REMEMBER", false);
}
public void SignIn(View v){
// dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
// custom dialog
final Dialog dialog = new Dialog(StartActivity.this);
dialog.setContentView(R.layout.sign_in_layout); //il suo layout personalizzato
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
// set the custom dialog components - text, image and button
final EditText email = (EditText) dialog.findViewById(R.id.editEmailSignIn);
final EditText password = (EditText) dialog.findViewById(R.id.editPasswordSignIn);
final CheckBox rememberCheck = (CheckBox) dialog.findViewById(R.id.check_in);
if(remember == true && !email_pref.equals("") && !passw_pref.equals("")){
email.setText(email_pref);
password.setText(passw_pref);
rememberCheck.setChecked(true);
}
Button dialogButton = (Button) dialog.findViewById(R.id.btn_confirm_signin);
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String mail = email.getText().toString();
String psw = password.getText().toString();
//QUERY AL DB PER VEDERE SE L'UTENTE ESISTE E ACCESSO EVENTUALE ALL'ACTIVITY SUCCESSIVA OPPURE ERRORE
String query = "SELECT email, password FROM users WHERE email='" + mail + "' AND password = '" + psw + "'";
final SQLiteDatabase db = openOrCreateDatabase("AuctionDB", MODE_PRIVATE, null);
//db.execSQL(query);
Cursor c = db.rawQuery(query, null);
if(!c.moveToFirst()){
Toast.makeText(getApplicationContext(),"No matches!", Toast.LENGTH_LONG).show();
}else{
//Toast.makeText(getApplicationContext(),"UTENTE TROVATO", Toast.LENGTH_LONG).show();
dialog.dismiss();
if(rememberCheck.isChecked()){
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("EMAIL_SAVED", email.getText().toString());
editor.putString("PASSW_SAVED", password.getText().toString());
editor.putBoolean("REMEMBER", true);
editor.commit();
}else{
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("EMAIL_SAVED", "");
editor.putString("PASSW_SAVED", "");
editor.putBoolean("REMEMBER", false);
editor.commit();
}
//pass to the ViewActivity
Intent intent = new Intent(StartActivity.this, ViewActivity.class);
Bundle b = new Bundle();
b.putString("email", mail);
intent.putExtras(b);
startActivity(intent);
finish();
}
}
});
dialog.show();
}
整个例子是: