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

Asked

Viewed 755 times

0

I would like it to be confirmed that the choices of both Uttons and checkboxes become inaccessible/irresponsive/inactive.

I know you can do this using if, but I believe there must 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;

        }

    }

}

  • Inaccessible in the sense of disabled?

  • @Acklay!

  • So, that’s what I imagined. I put an answer, showing that you can do both in xml and java. See below.

2 answers

2


To disable a CheckBox so that it cannot be clicked, the attribute is used enabled, being him false for disabled or true for enabled. See below 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.

1

CheckBox meuCheckBox = (CheckBox) findViewByid(R.id.meu_check_box);
meuCheckBox.setEnabled(false);


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

Browser other questions tagged

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