我正在尝试使用暗模式的3个选项创建AlertDialog:亮,暗和系统默认设置(就像Google Apps中的选项一样) 另外,我正在使用SharedPreferences保存用户选择。 我正在使用Theme.AppCompat.DayNight 我不知道为什么,但是MODE_NIGHT_FOLLOW_SYSTEM中的主题不起作用 同样,退出应用程序并返回(或终止应用程序)时,AlertDialog显示正确的选项,但主题不正确。
这是我的代码:
package com.example.myapplication;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.Switch;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private CheckBox targetSel;
private CheckBox timeOnTarget;
private CheckBox Yield;
private Button launch;
private ImageButton mImageButton;
private Switch missSwitch;
private Switch miss1;
private Switch miss2;
private Switch miss3;
private TextView screen;
private final String MODE="themeState";
private CountDownTimer countDownTimer;
private ProgressDialog progressDialog;
private AlertDialog.Builder mAlertDialog;
private int progress=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
targetSel=findViewById(R.id.checkBox);
timeOnTarget=findViewById(R.id.checkBox2);
Yield=findViewById(R.id.checkBox3);
launch=findViewById(R.id.button);
mImageButton=findViewById(R.id.imageButton2);
missSwitch=findViewById(R.id.switch1);
miss1=findViewById(R.id.switch3);
miss2=findViewById(R.id.switch4);
miss3=findViewById(R.id.switch5);
screen=findViewById(R.id.textView2);
countDownTimer=new CountDownTimer(11000,1000) {
@Override
public void onTick(long millisUntilFinished) {
screen.setText("t-"+millisUntilFinished/1000);
}
@Override
public void onFinish() {
screen.setText("Click launch button");
launch.setEnabled(true);
}
};
missSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (targetSel.isChecked() && timeOnTarget.isChecked() && Yield.isChecked() && missSwitch.isChecked()){
countDownTimer.start();
miss1.setVisibility(View.VISIBLE);
miss2.setVisibility(View.VISIBLE);
miss3.setVisibility(View.VISIBLE);
}
else{
countDownTimer.cancel();
screen.setText("");
miss1.setVisibility(View.INVISIBLE);
miss2.setVisibility(View.INVISIBLE);
miss3.setVisibility(View.INVISIBLE);
miss1.setChecked(false);
miss2.setChecked(false);
miss3.setChecked(false);
launch.setEnabled(false);
}
}
});
//missile 1 Listener
miss1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (miss1.isChecked()) {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("Missile 1 loading");
progressDialog.setMessage("arming...");
progressDialog.setProgressStyle(progressDialog.STYLE_HORIZONTAL);
progressDialog.show();
progressDialog.setCancelable(false);
new Thread(new Runnable() {
@Override
public void run() {
while (progress < 100) {
{
try {
Thread.sleep(10);
progress++;
progressDialog.setProgress(progress);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
if (progress == 100) {
progressDialog.dismiss();
progress = 0;
}
}
}).start();
}
else{
}
}
});
//missile 2 Listener
miss2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (miss2.isChecked()) {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("Missile 2 loading");
progressDialog.setMessage("arming...");
progressDialog.setProgressStyle(progressDialog.STYLE_HORIZONTAL);
progressDialog.show();
progressDialog.setCancelable(false);
new Thread(new Runnable() {
@Override
public void run() {
while (progress < 100) {
{
try {
Thread.sleep(10);
progress++;
progressDialog.setProgress(progress);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
if (progress == 100) {
progressDialog.dismiss();
progress = 0;
}
}
}).start();
}
else{
}
}
});
//missile 3 Listener
miss3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (miss3.isChecked()) {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("Missile 3 loading");
progressDialog.setMessage("arming...");
progressDialog.setProgressStyle(progressDialog.STYLE_HORIZONTAL);
progressDialog.show();
progressDialog.setCancelable(false);
new Thread(new Runnable() {
@Override
public void run() {
while (progress < 100) {
{
try {
Thread.sleep(10);
progress++;
progressDialog.setProgress(progress);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
if (progress == 100) {
progressDialog.dismiss();
progress = 0;
}
}
}).start();
}
else{
}
}
});
launch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if ( (miss1.isChecked() && !miss2.isChecked() && !miss3.isChecked() ) || (!miss1.isChecked() && miss2.isChecked() && !miss3.isChecked() ) || (!miss1.isChecked() && !miss2.isChecked() && miss3.isChecked())){
mAlertDialog =new AlertDialog.Builder(MainActivity.this);
mAlertDialog.setMessage("Missile launched");
mAlertDialog.show();
mAlertDialog.setCancelable(true);
}
else if (!miss1.isChecked() && !miss2.isChecked() && !miss3.isChecked()){
mAlertDialog=new AlertDialog.Builder(MainActivity.this);
mAlertDialog.setMessage("No Missile selected, aborting mission");
mAlertDialog.show();
mAlertDialog.setCancelable(true);
}
else{
mAlertDialog=new AlertDialog.Builder(MainActivity.this);
mAlertDialog.setMessage("Missiles launched");
mAlertDialog.show();
mAlertDialog.setCancelable(true);
}
}
});
mImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAlertDialog=new AlertDialog.Builder(MainActivity.this);
mAlertDialog.setTitle("Theme");
mAlertDialog.setSingleChoiceItems(R.array.themeList,loadSetting(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(which==0){
saveSetting(MODE,0);
themeChange(0);
}
else if (which==1){
saveSetting(MODE,1);
themeChange(1);
}
else{
saveSetting(MODE,2);
themeChange(2);
}
}
});
mAlertDialog.show();
}
});
}
private void saveSetting(String s, int id){
SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor=pref.edit();
editor.putInt(s,id);
editor.apply();
}
private int loadSetting(){
SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(this);
return pref.getInt(MODE,2);
}
private void themeChange(int id){
switch (id){
case 0:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
getDelegate().applyDayNight();
case 1:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
getDelegate().applyDayNight();
case 2:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
getDelegate().applyDayNight();
default:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
getDelegate().applyDayNight();
}
}
}
``