How to make a button or a checkbox become inaccessible in android studio?

0

I would like to confirm the choices of both the buttons and the checkboxes, so that they become inaccessible / irresponsible / inactive.

I know you can do this using if, but I think there should be some function that makes them "inaccessible."

Activity:

package genesysgeneration.checkeradio;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btn01, btn02;
    private CheckBox cb01, cb02;
    private AlertDialog alerta;

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

        btn01=(Button)findViewById(R.id.btn01);
        btn02=(Button)findViewById(R.id.btn02);
        cb01=(CheckBox)findViewById(R.id.cb01);
        cb02=(CheckBox)findViewById(R.id.cb02);

        btn01.setOnClickListener(this);
        btn02.setOnClickListener(this);
        cb01.setOnClickListener(this);
        cb02.setOnClickListener(this);

    }

    public void onClick(View v){

        switch (v.getId()){

            case R.id.btn01:

                AlertDialog.Builder builder01 = new AlertDialog.Builder(this);
                builder01.setMessage("Tem certeza?");
                builder01.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        Toast.makeText(getApplicationContext(), "Você escolheu o button 01", Toast.LENGTH_SHORT).show();
                        //tornar button01 e button02 inacessíveis

                    }
                });
                builder01.setNegativeButton("Não", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                alerta = builder01.create();
                alerta.show();
                break;

            case R.id.btn02:

                AlertDialog.Builder builder02 = new AlertDialog.Builder(this);
                builder02.setMessage("Tem certeza?");
                builder02.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        Toast.makeText(getApplicationContext(), "Você escolheu o button 02", Toast.LENGTH_SHORT).show();
                        //tornar button01 e button02 inacessíveis

                    }
                });
                builder02.setNegativeButton("Não", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                alerta = builder02.create();
                alerta.show();
                break;

            case R.id.cb01:

                AlertDialog.Builder builder03 = new AlertDialog.Builder(this);
                builder03.setMessage("Tem certeza?");
                builder03.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        Toast.makeText(getApplicationContext(), "Você escolheu a checkbox01", Toast.LENGTH_SHORT).show();
                        //tornar checkbox 01 e checkbox02 inacessíveis

                    }
                });
                builder03.setNegativeButton("Não", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        cb01.setChecked(false);

                    }
                });
                alerta = builder03.create();
                alerta.show();
                break;

            case R.id.cb02:

                AlertDialog.Builder builder04 = new AlertDialog.Builder(this);
                builder04.setMessage("Tem certeza?");
                builder04.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        Toast.makeText(getApplicationContext(), "Você escolheu a checkbox02", Toast.LENGTH_SHORT).show();
                        //tornar checkbox 01 e checkbox02 inacessíveis

                    }
                });
                builder04.setNegativeButton("Não", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        cb02.setChecked(false);

                    }
                });
                alerta = builder04.create();
                alerta.show();
                break;

        }

    }

}
    
asked by anonymous 22.03.2017 / 00:20

2 answers

2

To disable a CheckBox so that it can not be clicked, the enabled attribute is used, being false disabled or true enabled. See below for examples:

XML

<CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Desabilitado"
        android:enabled="false"/>

Programmatically

Checkbox checkbox = (Checkbox) findViewById(R.id.checkbox);
checkbox.setEnabled(false); 

See more details in the documentation.

    
22.03.2017 / 14:56
1
CheckBox meuCheckBox = (CheckBox) findViewByid(R.id.meu_check_box);
meuCheckBox.setEnabled(false);


(The same method setEnabled(false) is applied to the buttons)

    
22.03.2017 / 14:35