Click Checkbox component and enable Edittext

Asked

Viewed 343 times

1

How to enable an edittext component in the same view by clicking on the "other" checkbox component? As in the image:

inserir a descrição da imagem aqui

2 answers

1


Try it like this:

CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox);

checkbox.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        if (((CheckBox) v).isChecked()) {
           //Habilitar EditText 
            editText.setEnabled(true);         
        }
        else 
           //Desabilitar EditText 
           editText.setEnabled(false);         


      }
    });

0

After conducting some research, follows the answer:

XML

    <CheckBox
                android:id="@+id/cb_outros"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="17dp"
                android:button="@drawable/selector_chk"
                android:text="@string/cb_outros"
                android:textSize="17dp" />

     <EditText
              android:id="@+id/edt_outroMaterial"
              android:layout_width="240dp"
              android:visibility="gone"
              android:layout_height="wrap_content"
              android:hint="Ex: baterias, pilhas..."
              android:inputType="text"
              android:layout_marginRight="17dp" />

JAVA

     private void opcaoCheckOutro(){
    cbOutros.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

             EditText campoOutroMaterial = view.findViewById(R.id.edt_outroMaterial);

            if (cbOutros.isChecked()) {
                campoOutroMaterial.setVisibility(View.VISIBLE);

            } else {
                linearLayoutCheckOutro.setVisibility(View.GONE);
                campoOutroMaterial.setText("");
            }
        }
    });

}

Browser other questions tagged

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