为什么setOnChangeListener无法正常工作?

时间:2019-02-04 12:48:58

标签: java android

我有三个带有setOnChangeListener的开关。前两个工作正常。但是由于某些原因,第三个是什么也不做。我读了50次代码,但找不到错误。它应该将一个int保存到SharedPreferences并显示Toast。调试器表明,他进入了案例但不执行它。执行此操作后,MainActivity应该加载保存的值并更改布局。但是代码甚至没有保存。当我重新启动SettingsTheme Activity时,应该加载开关的状态,并且该开关的状态适用于前两个,而不适用于称为行的Swtich。

如果您能提供帮助,那就太好了!

我也尝试将第3个侦听器的处理内容放到第二个侦听器中,但它不起作用。试图删除onChangeListener中的保存内容并进行烤面包,尝试删除MainActivity中的variabels内容,但没有任何效果。

public class SettingsTheme extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings_theme);
    setTitle("Themen");
    final Switch darkmode=findViewById(R.id.darkmode);
    final Switch auto_darkmode=findViewById(R.id.auto_darkmode);
    final Switch rows=findViewById(R.id.modul_rows);
    if(variables.LoadInt("dark_mode")==1){darkmode.setChecked(true);}
    if(variables.LoadInt("dark_mode")==2){
        auto_darkmode.setChecked(true);
        darkmode.setBackgroundResource(R.color.light_grey);
        darkmode.setClickable(false);
        darkmode.setVisibility(View.GONE);}
    if(variables.LoadInt("rows")==3){rows.setChecked(true);}

    darkmode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked==true){
                variables.SaveInt("dark_mode",1);
            }
            else{
                variables.SaveInt("dark_mode",0);
            }
        }
    });
    auto_darkmode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked==true){
                variables.SaveInt("dark_mode",2);
                darkmode.setBackgroundResource(R.color.light_grey);
                darkmode.setClickable(false);
                darkmode.setChecked(false);
                darkmode.setVisibility(View.GONE);
            }
            else{
                variables.SaveInt("dark_mode",0);
                darkmode.setClickable(true);
                darkmode.setBackgroundResource(0);
                darkmode.setVisibility(View.VISIBLE);
            }
        }
    });
    rows.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked==true){

                variables.SaveInt("rows",3);
                Toast.makeText(SettingsTheme.this, "3x7 Layout gewählt",Toast.LENGTH_SHORT);
            }
            else{
                variables.SaveInt("rows",2);
                Toast.makeText(SettingsTheme.this,"2x3 Layout,Toast.LENGTH_SHORT); }
        }
    });}




public class variables {

public static SharedPreferences Var;
public static int LoadInt(String s){
    Var=MainActivity.PublicContext.getSharedPreferences(s,       Context.MODE_PRIVATE);
    return Var.getInt(s,0);
}
public static void SaveInt(String s, int i){
    SharedPreferences.Editor editor=Var.edit();
    editor.putInt(s,i).apply();
}
public static String LoadString(String s){
    Var=MainActivity.PublicContext.getSharedPreferences(s, Context.MODE_PRIVATE);
    return Var.getString(s,"");
}
public static void SaveString(String s, String sts){
    SharedPreferences.Editor editor=Var.edit();
    editor.putString(s,sts).apply();
}

}

public class MainActivity extends AppCompatActivity {

public View root;
public static Context PublicContext=null;
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(PublicContext==null)PublicContext=getApplicationContext();
    View view1=getLayoutInflater().inflate(R.layout.activity_main_row2,null);
    View view2= getLayoutInflater().inflate(R.layout.activity_main_row3,null);
    if(variables.LoadInt("rows")==3){setContentView(view2);}
    else{setContentView(view1);}
}
}

2 个答案:

答案 0 :(得分:2)

您的Toast缺少.show()。因此,即使执行代码,也不会显示任何敬酒。

答案 1 :(得分:0)

我通过更改variables.java中的代码解决了这个问题。我使用了TinyDB类(https://github.com/kcochibili/TinyDB--Android-Shared-Preferences-Turbo/blob/master/TinyDB.java):

public class variables {

public static TinyDB Var=new TinyDB(MainActivity.PublicContext);

public static  int LoadInt(String s){
    return Var.getInt(s);
}
public static void SaveInt(String s, int i){
    Var.putInt(s,i);
}
public static String LoadString(String s){
    return Var.getString(s);
}
public static void SaveString(String s, String sts){
    Var.putString(s,sts);
}

}