多个开关按钮

时间:2018-01-23 13:48:31

标签: java android listview android-fragments

我是Android开发新手,对于多个切换按钮有疑问,如何巧妙地检索值,并将这些数据用于其他活动?

目前,我实现了保存按钮状态并检索当前活动中的信息,如下所示:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    switchButtonOne = findViewById(R.id.switchButtonOne);
    switchButtonOne.setChecked(true);

    switchButtonTwo = findViewById(R.id.switchButtonTwo);
    switchButtonTwo.setChecked(false);

    //...... (In total there are 5 switch buttons)

    //switchButtonOne
    switchButtonOne.setOnCheckedChangeListener((compoundButton, isChecked) -> {
        if(isChecked){
            stringArrayList.add("1");
        }else {
            stringArrayList.remove("1");
        }
        getArray(stringArrayList);
        //SAVE
        sharedPreferences[0] = PreferenceManager
                .getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = sharedPreferences[0].edit();
        editor.putBoolean("1", switchButtonOne.isChecked());
        editor.commit();
    });
    switchButtonOne.setChecked(sharedPreferences[0].getBoolean("1", false));

    //switchButtonTwo
    switchButtonTwo.setOnCheckedChangeListener((compoundButton, isChecked) -> {
        if(isChecked){
            stringArrayList.add("2");
        }else {
            stringArrayList.remove("2");
        }
        getArray(stringArrayList);
        //SAVE
        sharedPreferences[0] = PreferenceManager
                .getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = sharedPreferences[0].edit();
        editor.putBoolean("2", switchButtonTwo.isChecked());
        editor.commit();
    });
    //.......
}

我确信有更有效的方法来实现这一点,我很乐意感谢有效的资源来学习在活动之间提供信息和活动状态的最佳实践。

提前非常感谢

2 个答案:

答案 0 :(得分:0)

一个选项是对所有五个按钮仅使用一个侦听器。你会这样做:

public class MyActivity extends Activity implements CompoundButton.OnCheckedChangeListener {
    Switch[] buttons;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        buttons = new Switch[5];
        Switch switchButtonOne = findViewById(R.id.switchButtonOne);
        switchButtonOne.setOnCheckedChangeListener(this);
        switchButtonOne.setChecked(sharedPreferences[0].getBoolean("1", false));
        buttons[0] = switchButtonOne;

        Switch switchButtonTwo = findViewById(R.id.switchButtonTwo);
        switchButtonTwo.setOnCheckedChangeListener(this);
        switchButtonTwo.setChecked(sharedPreferences[1].getBoolean("2", false));
        buttons[1] = switchButtonTwo;

        //...... (In total there are 5 switch buttons)
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        int index;
        switch (buttonView.getId()) {
            case R.id.switchButtonOne:
                index = 0;
                break;
            case R.id.switchButtonTwo:
                index = 1;
                break;
                ...
        }
        if (isChecked) {
            stringArrayList.add(String.valueOf(index + 1));
        } else {
            stringArrayList.remove(String.valueOf(index + 1));
        }
        getArray(stringArrayList);
        //SAVE
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = sharedPreferences[index].edit();
        editor.putBoolean(String.valueOf(index + 1), buttons[index].isChecked());
        editor.commit();
    }
}

此时代码并不比你的短,但是,只要你添加其他三个按钮,你就会看到所有按钮的代码最终变得更短更简洁。

BooleanArray设置SharedPreferences会很棒,但是,这样的类不存在。可以找到解决方法here

修改

再次查看您的问题和完整代码,我建议采用不同的方法。你根本不需要任何OnCheckedChangeListener。只要保留活动(onPause),只需保存按钮的状态,并在活动恢复后恢复状态(onResume)。副作用是代码变得更短。

public class MainActivity extends Activity {
    Switch[] buttons;
    int numberOfButtons = 5;
    SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);

        buttons = new Switch[numberOfButtons];
        sharedPreferences = getSharedPreferences("YourPreferenceName", MODE_PRIVATE);

        for (int i = 0; i < numberOfButtons; i++) {
            buttons[i] = findViewById(getResources().getIdentifier("switchButton" + String.valueOf(i + 1), "id", getPackageName()));
        }
    }

    public void onPause() {
        super.onPause();
        for (int i = 0; i < numberOfButtons; i++) {
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putBoolean(String.valueOf(i + 1), buttons[i].isChecked());
            editor.apply();
        }
    }

    public void onResume(){
        super.onResume();
        for (int i = 0; i < numberOfButtons; i++) {
            buttons[i].setChecked(sharedPreferences.getBoolean(String.valueOf(i + 1), false));
        }
    }
}

请注意,在您的布局xml中,您必须将交换机的ID更改为以下内容:switchButton1,...,switchButton5

答案 1 :(得分:0)

您可以为整个活动实施CompoundButton.OnCheckedChangeListener,然后为您使用过的所有交换机设置监听器。处理单个回调onCheckedChanged中的所有切换。

public class SwitchActivity extends Activity implements CompoundButton.OnCheckedChangeListener {
    Switch switchButtonOne;
    Switch switchButtonTwo;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {

        super.onCreate(savedInstanceState, persistentState);
        switchButtonOne = findViewById(R.id.switchButtonOne);
        switchButtonOne.setChecked(true);

        switchButtonTwo = findViewById(R.id.switchButtonTwo);
        switchButtonTwo.setChecked(false);

        switchButtonOne.setOnCheckedChangeListener(this);
        switchButtonTwo.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.switchButtonOne:
                //Handle Code for switchButtonOne
                break;
            case R.id.switchButtonTwo:
                //Handle Code for switchButtonOne
                break;
        }
    }
}