如何使我的切换按钮延迟4秒播放共享的偏好设置,并在同一按钮上进行第二次检查

时间:2018-12-14 00:17:50

标签: java sharedpreferences togglebutton multiple-instances

我在尝试播放声音时遇到麻烦,在代码中播放第二个布尔值,我想知道保存是否遇到麻烦或其他问题。我将变量设置为私有静态int,该变量可以可以在1到2之间更改。之后,我需要一个SharedPreferences编辑器来更改值,然后我有一个可单击的按钮,它可以在两个不同的场合读取共享的首选项。它根本不起作用,我想知道是否可以使用静态int来更改单独的SharedPreferences场合。这是我的代码

按钮

 toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            switch(ButtonConfig) {
                case '1':
                    if(ButtonConfig == 1){
                    SharedPreferences.Editor editor = preferences.edit();
                    editor.putBoolean(MY_BUTTON_PREF, isChecked);
                    editor.apply();}
                    break;

                case '2':
                    if(ButtonConfig == 2){
                    SharedPreferences.Editor editor = preferences.edit();
                    editor.putBoolean(AAA, isChecked);
                    editor.apply();}
                    break;


            }  }
    });

播放按钮

Playx2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            boolean flag = preferences.getBoolean(MY_BUTTON_PREF, false);
            toggleButton.setChecked(flag);
            try {
                Thread.sleep(4000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            boolean flag1 = preferences.getBoolean(AAA, false);
            toggleButton.setChecked(flag1);

声明

 Button Play;
private static int ButtonConfig;
public static int Options;
private static final String MY_SHARED_PREFERENCES = "SharedPreferences";
private static final String MY_BUTTON_PREF = "button1";
private static final String MY_SHARED_PREFERENCES2 = "SharedPreferences2";
private static final String AAA = "button500";

外面,我有onClick方法

 public void D1(View view) {
    boolean checked = ((ToggleButton) view).isChecked();
    if (checked) {
        switch (toggleButton) {

            case '1':
                if (ButtonConfig == 1) {
                    SharedPreferences.Editor editor = getSharedPreferences("SharedPreferences", MODE_PRIVATE).edit();
                    editor.putBoolean("button1", true);
                    editor.apply();
                    soundPool.play(sound1, 1, 1, 0, 0, 1);
                }
                break;

            case '2':
                if (ButtonConfig == 2) {
                    SharedPreferences.Editor editor = getSharedPreferences("SharedPreferences", MODE_PRIVATE).edit();
                    editor.putBoolean("button500", true);
                    editor.apply();
                    soundPool.play(sound2, 1, 1, 0, 0, 1);
                }
                break;
        }
    } else {
        switch(ButtonConfig){

            case '1'  :

        SharedPreferences.Editor editor = getSharedPreferences("SharedPreferences", MODE_PRIVATE).edit();
        editor.putBoolean("button1", false);
        editor.apply();
        break;

            case '2'   :

                editor = getSharedPreferences("SharedPreferences2", MODE_PRIVATE).edit();
                editor.putBoolean("button500", false);
                editor.apply();
                break;

    }
}}

public void S49(View view) {
    boolean checked = ((ToggleButton) view).isChecked();
    if (checked) {

        ButtonConfig = 1;
    }else{
}}

public void S50(View view) {
    boolean checked = ((ToggleButton) view).isChecked();
    if (checked) {

        ButtonConfig = 2;
    }else{
} }

这两个按钮都有一个int值,它们会根据情况返回,但是每次我尝试加载两个单独的按钮值时,它都会将第一个按钮选中并第二个按钮取消选中。声音也不会在case语句中播放,但是如果我将它们带到外面,它们会很好。有人可以帮忙吗?我真的很感激

0 个答案:

没有答案