How to make three buttons work as radio button?

Asked

Viewed 94 times

0

I wonder how to make three buttons work as "radio button".

By clicking the button it changes color and executes command, by clicking another button changes color and another normal turn.

1 answer

1


I’ll give you a simple example of use. 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;
            }
        }
    }
}

This is all the foundation you will need. Now, you just need to insert the actions on each button, and still check if a button is already selected. Use a boolean variable.

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

Hugs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.