N
N
Nikita Avdeev2017-07-14 10:37:54
Java
Nikita Avdeev, 2017-07-14 10:37:54

How to set up ToggleButton to change text?

Good day!
We have: 6 Button, 1 ToggleButton, 1 TextView, and an Array.
When one of the 6 buttons is clicked, information is displayed in the TextView.
If you translate ToggleButton to true, when you click on the same 6 buttons, the opposite information will be displayed.
All data for the TextView is taken from the array.
Now faced such problem. I press a button (for example btnP) and the text comes out in the TextView. After that I click on the ToggleButton and nothing happens on the screen. Then I click on the same btnP again and other information is displayed.
How to make it so that when the ToggleButton is pressed, the information already shown in the TextView immediately changes to the opposite?

My code
Bundle extraArray = getIntent().getBundleExtra("extraArray");
        ArrayList<Integer> Array = (ArrayList<Integer>) extraRunes.getSerializable("Array");

        assert Array != null;
        TextView.setText(Array.get(1));
        iP = 1;
        iL = 3;
        iC = 5;
        iT = 7;
        iH = 9;
        iW = 11;
    }
    @Override
    public void onClick(View v) {

        Bundle extraArray = getIntent().getBundleExtra("extraArray");
        ArrayList<Integer> Array = (ArrayList<Integer>) extraArray.getSerializable("Array");

        assert Array != null;
        switch (v.getId()) {
            case R.id.btnP:
                TextView.setText(Array.get(iP));
                break;
            case R.id.btnL:
                TextView.setText(Array.get(iL));
                break;
            case R.id.btnC:
                TextView.setText(Array.get(iC));
                break;
            case R.id.btnT:
                TextView.setText(Array.get(iT));
                break;
            case R.id.btnH:
                TextView.setText(Array.get(iH));
                break;
            case R.id.btnW:
                TextView.setText(Array.get(iW));
                break;
        }

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        boolean checked = tbFlip.isChecked();
        Bundle extraArray = getIntent().getBundleExtra("extraArray");
        ArrayList<Integer> Array = (ArrayList<Integer>) extraRunes.getSerializable("Array");

        if (checked) {
            assert Array != null;
            iP = 2;
            iL = 4;
            iC = 6;
            iT = 8;
            iH = 10;
            iW = 12;

        } else {
            assert Array != null;
            iP = 1;
            iL = 3;
            iC = 5;
            iT = 7;
            iH = 9;
            iW = 11;
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2017-07-14
@zagayevskiy

Obviously, you need to take and manually set the desired text. Buttons do not know at all that you are putting text into them from an array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question