How do three buttons work as radio button?

0

I would like to know how to make three buttons work as a "radio button".

When you click the button it changes color and executes command, clicking another button changes color and another normal return.

    
asked by anonymous 18.02.2015 / 14:23

1 answer

1

I'll give you a simple example of use. Please remember that the code below is just a demo.


public class Main extends Activity {
    @Override
    protected void onCreate(Bundle cicle) {
        super.onCreate(cicle);
        setContentView(R.layout.first); // é apenas um exemplo!
        // ... resto do código

        // Primeiramente, vamos inserir um OnClickListener em nossos três botões.
        findViewById(R.id.button1)).setOnClickListener(bHandler); // Click Listener do Primeiro Botão!
        findViewById(R.id.button1)).setOnClickListener(bHandler); // Listener do segundo
        findViewById(R.id.button1)).setOnClickListener(bHandler); // Listener do terceiro
    }

    View.OnClickListener bHandler = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch(v.getId()) {
                case R.id.button1:
                    break;
                case R.id.button2:
                    break;
                case R.id.button3:
                    break;
            }
        }
    }
}

That's all the basis you'll need. Now, you just have to enter the actions on each button, and still check if a button is already selected. Use a Boolean variable.

If you still have questions about the actions of each button, leave a comment that I will help.

Hugs.

    
18.02.2015 / 18:29