为什么setonchangelistener不能正常工作?

qgelzfjb  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(199)

我有三个setonchangelistener开关。前两个工作正常。但第三个原因是什么也不做。我把我的代码读了50遍,但我找不出哪里错了。它应该保存一个int以共享引用并显示toast。调试器显示,他进入了案件,但没有执行它。执行此操作后,mainactivity应加载保存的值并更改布局。但代码甚至没有保存。当我重新启动setingsTime活动时,应该加载开关的状态,这对前两个有效,但对swtich调用的行无效。
如果你能帮忙就太好了!
我也试着把待办事项从第三个听众的第二个,但它没有工作。尝试删除onchangelistener中的save内容并制作toast,尝试删除mainactivity中的Variables内容,但没有任何效果。

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);}
}
}
kxe2p93d

kxe2p93d1#

我通过在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);
}

}
mrzz3bfm

mrzz3bfm2#

你的 Toast 有人失踪了 .show() . 因此,即使代码执行,也不会显示祝酒词。

相关问题