Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android中使用RadioButton启用夜间模式_Java_Android_Android Studio_Android Night Mode - Fatal编程技术网

Java 在Android中使用RadioButton启用夜间模式

Java 在Android中使用RadioButton启用夜间模式,java,android,android-studio,android-night-mode,Java,Android,Android Studio,Android Night Mode,我有一个应用程序,它有对话框,带有3个收音机和无线电组, 因此,如果选中了灯光收音机,单击OK后,应用程序的主题将更改为灯光主题 如果选中了Dark,单击OK后,主题将更改为night主题,如果system则将更改为system 工具栏: <?xml version="1.0" encoding="utf-8"?> <androidx.appcompat.widget.Toolbar xmlns:android="http:/

我有一个应用程序,它有
对话框
,带有3个
收音机
无线电组

因此,如果选中了
灯光收音机
,单击OK后,应用程序的主题将更改为
灯光主题

如果选中了
Dark
,单击OK后,主题将更改为
night主题

,如果
system
则将更改为
system

工具栏:

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFF"
    style="@style/TextAppearance.AppCompat.Widget.Button.Borderless.Colored"
    android:elevation="0dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

</androidx.appcompat.widget.Toolbar>
我尝试了我知道的一切,但我不知道怎么做。
谢谢大家的关注

我在你的代码中找到了一些要点。 首先,我认为你也必须从你的视图中找到你的单选按钮。 像这样:

 RadioButton radioLight = mView.findViewById(R.id.radioLight);
 final RadioButton radioDark = mView.findViewById(R.id.radioDark);
 RadioButton radioSystem = mView.findViewById(R.id.radioSystem);

第二,我认为您可以在调用
对话框之前使用
SharedReferences
查找用户已检查的内容。dimiss

以下内容将检查在RadioGroup中选中的单选按钮:

RadioGroup radioGroup = mView.findViewById(R.id.themeGroup);

这就是我想要达到的目标

Boolean night = false;
    public void chooseTheme(MenuItem item) {
        final AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
        final View mView = getLayoutInflater().inflate(R.layout.dialog_theme,null);
        Button btn_okay = mView.findViewById(R.id.btn_okay);
        Button btn_cancel = mView.findViewById(R.id.btn_cancel);
        alert.setView(mView);
        final AlertDialog alertDialog = alert.create();
        alertDialog.setCanceledOnTouchOutside(false);
        final RadioGroup themeGroup = mView.findViewById(R.id.themeGroup);
        themeGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @SuppressLint("NonConstantResourceId")
            @Override
            public void onCheckedChanged(RadioGroup themeGroup, int i) {
                switch(i) {
                    case R.id.radioLight:
                        night = false;
                        break;
                    case R.id.radioDark:
                        night = true;
                        break;
                }
            }
        });
        btn_okay.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onClick(View v) {
                if(night){
                    sharedpref.setNightModeState(true);
                    Toast.makeText(getApplicationContext(),"Dark mode", Toast.LENGTH_LONG).show();
                }
                else if (!night){
                    sharedpref.setNightModeState(false);
                    Toast.makeText(getApplicationContext(),"Light mode",Toast.LENGTH_LONG).show();
                }
                alertDialog.dismiss();
                restartApp();
            }
        });
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
                                    });
        alertDialog.show();
    }
SharedPref:

public class SharedPref {
    SharedPreferences mySharedPref ;
    public SharedPref(Context context) {
        mySharedPref = context.getSharedPreferences("filename",Context.MODE_PRIVATE);
    }
    // this method will save the nightMode State : True or False
    public void setNightModeState(Boolean state) {
        SharedPreferences.Editor editor = mySharedPref.edit();
        editor.putBoolean("NightMode",state);
        editor.apply();
    }
    // this method will load the Night Mode State
    public Boolean loadNightModeState (){
        Boolean state = mySharedPref.getBoolean("NightMode",false);
        return  state;
    }
}

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.RadioGroup.setOnCheckedChangeListener(android.widget.RadioGroup$OnCheckedChangeListener)”,是的,最终的RadioGroup RadioGroup=findViewById(R.id.themeGroup);抱歉,它已初始化为Final RadioGroup RadioGroup=findViewById(R.id.themeGroup);我不知道你在说什么。我刚试过烤面包,当我单击“亮”或“暗”时,烤面包会显示出来。
RadioGroup radioGroup = mView.findViewById(R.id.themeGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            switch(i) {
                case R.id.radioLight:
                     setLightTheme();
                    Toast.makeText(getApplicationContext(),"Light mode",Toast.LENGTH_LONG).show();
                    break;
                case R.id.radioDark:
                     setDarkTheme();
                    Toast.makeText(getApplicationContext(),"Dark mode",Toast.LENGTH_LONG).show();
                    break;
            }
        }
    });
Boolean night = false;
    public void chooseTheme(MenuItem item) {
        final AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
        final View mView = getLayoutInflater().inflate(R.layout.dialog_theme,null);
        Button btn_okay = mView.findViewById(R.id.btn_okay);
        Button btn_cancel = mView.findViewById(R.id.btn_cancel);
        alert.setView(mView);
        final AlertDialog alertDialog = alert.create();
        alertDialog.setCanceledOnTouchOutside(false);
        final RadioGroup themeGroup = mView.findViewById(R.id.themeGroup);
        themeGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @SuppressLint("NonConstantResourceId")
            @Override
            public void onCheckedChanged(RadioGroup themeGroup, int i) {
                switch(i) {
                    case R.id.radioLight:
                        night = false;
                        break;
                    case R.id.radioDark:
                        night = true;
                        break;
                }
            }
        });
        btn_okay.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onClick(View v) {
                if(night){
                    sharedpref.setNightModeState(true);
                    Toast.makeText(getApplicationContext(),"Dark mode", Toast.LENGTH_LONG).show();
                }
                else if (!night){
                    sharedpref.setNightModeState(false);
                    Toast.makeText(getApplicationContext(),"Light mode",Toast.LENGTH_LONG).show();
                }
                alertDialog.dismiss();
                restartApp();
            }
        });
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
                                    });
        alertDialog.show();
    }
public class SharedPref {
    SharedPreferences mySharedPref ;
    public SharedPref(Context context) {
        mySharedPref = context.getSharedPreferences("filename",Context.MODE_PRIVATE);
    }
    // this method will save the nightMode State : True or False
    public void setNightModeState(Boolean state) {
        SharedPreferences.Editor editor = mySharedPref.edit();
        editor.putBoolean("NightMode",state);
        editor.apply();
    }
    // this method will load the Night Mode State
    public Boolean loadNightModeState (){
        Boolean state = mySharedPref.getBoolean("NightMode",false);
        return  state;
    }
}